Workflow file for this run
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: Build and Upload CLI Python Package | |
| on: | |
| release: | |
| types: [published] | |
| permissions: | |
| contents: read | |
| jobs: | |
| build-release-distributions: | |
| runs-on: ubuntu-latest | |
| defaults: | |
| run: | |
| working-directory: cli | |
| steps: | |
| - uses: actions/checkout@v6 | |
| - uses: actions/setup-python@v6 | |
| with: | |
| python-version: "3.x" | |
| - name: Ensure version is set in pyproject.toml | |
| run: | | |
| set -euo pipefail | |
| PYPROJECT_VERSION=$(awk -F '"' '/^\s*version\s*=\s*"/ {print $2; exit}' pyproject.toml) | |
| if [ -z "${PYPROJECT_VERSION:-}" ]; then | |
| echo "ERROR: Unable to read cli [project].version from cli/pyproject.toml" | |
| exit 1 | |
| fi | |
| echo "CLI version in pyproject.toml: $PYPROJECT_VERSION" | |
| - name: Check if version already exists on PyPI | |
| run: | | |
| set -euo pipefail | |
| PKG_NAME=$(grep -E '^name\s*=\s*' pyproject.toml | head -1 | sed 's/name\s*=\s*"\(.*\)"/\1/') | |
| PKG_VERSION=$(grep -E '^version\s*=\s*' pyproject.toml | head -1 | sed 's/version\s*=\s*"\(.*\)"/\1/') | |
| echo "Package name: $PKG_NAME" | |
| echo "Package version: $PKG_VERSION" | |
| echo "Checking if version exists on PyPI..." | |
| EXISTS=$(curl -s "https://pypi.org/pypi/$PKG_NAME/json" | grep -F "\"$PKG_VERSION\"" || true) | |
| if [ -n "$EXISTS" ]; then | |
| echo "Error: Version $PKG_VERSION of $PKG_NAME already exists on PyPI." >&2 | |
| exit 1 | |
| fi | |
| - name: Build release distributions | |
| run: | | |
| python -m pip install build | |
| python -m build | |
| - name: Upload distributions | |
| uses: actions/upload-artifact@v7 | |
| with: | |
| name: release-dists | |
| path: cli/dist/ | |
| pypi-publish: | |
| runs-on: ubuntu-latest | |
| needs: | |
| - build-release-distributions | |
| environment: | |
| name: pypi | |
| steps: | |
| - name: Retrieve release distributions | |
| uses: actions/download-artifact@v8 | |
| with: | |
| name: release-dists | |
| path: dist/ | |
| - name: Publish release distributions to PyPI | |
| uses: pypa/gh-action-pypi-publish@release/v1 | |
| with: | |
| packages-dir: dist/ | |
| password: ${{ secrets.PYPI_API_TOKEN }} | |
| notify-on-failure: | |
| runs-on: ubuntu-latest | |
| needs: | |
| - build-release-distributions | |
| - pypi-publish | |
| if: failure() | |
| permissions: | |
| contents: read | |
| issues: write | |
| steps: | |
| - name: Open issue on CLI publish failure | |
| uses: actions/github-script@v9 | |
| with: | |
| script: | | |
| const releaseTag = context.payload.release?.tag_name || context.ref; | |
| const releaseUrl = context.payload.release?.html_url || ''; | |
| const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`; | |
| const title = `CLI publish to PyPI failed for release ${releaseTag}`; | |
| const body = [ | |
| `The \`Build and Upload CLI Python Package\` workflow failed for release **${releaseTag}**.`, | |
| '', | |
| `A common cause is that \`cli/pyproject.toml\` was not bumped before publishing, so the version already exists on PyPI.`, | |
| '', | |
| `- Workflow run: ${runUrl}`, | |
| ...(releaseUrl ? [`- Release: ${releaseUrl}`] : []), | |
| '', | |
| `Bump \`cli/pyproject.toml\`, merge, and re-publish the release (or cut a new one) to retry.`, | |
| ].join('\n'); | |
| await github.rest.issues.create({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| title, | |
| body, | |
| labels: ['cli', 'bug'], | |
| }); |