Publish Python Package #6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Publish Python Package | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| publish_to: | |
| description: Target package index | |
| required: true | |
| default: testpypi | |
| type: choice | |
| options: | |
| - testpypi | |
| - pypi | |
| release: | |
| types: | |
| - published | |
| jobs: | |
| build: | |
| name: Build distributions | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| steps: | |
| - name: Check out repository | |
| uses: actions/checkout@v6 | |
| with: | |
| persist-credentials: false | |
| - name: Set up Python | |
| uses: actions/setup-python@v6 | |
| with: | |
| python-version: '3.12' | |
| - name: Build distributions | |
| run: | | |
| rm -rf dist | |
| python -m pip install --upgrade build twine | |
| python -m build | |
| python -m twine check dist/* | |
| - name: Upload distributions | |
| uses: actions/upload-artifact@v5 | |
| with: | |
| name: python-package-distributions | |
| path: dist/ | |
| attach-release-assets: | |
| name: Attach assets to GitHub Release | |
| if: github.event_name == 'release' | |
| needs: build | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| steps: | |
| - name: Download distributions | |
| uses: actions/download-artifact@v6 | |
| with: | |
| name: python-package-distributions | |
| path: dist/ | |
| - name: Prepare release assets | |
| run: | | |
| mkdir -p release-assets | |
| cp dist/* release-assets/ | |
| wheel=$(find dist -maxdepth 1 -name 'routesmith-*.whl' | head -n 1) | |
| sdist=$(find dist -maxdepth 1 -name 'routesmith-*.tar.gz' | head -n 1) | |
| # Stable aliases power README download/install links for the latest release. | |
| cp "$wheel" release-assets/routesmith-latest-py3-none-any.whl | |
| cp "$sdist" release-assets/routesmith-latest.tar.gz | |
| - name: Upload release assets | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| run: gh release upload "${{ github.event.release.tag_name }}" release-assets/* --clobber | |
| publish-testpypi: | |
| name: Upload release to TestPyPI | |
| if: github.event_name == 'workflow_dispatch' && github.event.inputs.publish_to == 'testpypi' | |
| needs: build | |
| runs-on: ubuntu-latest | |
| environment: | |
| name: testpypi | |
| url: https://test.pypi.org/project/routesmith/ | |
| permissions: | |
| id-token: write | |
| steps: | |
| - name: Download distributions | |
| uses: actions/download-artifact@v6 | |
| with: | |
| name: python-package-distributions | |
| path: dist/ | |
| - name: Publish package distributions to TestPyPI | |
| uses: pypa/gh-action-pypi-publish@v1.14.0 | |
| with: | |
| repository-url: https://test.pypi.org/legacy/ | |
| skip-existing: true | |
| publish-pypi: | |
| name: Upload release to PyPI | |
| if: github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.publish_to == 'pypi') | |
| needs: build | |
| runs-on: ubuntu-latest | |
| environment: | |
| name: pypi | |
| url: https://pypi.org/project/routesmith/ | |
| permissions: | |
| id-token: write | |
| steps: | |
| - name: Download distributions | |
| uses: actions/download-artifact@v6 | |
| with: | |
| name: python-package-distributions | |
| path: dist/ | |
| - name: Publish package distributions to PyPI | |
| uses: pypa/gh-action-pypi-publish@v1.14.0 |