|
| 1 | +name: Publish release to PyPi |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [published] |
| 6 | +env: |
| 7 | + PYTHON_VERSION: "3.12" |
| 8 | + |
| 9 | +jobs: |
| 10 | + build: |
| 11 | + name: Builds and prepares for publishing releases to PyPI |
| 12 | + runs-on: ubuntu-latest |
| 13 | + outputs: |
| 14 | + version: ${{ steps.vars.outputs.tag }} |
| 15 | + permissions: |
| 16 | + id-token: write # Required for PyPI publishing |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@v5.0.1 |
| 19 | + - name: Get tag |
| 20 | + id: vars |
| 21 | + run: echo "tag=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT |
| 22 | + - name: Validate version number |
| 23 | + run: >- |
| 24 | + if [[ "${{ github.event.release.prerelease }}" == "true" ]]; then |
| 25 | + if ! [[ "${{ steps.vars.outputs.tag }}" =~ "b" ]]; then |
| 26 | + echo "Pre-release: Tag is missing beta suffix (${{ steps.vars.outputs.tag }})" |
| 27 | + exit 1 |
| 28 | + fi |
| 29 | + else |
| 30 | + if [[ "${{ steps.vars.outputs.tag }}" =~ "b" ]]; then |
| 31 | + echo "Release: Tag must not have a beta suffix (${{ steps.vars.outputs.tag }})" |
| 32 | + exit 1 |
| 33 | + fi |
| 34 | + fi |
| 35 | + - name: Set up Python ${{ env.PYTHON_VERSION }} |
| 36 | + uses: actions/setup-python@v6.0.0 |
| 37 | + with: |
| 38 | + python-version: ${{ env.PYTHON_VERSION }} |
| 39 | + - name: Install build |
| 40 | + run: >- |
| 41 | + pip install build tomli tomli-w |
| 42 | + - name: Set Python project version from tag |
| 43 | + shell: python |
| 44 | + run: |- |
| 45 | + import tomli |
| 46 | + import tomli_w |
| 47 | +
|
| 48 | + with open("pyproject.toml", "rb") as f: |
| 49 | + pyproject = tomli.load(f) |
| 50 | +
|
| 51 | + pyproject["project"]["version"] = "${{ steps.vars.outputs.tag }}" |
| 52 | +
|
| 53 | + with open("pyproject.toml", "wb") as f: |
| 54 | + tomli_w.dump(pyproject, f) |
| 55 | + - name: Build python package |
| 56 | + run: >- |
| 57 | + python3 -m build |
| 58 | +
|
| 59 | + - uses: actions/upload-artifact@v5 |
| 60 | + with: |
| 61 | + name: dist |
| 62 | + path: ./dist |
| 63 | + |
| 64 | + publish: |
| 65 | + needs: ['build'] |
| 66 | + environment: 'publish' |
| 67 | + |
| 68 | + name: upload release to PyPI |
| 69 | + runs-on: ubuntu-latest |
| 70 | + permissions: |
| 71 | + id-token: write |
| 72 | + steps: |
| 73 | + - uses: actions/download-artifact@v6 |
| 74 | + with: |
| 75 | + name: dist |
| 76 | + path: dist |
| 77 | + |
| 78 | + - name: Publish release to PyPI |
| 79 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 80 | + with: |
| 81 | + packages-dir: dist/ |
0 commit comments