Repository navigation
213 lines (203 loc) · 7.63 KB
/
Copy pathrelease.yml
File metadata and controls
213 lines (203 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
name: Release
# Reusable workflow: cut a git tag and a GitHub Release from the version
# declared in the repository, so the declared version and the tag can never
# drift apart. Idempotent - if the tag already exists it does nothing.
#
# Call it from a repository with:
#
# jobs:
# release:
# permissions:
# contents: write
# uses: DesignBuilderSoftware/db-github-workflows/.github/workflows/release.yml@v1
#
# See README.md for the full input reference.
on:
workflow_call:
inputs:
version-file:
description: >-
File to read the version from. pyproject.toml and package.json are
parsed; anything else is read as a plain-text file containing only
the version. Ignored when `version` is set.
type: string
default: pyproject.toml
version:
description: >-
Explicit version (e.g. 1.2.3), for repositories that keep the version
somewhere this workflow cannot parse. Overrides `version-file`.
type: string
default: ''
tag-prefix:
description: Prefix prepended to the version to form the tag.
type: string
default: v
target:
description: >-
Commit-ish the tag points at. Defaults to the SHA that triggered the
calling workflow.
type: string
default: ''
title:
description: Release title. Defaults to the tag.
type: string
default: ''
notes-file:
description: >-
Path to a file holding the release notes. When empty, notes are
generated from the commits and pull requests since the last release.
type: string
default: ''
draft:
description: Create the release as a draft.
type: boolean
default: false
prerelease:
description: Mark the release as a pre-release.
type: boolean
default: false
fail-if-tag-exists:
description: >-
Fail instead of skipping when the tag is already present. Useful if
you would rather be told loudly that a version was not bumped.
type: boolean
default: false
runs-on:
description: Runner label for the release job.
type: string
default: ubuntu-latest
secrets:
token:
description: >-
Token used to create the tag and release. Optional; defaults to the
caller's GITHUB_TOKEN. Supply a PAT if the published release needs to
trigger other workflows, which GITHUB_TOKEN deliberately will not.
required: false
outputs:
version:
description: The resolved version, without the tag prefix.
value: ${{ jobs.release.outputs.version }}
tag:
description: The tag name (prefix + version).
value: ${{ jobs.release.outputs.tag }}
created:
description: "'true' if a release was created, 'false' if it already existed."
value: ${{ jobs.release.outputs.created }}
url:
description: URL of the release; empty when nothing was created.
value: ${{ jobs.release.outputs.url }}
jobs:
release:
runs-on: ${{ inputs.runs-on }}
outputs:
version: ${{ steps.resolve.outputs.version }}
tag: ${{ steps.resolve.outputs.tag }}
created: ${{ steps.create.outputs.created || 'false' }}
url: ${{ steps.create.outputs.url }}
steps:
- uses: actions/checkout@v4
with:
# Full history and tags, so the "does this tag exist" check is honest
# and the generated notes have something to diff against.
fetch-depth: 0
# Kept in step with actions/read-version/action.yml in this repository:
# a reusable workflow cannot call a composite action stored alongside it
# without pinning a ref, so the resolution rules live in both places.
- name: Resolve version
id: resolve
env:
VERSION_INPUT: ${{ inputs.version }}
VERSION_FILE: ${{ inputs.version-file }}
TAG_PREFIX: ${{ inputs.tag-prefix }}
run: |
set -euo pipefail
if [ -n "$VERSION_INPUT" ]; then
version="$VERSION_INPUT"
origin="the version input"
elif [ ! -f "$VERSION_FILE" ]; then
echo "::error::version-file '$VERSION_FILE' does not exist"
exit 1
else
origin="$VERSION_FILE"
case "$(basename "$VERSION_FILE")" in
pyproject.toml)
version=$(python3 -c 'import pathlib, sys, tomllib; d = tomllib.loads(pathlib.Path(sys.argv[1]).read_text(encoding="utf-8")); print(d.get("project", {}).get("version") or d.get("tool", {}).get("poetry", {}).get("version") or "")' "$VERSION_FILE")
;;
package.json)
version=$(jq -er '.version' "$VERSION_FILE")
;;
*)
version=$(tr -d '[:space:]' < "$VERSION_FILE")
;;
esac
fi
if [ -z "$version" ]; then
echo "::error::could not read a version from $origin"
exit 1
fi
echo "Resolved version $version from $origin"
echo "version=$version" >> "$GITHUB_OUTPUT"
echo "tag=${TAG_PREFIX}${version}" >> "$GITHUB_OUTPUT"
- name: Check whether the tag already exists
id: check
env:
TAG: ${{ steps.resolve.outputs.tag }}
FAIL_IF_EXISTS: ${{ inputs.fail-if-tag-exists }}
run: |
set -euo pipefail
if git rev-parse -q --verify "refs/tags/$TAG" >/dev/null; then
if [ "$FAIL_IF_EXISTS" = "true" ]; then
echo "::error::tag $TAG already exists - was the version bumped?"
exit 1
fi
echo "Tag $TAG already exists - nothing to do."
echo "exists=true" >> "$GITHUB_OUTPUT"
else
echo "exists=false" >> "$GITHUB_OUTPUT"
fi
- name: Create tag and release
id: create
if: steps.check.outputs.exists == 'false'
env:
GH_TOKEN: ${{ secrets.token || secrets.GITHUB_TOKEN }}
TAG: ${{ steps.resolve.outputs.tag }}
TARGET: ${{ inputs.target || github.sha }}
TITLE: ${{ inputs.title || steps.resolve.outputs.tag }}
NOTES_FILE: ${{ inputs.notes-file }}
DRAFT: ${{ inputs.draft }}
PRERELEASE: ${{ inputs.prerelease }}
run: |
set -euo pipefail
args=( "$TAG" --target "$TARGET" --title "$TITLE" )
if [ -n "$NOTES_FILE" ]; then
if [ ! -f "$NOTES_FILE" ]; then
echo "::error::notes-file '$NOTES_FILE' does not exist"
exit 1
fi
args+=( --notes-file "$NOTES_FILE" )
else
args+=( --generate-notes )
fi
if [ "$DRAFT" = "true" ]; then
args+=( --draft )
fi
if [ "$PRERELEASE" = "true" ]; then
args+=( --prerelease )
fi
url=$(gh release create "${args[@]}")
echo "Created $TAG -> $url"
echo "created=true" >> "$GITHUB_OUTPUT"
echo "url=$url" >> "$GITHUB_OUTPUT"
- name: Summary
if: always() && steps.resolve.outcome == 'success'
env:
TAG: ${{ steps.resolve.outputs.tag }}
CREATED: ${{ steps.create.outputs.created || 'false' }}
URL: ${{ steps.create.outputs.url }}
run: |
set -euo pipefail
if [ "$CREATED" = "true" ]; then
echo "Released **$TAG** - $URL" >> "$GITHUB_STEP_SUMMARY"
else
echo "No release created; **$TAG** already exists." >> "$GITHUB_STEP_SUMMARY"
fi