|
| 1 | +name: Publish |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: ['v*'] |
| 6 | + workflow_dispatch: {} |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: write |
| 10 | + id-token: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + build: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - uses: actions/checkout@v7 |
| 17 | + |
| 18 | + - uses: actions/setup-python@v7 |
| 19 | + with: |
| 20 | + python-version: '3.11' |
| 21 | + |
| 22 | + - name: Verify tag matches package version |
| 23 | + if: startsWith(github.ref, 'refs/tags/v') |
| 24 | + run: | |
| 25 | + set -euo pipefail |
| 26 | + ver=$(python3 -c "import sys; sys.path.insert(0, 'utils'); import check_version_sync as c; print(c.config_version())") |
| 27 | + tag="${GITHUB_REF_NAME#v}" |
| 28 | + if [ "$tag" != "$ver" ]; then |
| 29 | + echo "::error::Tag ${GITHUB_REF_NAME} does not match package version ${ver}" |
| 30 | + exit 1 |
| 31 | + fi |
| 32 | + echo "Tag ${GITHUB_REF_NAME} matches package version ${ver}" |
| 33 | +
|
| 34 | + - name: Build distributions |
| 35 | + run: | |
| 36 | + python3 -m pip install --upgrade build |
| 37 | + python3 -m build |
| 38 | +
|
| 39 | + - name: Upload dist |
| 40 | + uses: actions/upload-artifact@v7 |
| 41 | + with: |
| 42 | + name: dist |
| 43 | + path: dist/ |
| 44 | + |
| 45 | + publish-testpypi: |
| 46 | + needs: build |
| 47 | + if: github.event_name == 'workflow_dispatch' |
| 48 | + runs-on: ubuntu-latest |
| 49 | + steps: |
| 50 | + - name: Download dist |
| 51 | + uses: actions/download-artifact@v8 |
| 52 | + with: |
| 53 | + name: dist |
| 54 | + path: dist/ |
| 55 | + |
| 56 | + - name: Publish to TestPyPI |
| 57 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 58 | + with: |
| 59 | + repository-url: https://test.pypi.org/ |
| 60 | + password: ${{ secrets.TEST_PYPI_API_TOKEN }} |
| 61 | + |
| 62 | + publish-pypi: |
| 63 | + needs: build |
| 64 | + if: startsWith(github.ref, 'refs/tags/v') |
| 65 | + runs-on: ubuntu-latest |
| 66 | + steps: |
| 67 | + - name: Download dist |
| 68 | + uses: actions/download-artifact@v8 |
| 69 | + with: |
| 70 | + name: dist |
| 71 | + path: dist/ |
| 72 | + |
| 73 | + - name: Publish to PyPI (Trusted Publishing / OIDC) |
| 74 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 75 | + # No `password`: uses OIDC Trusted Publishing (configured once on PyPI). |
| 76 | + # Fallback if not using Trusted Publishing: set the PYPI_API_TOKEN |
| 77 | + # secret and uncomment the `with` block below. |
| 78 | + # with: |
| 79 | + # password: ${{ secrets.PYPI_API_TOKEN }} |
| 80 | + |
| 81 | + - name: Create GitHub Release |
| 82 | + env: |
| 83 | + GH_TOKEN: ${{ github.token }} |
| 84 | + run: | |
| 85 | + set -euo pipefail |
| 86 | + ver="${GITHUB_REF_NAME#v}" |
| 87 | + awk -v v="$ver" ' |
| 88 | + index($0, "## [" v "]") == 1 {insec=1; next} |
| 89 | + /^## / && insec {exit} |
| 90 | + insec {print} |
| 91 | + ' CHANGELOG.md > release_notes.md |
| 92 | + gh release create "${GITHUB_REF_NAME}" --title "${GITHUB_REF_NAME}" \ |
| 93 | + --notes-file release_notes.md dist/* |
0 commit comments