|
1 | | -# Publishes a release to PyPI with trusted publishing (no stored token). |
2 | | -# One-time setup on pypi.org: add a pending publisher for project |
3 | | -# "saltext.noderings", owner "noderings", repository "saltext-noderings", |
4 | | -# workflow "release.yml", environment "release". |
| 1 | +# Builds and publishes a release when release-please pushes a vX.Y.Z tag. |
| 2 | +# |
| 3 | +# 1. build: sdist and wheel from the tag (setuptools_scm turns the tag into |
| 4 | +# the version) with build provenance attestations. |
| 5 | +# 2. pypi: publishes to https://pypi.org/project/saltext.noderings/ with |
| 6 | +# trusted publishing (no stored token) from the "pypi" environment. Skipped |
| 7 | +# with a warning until the repository variable PYPI_TRUSTED_PUBLISHING is |
| 8 | +# "true" (set it once the pending publisher exists on pypi.org, see |
| 9 | +# CONTRIBUTING.md, Releasing). |
| 10 | +# 3. github-release: uploads the files to the draft release made by |
| 11 | +# release-please and publishes it. |
| 12 | +# |
| 13 | +# Always runs on GitHub-hosted runners: release builds never touch the |
| 14 | +# self-hosted runner. |
5 | 15 | name: Release |
6 | 16 |
|
7 | | -on: |
| 17 | +"on": |
8 | 18 | push: |
9 | 19 | tags: ["v*.*.*"] |
| 20 | + workflow_dispatch: # run it from a tag (Use workflow from: Tags) to retry a release |
10 | 21 |
|
11 | 22 | permissions: |
12 | | - contents: write |
| 23 | + contents: read |
| 24 | + |
| 25 | +concurrency: |
| 26 | + group: release-${{ github.ref_name }} |
| 27 | + cancel-in-progress: false |
| 28 | + |
| 29 | +env: |
| 30 | + TAG: ${{ github.ref_name }} |
13 | 31 |
|
14 | 32 | jobs: |
15 | 33 | build: |
| 34 | + name: Build |
16 | 35 | runs-on: ubuntu-latest |
| 36 | + timeout-minutes: 15 |
| 37 | + permissions: |
| 38 | + contents: read |
| 39 | + id-token: write |
| 40 | + attestations: write |
17 | 41 | steps: |
18 | | - - uses: actions/checkout@v7 |
| 42 | + - name: Check this runs for a version tag |
| 43 | + run: | |
| 44 | + case "${TAG}" in |
| 45 | + v[0-9]*.[0-9]*.[0-9]*) ;; |
| 46 | + *) echo "::error::Run this workflow from a vX.Y.Z tag, not ${TAG}."; exit 1 ;; |
| 47 | + esac |
| 48 | + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
19 | 49 | with: |
20 | 50 | fetch-depth: 0 |
21 | | - - uses: actions/setup-python@v7 |
| 51 | + persist-credentials: false |
| 52 | + - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 |
22 | 53 | with: |
23 | 54 | python-version: "3.12" |
24 | | - - run: pip install build |
| 55 | + - run: python -m pip install build twine |
25 | 56 | - run: python -m build |
26 | | - - uses: actions/upload-artifact@v7 |
| 57 | + - run: python -m twine check --strict dist/* |
| 58 | + - name: Check the version matches the tag |
| 59 | + run: | |
| 60 | + want="${TAG#v}" |
| 61 | + ls dist |
| 62 | + test -f "dist/saltext_noderings-${want}.tar.gz" || test -f "dist/saltext.noderings-${want}.tar.gz" \ |
| 63 | + || { echo "::error::The sdist version does not match ${TAG}."; exit 1; } |
| 64 | + ls dist/*-"${want}"-py*.whl >/dev/null || { echo "::error::The wheel version does not match ${TAG}."; exit 1; } |
| 65 | + - uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4.2.2 |
| 66 | + with: |
| 67 | + subject-path: dist/* |
| 68 | + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 |
27 | 69 | with: |
28 | 70 | name: dist |
29 | 71 | path: dist/ |
| 72 | + if-no-files-found: error |
30 | 73 |
|
31 | 74 | pypi: |
| 75 | + name: Publish to PyPI |
32 | 76 | needs: build |
33 | 77 | runs-on: ubuntu-latest |
34 | | - environment: release |
| 78 | + timeout-minutes: 10 |
| 79 | + environment: |
| 80 | + name: pypi |
| 81 | + url: https://pypi.org/project/saltext.noderings/ |
35 | 82 | permissions: |
36 | 83 | id-token: write |
37 | 84 | steps: |
38 | | - - uses: actions/download-artifact@v8 |
| 85 | + - name: Check PyPI trusted publishing is set up |
| 86 | + id: check |
| 87 | + env: |
| 88 | + ENABLED: ${{ vars.PYPI_TRUSTED_PUBLISHING }} |
| 89 | + run: | |
| 90 | + if [ "${ENABLED}" = "true" ]; then |
| 91 | + echo "publish=true" >> "$GITHUB_OUTPUT" |
| 92 | + else |
| 93 | + echo "::warning title=PyPI skipped::Add the pending publisher on pypi.org, then set the repository variable PYPI_TRUSTED_PUBLISHING=true (see CONTRIBUTING.md, Releasing). Re-run this workflow from the tag afterwards." |
| 94 | + echo "publish=false" >> "$GITHUB_OUTPUT" |
| 95 | + fi |
| 96 | + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 |
| 97 | + if: steps.check.outputs.publish == 'true' |
39 | 98 | with: |
40 | 99 | name: dist |
41 | 100 | path: dist/ |
42 | | - - uses: pypa/gh-action-pypi-publish@release/v1 |
| 101 | + - uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3 |
| 102 | + if: steps.check.outputs.publish == 'true' |
| 103 | + with: |
| 104 | + skip-existing: true |
43 | 105 |
|
44 | 106 | github-release: |
45 | | - needs: pypi |
| 107 | + name: GitHub release |
| 108 | + needs: [build, pypi] |
| 109 | + if: always() && needs.build.result == 'success' && needs.pypi.result != 'failure' && needs.pypi.result != 'cancelled' |
46 | 110 | runs-on: ubuntu-latest |
| 111 | + timeout-minutes: 10 |
| 112 | + permissions: |
| 113 | + contents: write |
47 | 114 | steps: |
48 | | - - uses: actions/download-artifact@v8 |
| 115 | + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 |
49 | 116 | with: |
50 | 117 | name: dist |
51 | 118 | path: dist/ |
52 | | - - env: |
| 119 | + - name: Upload the files and publish the release |
| 120 | + env: |
53 | 121 | GH_TOKEN: ${{ github.token }} |
54 | | - run: gh release create "$GITHUB_REF_NAME" dist/* --repo "$GITHUB_REPOSITORY" --title "$GITHUB_REF_NAME" --notes "See CHANGELOG.md." |
| 122 | + GH_REPO: ${{ github.repository }} |
| 123 | + run: | |
| 124 | + if ! gh release view "${TAG}" >/dev/null 2>&1; then |
| 125 | + gh release create "${TAG}" --verify-tag --draft --title "${TAG}" --notes "See CHANGELOG.md." |
| 126 | + fi |
| 127 | + gh release upload "${TAG}" dist/* --clobber |
| 128 | + gh release edit "${TAG}" --draft=false --latest |
0 commit comments