|
| 1 | +name: Upload Python Package |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + python-version: |
| 7 | + description: 'Python version to build with' |
| 8 | + required: false |
| 9 | + default: '3.x' |
| 10 | + type: string |
| 11 | + package-name: |
| 12 | + description: 'PyPI package name, used to construct the PyPI URL (e.g. my-package)' |
| 13 | + required: true |
| 14 | + type: string |
| 15 | + environment-name: |
| 16 | + description: 'GitHub environment to use for the PyPI publish job' |
| 17 | + required: false |
| 18 | + default: 'pypi' |
| 19 | + type: string |
| 20 | + working-directory: |
| 21 | + description: 'Working directory for the build, relative to the repo root. Useful for monorepos.' |
| 22 | + required: false |
| 23 | + default: '.' |
| 24 | + type: string |
| 25 | + use-uv: |
| 26 | + description: 'Use uv to build the package instead of pip + build' |
| 27 | + required: false |
| 28 | + default: false |
| 29 | + type: boolean |
| 30 | + |
| 31 | +jobs: |
| 32 | + build: |
| 33 | + name: Build distribution |
| 34 | + runs-on: ubuntu-latest |
| 35 | + permissions: |
| 36 | + contents: read |
| 37 | + # Apply working-directory to all run steps in this job |
| 38 | + defaults: |
| 39 | + run: |
| 40 | + working-directory: ${{ inputs.working-directory }} |
| 41 | + |
| 42 | + steps: |
| 43 | + - uses: actions/checkout@v4 |
| 44 | + |
| 45 | + # When using uv, Python is managed by uv itself via the python-version input. |
| 46 | + # When using pip, setup-python handles it instead. |
| 47 | + - name: Set up Python |
| 48 | + if: ${{ !inputs.use-uv }} |
| 49 | + uses: actions/setup-python@v5 |
| 50 | + with: |
| 51 | + python-version: ${{ inputs.python-version }} |
| 52 | + |
| 53 | + - name: Set up uv |
| 54 | + if: inputs.use-uv |
| 55 | + uses: astral-sh/setup-uv@v5 |
| 56 | + with: |
| 57 | + python-version: ${{ inputs.python-version }} |
| 58 | + |
| 59 | + - name: Install build dependencies |
| 60 | + if: ${{ !inputs.use-uv }} |
| 61 | + run: | |
| 62 | + python -m pip install --upgrade pip |
| 63 | + pip install build |
| 64 | +
|
| 65 | + - name: Build package |
| 66 | + run: ${{ inputs.use-uv && 'uv build' || 'python -m build' }} |
| 67 | + |
| 68 | + - name: Store the distribution packages |
| 69 | + uses: actions/upload-artifact@v4 |
| 70 | + with: |
| 71 | + name: python-package-distributions |
| 72 | + # upload-artifact paths are relative to the workspace root, not working-directory, |
| 73 | + # so we need to include the working-directory prefix explicitly. |
| 74 | + path: ${{ inputs.working-directory }}/dist/ |
| 75 | + |
| 76 | + publish-to-pypi: |
| 77 | + name: Publish Python distribution to PyPI |
| 78 | + needs: build |
| 79 | + runs-on: ubuntu-latest |
| 80 | + environment: |
| 81 | + name: ${{ inputs.environment-name }} |
| 82 | + url: https://pypi.org/p/${{ inputs.package-name }} |
| 83 | + permissions: |
| 84 | + id-token: write # required for OIDC-based PyPI publishing (no API token needed) |
| 85 | + |
| 86 | + steps: |
| 87 | + - name: Download all the dists |
| 88 | + uses: actions/download-artifact@v4 |
| 89 | + with: |
| 90 | + name: python-package-distributions |
| 91 | + path: dist/ |
| 92 | + |
| 93 | + - name: Publish distribution to PyPI |
| 94 | + uses: pypa/gh-action-pypi-publish@release/v1 |
0 commit comments