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
| # Publish Python Package to PyPI when a release is created | |
| name: Publish to PyPI | |
| on: | |
| release: | |
| types: [published] | |
| workflow_dispatch: | |
| inputs: | |
| use_test_pypi: | |
| description: 'Publish to Test PyPI instead of PyPI' | |
| required: false | |
| default: false | |
| type: boolean | |
| permissions: | |
| contents: read | |
| id-token: write # Required for trusted publishing | |
| jobs: | |
| test-before-publish: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Install uv | |
| uses: astral-sh/setup-uv@v7 | |
| with: | |
| enable-cache: true | |
| cache-dependency-glob: "pyproject.toml" | |
| - name: Set up Python 3.11 | |
| run: | | |
| uv python install 3.11 | |
| uv venv | |
| - name: Install and test package | |
| run: | | |
| uv pip install -e .[test] | |
| uv run pytest tests/ --tb=short -q | |
| publish: | |
| runs-on: ubuntu-latest | |
| needs: test-before-publish | |
| environment: | |
| name: ${{ github.event.inputs.use_test_pypi == 'true' && 'test-pypi' || 'pypi' }} | |
| url: ${{ github.event.inputs.use_test_pypi == 'true' && 'https://test.pypi.org/p/allocator' || 'https://pypi.org/p/allocator' }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Install uv | |
| uses: astral-sh/setup-uv@v7 | |
| - name: Set up Python 3.11 | |
| run: | | |
| uv python install 3.11 | |
| uv venv | |
| - name: Build package | |
| run: | | |
| uv build | |
| - name: Check package quality | |
| run: | | |
| uv tool run twine check dist/* | |
| - name: Publish to PyPI | |
| uses: pypa/gh-action-pypi-publish@v1.18.0 | |
| with: | |
| repository-url: ${{ github.event.inputs.use_test_pypi == 'true' && 'https://test.pypi.org/legacy/' || '' }} |