Repository navigation
396 lines (385 loc) · 16.4 KB
/
Copy pathrelease.yml
File metadata and controls
396 lines (385 loc) · 16.4 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
name: Release
on:
workflow_call:
inputs:
prepare-command:
type: string
default: ""
description: Bash command that updates files before the release. Pixi is on PATH. Empty skips the prepare stage.
release-branch:
type: string
default: ""
description: Branch that gets the prepare commit. Its tip must be the tag commit. Empty means the default branch.
prepare-commit-message:
type: string
default: ""
description: "Commit message of the prepare commit. Empty means `[github-actions.ci] prepare release <tag>`."
test:
type: boolean
default: true
description: Run test.yml before the build.
test-inputs:
type: string
default: "{}"
description: JSON object with test.yml inputs (string values for string inputs, true/false for boolean inputs).
build:
type: boolean
default: true
description: Build and verify the sdist and wheel (artifact Packages-release). Set false for repos without a Python package.
pypi:
type: boolean
default: true
description: Publish to PyPI. Needs build.
testpypi:
type: boolean
default: false
description: Publish to TestPyPI and run a smoke check before the GitHub release and PyPI.
smoke-command:
type: string
default: ""
description: Command for the TestPyPI smoke check. Empty means `python -c "import <top-level module>"`.
tag:
type: string
default: ""
description: Release tag. Empty means the ref name of the trigger.
release-notes-file:
type: string
default: ""
description: File with the release body. Empty means generated release notes.
release-notes-url:
type: string
default: ""
description: URL put in a line above the release body.
github-release:
type: boolean
default: true
description: Create the GitHub release.
dry-run:
type: boolean
default: false
description: Build and verify only. Skip the TestPyPI upload, the GitHub release and PyPI.
working-directory:
type: string
default: .
description: Directory of the package, relative to the repo root.
secrets:
CDSAPI_KEY:
required: false
EARTHDATAHUB_API_KEY:
required: false
MOSEKLM_LICENSE_FILE:
required: false
GUROBI_LIC:
required: false
outputs:
ready:
description: "`true` if all checks passed and the run is not a dry run. A caller-side publish job gates on it."
value: ${{ jobs.ready.outputs.ready }}
permissions: {}
env:
TAG: ${{ inputs.tag || github.ref_name }}
jobs:
prepare:
if: inputs.prepare-command != '' && !inputs.dry-run
runs-on: ubuntu-latest
environment: release
permissions:
contents: read
outputs:
prepared: ${{ steps.check.outputs.prepared }}
steps:
- uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
with:
egress-policy: audit
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
persist-credentials: false
- id: check
env:
MESSAGE: ${{ inputs.prepare-commit-message }}
BRANCH: ${{ inputs.release-branch || github.event.repository.default_branch }}
run: |
set -euo pipefail
message="${MESSAGE:-[github-actions.ci] prepare release $TAG}"
{ echo "message<<EOF_MESSAGE"; echo "$message"; echo "EOF_MESSAGE"; } >> "$GITHUB_OUTPUT"
if [ "$(git log -1 --pretty=%B)" = "$message" ]; then
echo "prepared=true" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "prepared=false" >> "$GITHUB_OUTPUT"
[ -n "$BRANCH" ] || { echo "::error::release branch unknown, set release-branch"; exit 1; }
git rev-parse -q --verify "refs/remotes/origin/$BRANCH" > /dev/null || { echo "::error::branch $BRANCH not found"; exit 1; }
git merge-base --is-ancestor HEAD "refs/remotes/origin/$BRANCH" || { echo "::error::branch $BRANCH does not contain $TAG"; exit 1; }
echo "branch=$BRANCH" >> "$GITHUB_OUTPUT"
echo "commit=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
if: steps.check.outputs.prepared == 'false'
with:
ref: ${{ steps.check.outputs.branch }}
fetch-depth: 0
persist-credentials: false
- if: steps.check.outputs.prepared == 'false'
env:
BRANCH: ${{ steps.check.outputs.branch }}
COMMIT: ${{ steps.check.outputs.commit }}
run: |
set -euo pipefail
[ "$(git rev-parse HEAD)" = "$COMMIT" ] || { echo "::error::$BRANCH has commits after $TAG. Tag the tip of $BRANCH."; exit 1; }
- uses: prefix-dev/setup-pixi@ad1da6449d9e506fd127fd67ab2d054a769f6a94 # v0.11.2
if: steps.check.outputs.prepared == 'false'
with:
pixi-version: v0.81.0
run-install: false
- if: steps.check.outputs.prepared == 'false'
working-directory: ${{ inputs.working-directory }}
env:
PREPARE_COMMAND: ${{ inputs.prepare-command }}
run: bash -euo pipefail -c "$PREPARE_COMMAND"
- uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
id: token
if: steps.check.outputs.prepared == 'false'
with:
app-id: ${{ vars.PYPSA_BOT_ID }}
private-key: ${{ secrets.PYPSA_BOT_PRIVATE_KEY }}
permission-contents: write
- if: steps.check.outputs.prepared == 'false'
env:
GH_TOKEN: ${{ steps.token.outputs.token }}
APP_SLUG: ${{ steps.token.outputs.app-slug }}
MESSAGE: ${{ steps.check.outputs.message }}
BRANCH: ${{ steps.check.outputs.branch }}
run: |
set -euo pipefail
user_id=$(gh api "/users/${APP_SLUG}%5Bbot%5D" -q .id)
git config user.name "${APP_SLUG}[bot]"
git config user.email "${user_id}+${APP_SLUG}[bot]@users.noreply.github.com"
git add -A
git commit --allow-empty -m "$MESSAGE"
git tag -f -a "$TAG" -m "$TAG"
GIT_CONFIG_COUNT=1 \
GIT_CONFIG_KEY_0="http.$GITHUB_SERVER_URL/.extraheader" \
GIT_CONFIG_VALUE_0="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$GH_TOKEN" | base64 -w0)" \
git push --atomic origin "HEAD:refs/heads/$BRANCH" "+refs/tags/$TAG"
echo "Pushed the prepare commit and moved $TAG. The new tag starts the release run." >> "$GITHUB_STEP_SUMMARY"
test:
needs: prepare
if: always() && inputs.test && (needs.prepare.result == 'skipped' || needs.prepare.outputs.prepared == 'true')
uses: $/.github/workflows/test.yml
permissions:
contents: read
id-token: write
secrets:
CDSAPI_KEY: ${{ secrets.CDSAPI_KEY }}
EARTHDATAHUB_API_KEY: ${{ secrets.EARTHDATAHUB_API_KEY }}
MOSEKLM_LICENSE_FILE: ${{ secrets.MOSEKLM_LICENSE_FILE }}
GUROBI_LIC: ${{ secrets.GUROBI_LIC }}
with:
extras: ${{ fromJSON(inputs.test-inputs).extras || 'dev' }}
pixi-add: ${{ fromJSON(inputs.test-inputs).pixi-add || '' }}
uv-locked: ${{ fromJSON(inputs.test-inputs).uv-locked == true }}
cooldown: ${{ fromJSON(inputs.test-inputs).cooldown == null && '7d' || fromJSON(inputs.test-inputs).cooldown }}
matrix: ${{ fromJSON(inputs.test-inputs).matrix || 'auto' }}
python-versions: ${{ fromJSON(inputs.test-inputs).python-versions || '' }}
os-full: ${{ fromJSON(inputs.test-inputs).os-full || '["ubuntu-latest","macos-latest","windows-latest"]' }}
pytest-args: ${{ fromJSON(inputs.test-inputs).pytest-args || '' }}
test-command: ${{ fromJSON(inputs.test-inputs).test-command || '' }}
pixi-task: ${{ fromJSON(inputs.test-inputs).pixi-task || '' }}
pixi-environments: ${{ fromJSON(inputs.test-inputs).pixi-environments || '["default"]' }}
manifest-path: ${{ fromJSON(inputs.test-inputs).manifest-path || '' }}
pixi-version: ${{ fromJSON(inputs.test-inputs).pixi-version || 'v0.81.0' }}
cache-path: ${{ fromJSON(inputs.test-inputs).cache-path || '' }}
cache-key: ${{ fromJSON(inputs.test-inputs).cache-key || '' }}
cov-source: ${{ fromJSON(inputs.test-inputs).cov-source || '' }}
artifact-suffix: -release-test
coverage: ${{ fromJSON(inputs.test-inputs).coverage == true }}
coverage-file: ${{ fromJSON(inputs.test-inputs).coverage-file || 'coverage.xml' }}
vars-env: ${{ fromJSON(inputs.test-inputs).vars-env || '' }}
working-directory: ${{ fromJSON(inputs.test-inputs).working-directory || inputs.working-directory }}
build:
needs: [prepare, test]
if: >-
always() && inputs.build
&& (needs.prepare.result == 'skipped' || needs.prepare.outputs.prepared == 'true')
&& (needs.test.result == 'success' || (!inputs.test && needs.test.result == 'skipped'))
runs-on: ubuntu-latest
permissions:
contents: read
outputs:
dist-name: ${{ steps.baipp.outputs.package_name }}
version: ${{ steps.verify.outputs.version }}
import-name: ${{ steps.verify.outputs.import-name }}
python-version: ${{ steps.verify.outputs.python-version }}
steps:
- uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
with:
egress-policy: audit
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
persist-credentials: false
- env:
DRY_RUN: ${{ inputs.dry-run }}
run: |
set -euo pipefail
if git rev-parse -q --verify "refs/tags/$TAG" > /dev/null; then
git checkout -q "refs/tags/$TAG"
elif [ "$DRY_RUN" = true ]; then
git tag "$TAG"
else
echo "::error::tag $TAG not found"
exit 1
fi
- uses: hynek/build-and-inspect-python-package@67230f7b8410ae0052b39c1efc89d89fff43ff10 # v3.0.2
id: baipp
with:
path: ${{ inputs.working-directory }}
upload-name-suffix: -release
- id: verify
env:
DIST: ${{ steps.baipp.outputs.dist }}
PYTHONS: ${{ steps.baipp.outputs.supported_python_classifiers_json_array }}
run: |
set -euo pipefail
wheel=$(ls "$DIST"/*.whl)
version=$(basename "$wheel" | cut -d- -f2)
[ "$version" = "${TAG#v}" ] || { echo "::error::wheel version $version does not match tag $TAG"; exit 1; }
import_name=$(unzip -Z1 "$wheel" | cut -d/ -f1 | sed 's/\.py$//' | grep -E '^[A-Za-z_][A-Za-z0-9_]*$' | sort -u | head -n 1)
{
echo "version=$version"
echo "import-name=$import_name"
echo "python-version=$(jq -r '.[-1] // empty' <<< "${PYTHONS:-[]}")"
} >> "$GITHUB_OUTPUT"
testpypi:
needs: build
if: always() && needs.build.result == 'success' && inputs.testpypi && !inputs.dry-run
runs-on: ubuntu-latest
environment:
name: testpypi
url: https://test.pypi.org/project/${{ needs.build.outputs.dist-name }}/${{ needs.build.outputs.version }}
permissions:
id-token: write
steps:
- uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
with:
egress-policy: audit
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: Packages-release
path: dist
- uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3
with:
repository-url: https://test.pypi.org/legacy/
skip-existing: true
smoke:
needs: [build, testpypi]
if: always() && needs.testpypi.result == 'success'
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
with:
egress-policy: audit
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: Packages-release
path: ${{ runner.temp }}/wheels
- uses: $/.github/actions/setup-env
with:
mode: library
python-version: ${{ needs.build.outputs.python-version }}
extras: ""
wheel-dir: ${{ runner.temp }}/wheels
working-directory: ${{ inputs.working-directory }}
- env:
DIST_NAME: ${{ needs.build.outputs.dist-name }}
VERSION: ${{ needs.build.outputs.version }}
run: |
set -euo pipefail
deadline=$((SECONDS + 300))
until $RUN uv pip install --no-cache --no-deps --reinstall-package "$DIST_NAME" \
--index-url https://test.pypi.org/simple/ "$DIST_NAME==$VERSION"; do
[ "$SECONDS" -lt "$deadline" ] || { echo "::error::$DIST_NAME==$VERSION not installable from TestPyPI after 5 minutes"; exit 1; }
sleep 20
done
- env:
SMOKE_COMMAND: ${{ inputs.smoke-command }}
IMPORT_NAME: ${{ needs.build.outputs.import-name }}
run: |
set -euo pipefail
cd "$RUNNER_TEMP"
$RUN bash -euo pipefail -c "${SMOKE_COMMAND:-python -c \"import $IMPORT_NAME\"}"
ready:
needs: [prepare, test, build, testpypi, smoke]
if: always()
runs-on: ubuntu-latest
permissions: {}
outputs:
ready: ${{ steps.ready.outputs.ready }}
steps:
- uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
with:
egress-policy: audit
- id: ready
env:
READY: >-
${{
!inputs.dry-run
&& (needs.prepare.result == 'skipped' || needs.prepare.outputs.prepared == 'true')
&& (needs.test.result == 'success' || (!inputs.test && needs.test.result == 'skipped'))
&& (needs.build.result == 'success' || (!inputs.build && needs.build.result == 'skipped'))
&& (needs.smoke.result == 'success' || (!inputs.testpypi && needs.smoke.result == 'skipped'))
}}
run: echo "ready=$READY" >> "$GITHUB_OUTPUT"
github-release:
needs: ready
if: always() && needs.ready.outputs.ready == 'true' && inputs.github-release
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
with:
egress-policy: audit
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
if: inputs.release-notes-file != ''
with:
ref: ${{ env.TAG }}
persist-credentials: false
- env:
GH_TOKEN: ${{ github.token }}
NOTES_FILE: ${{ inputs.release-notes-file }}
NOTES_URL: ${{ inputs.release-notes-url }}
run: |
set -euo pipefail
body="$RUNNER_TEMP/release-notes.md"
: > "$body"
if [ -n "$NOTES_URL" ]; then printf 'Release notes: %s\n\n' "$NOTES_URL" >> "$body"; fi
args=(--repo "$GITHUB_REPOSITORY" --verify-tag --notes-file "$body")
if [ -n "$NOTES_FILE" ]; then cat "$NOTES_FILE" >> "$body"; else args+=(--generate-notes); fi
if [[ "${TAG#v}" =~ [0-9](a|b|rc)[0-9]*$ ]]; then args+=(--prerelease); fi
gh release create "$TAG" "${args[@]}"
publish:
needs: [build, ready, github-release]
if: >-
always() && needs.ready.outputs.ready == 'true' && inputs.pypi
&& (needs.github-release.result == 'success' || needs.github-release.result == 'skipped')
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/project/${{ needs.build.outputs.dist-name }}/${{ needs.build.outputs.version }}
permissions:
id-token: write
steps:
- uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
with:
egress-policy: audit
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: Packages-release
path: dist
- uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3