Publish to PyPI #1
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 to PyPI | |
| on: | |
| release: | |
| types: [published] | |
| workflow_dispatch: | |
| inputs: | |
| version: | |
| description: "Version to publish (e.g. 0.1.0)" | |
| required: true | |
| default: "0.1.0" | |
| jobs: | |
| publish-pypi: | |
| name: Publish to PyPI | |
| runs-on: ubuntu-latest | |
| environment: pypi | |
| permissions: | |
| id-token: write # Trusted publisher (OIDC) | |
| steps: | |
| - name: Checkout Source | |
| uses: actions/checkout@v4 | |
| - name: Get version | |
| id: version | |
| run: | | |
| if [ -n "${{ github.event.inputs.version }}" ]; then | |
| version="${{ github.event.inputs.version }}" | |
| tag="v${version}" | |
| else | |
| tag="${GITHUB_REF#refs/tags/}" | |
| version="${tag#v}" | |
| fi | |
| echo "tag=${tag}" >> $GITHUB_OUTPUT | |
| echo "version=${version}" >> $GITHUB_OUTPUT | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.x" | |
| - name: Update version in packaging files | |
| working-directory: dist/pypi | |
| run: | | |
| sed -i "s/^version = \".*\"/version = \"${{ steps.version.outputs.version }}\"/" pyproject.toml | |
| - name: Build package | |
| working-directory: dist/pypi | |
| run: | | |
| python -m pip install --upgrade build | |
| python -m build | |
| - name: Publish to PyPI | |
| working-directory: dist/pypi | |
| env: | |
| TWINE_USERNAME: __token__ | |
| TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} | |
| run: | | |
| python -m pip install --upgrade twine | |
| twine upload dist/* |