|
| 1 | +name: Publish to PyPI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + - main |
| 8 | + workflow_dispatch: # Allow manual triggering |
| 9 | + |
| 10 | +jobs: |
| 11 | + publish: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + |
| 14 | + permissions: |
| 15 | + contents: write # Required to create and push tags |
| 16 | + id-token: write # Required for OIDC authentication (if using PyPI trusted publishing) |
| 17 | + |
| 18 | + steps: |
| 19 | + - name: Checkout repository |
| 20 | + uses: actions/checkout@v4 |
| 21 | + with: |
| 22 | + fetch-depth: 0 # Fetch all history for tags |
| 23 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 24 | + |
| 25 | + - name: Set up Python |
| 26 | + uses: actions/setup-python@v5 |
| 27 | + with: |
| 28 | + python-version: '3.11' |
| 29 | + |
| 30 | + - name: Install uv |
| 31 | + uses: astral-sh/setup-uv@v4 |
| 32 | + with: |
| 33 | + enable-cache: true |
| 34 | + |
| 35 | + - name: Read version from pyproject.toml |
| 36 | + id: version |
| 37 | + run: | |
| 38 | + VERSION=$(uv version | awk '{print $2}') |
| 39 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 40 | + echo "Package version: $VERSION" |
| 41 | + |
| 42 | + - name: Check if version already published |
| 43 | + id: check_published |
| 44 | + run: | |
| 45 | + VERSION="${{ steps.version.outputs.version }}" |
| 46 | + |
| 47 | + # Check if Git tag exists |
| 48 | + if git rev-parse "v$VERSION" >/dev/null 2>&1; then |
| 49 | + echo "tag_exists=true" >> $GITHUB_OUTPUT |
| 50 | + echo "Git tag v$VERSION already exists" |
| 51 | + else |
| 52 | + echo "tag_exists=false" >> $GITHUB_OUTPUT |
| 53 | + echo "Git tag v$VERSION does not exist" |
| 54 | + fi |
| 55 | + |
| 56 | + # Check if version exists on PyPI |
| 57 | + if curl -s "https://pypi.org/pypi/pytest-api-cov/$VERSION/json" > /dev/null 2>&1; then |
| 58 | + echo "pypi_exists=true" >> $GITHUB_OUTPUT |
| 59 | + echo "Version $VERSION already published to PyPI" |
| 60 | + else |
| 61 | + echo "pypi_exists=false" >> $GITHUB_OUTPUT |
| 62 | + echo "Version $VERSION not found on PyPI" |
| 63 | + fi |
| 64 | + |
| 65 | + - name: Create version tag |
| 66 | + if: steps.check_published.outputs.tag_exists == 'false' |
| 67 | + run: | |
| 68 | + git config user.name "github-actions[bot]" |
| 69 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 70 | + git tag -a "v${{ steps.version.outputs.version }}" -m "Release version ${{ steps.version.outputs.version }}" |
| 71 | + git push origin "v${{ steps.version.outputs.version }}" |
| 72 | + |
| 73 | + - name: Set up build environment |
| 74 | + if: steps.check_published.outputs.pypi_exists == 'false' |
| 75 | + run: | |
| 76 | + uv sync --dev |
| 77 | + |
| 78 | + - name: Run pipeline (tests, linting, etc.) |
| 79 | + if: steps.check_published.outputs.pypi_exists == 'false' |
| 80 | + run: make pipeline |
| 81 | + |
| 82 | + - name: Build package |
| 83 | + if: steps.check_published.outputs.pypi_exists == 'false' |
| 84 | + run: make build |
| 85 | + |
| 86 | + - name: Publish to PyPI |
| 87 | + if: steps.check_published.outputs.pypi_exists == 'false' |
| 88 | + env: |
| 89 | + PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }} |
| 90 | + run: | |
| 91 | + if [ -z "$PYPI_TOKEN" ]; then |
| 92 | + echo "❌ PYPI_TOKEN secret is not set. Please configure it in repository settings." |
| 93 | + echo " Settings → Secrets and variables → Actions → New repository secret" |
| 94 | + exit 1 |
| 95 | + fi |
| 96 | + echo $PYPI_TOKEN > /tmp/pypi_token.txt |
| 97 | + uv publish --token $(cat /tmp/pypi_token.txt) |
| 98 | + rm /tmp/pypi_token.txt |
| 99 | + |
| 100 | + - name: Verify publication |
| 101 | + if: steps.check_published.outputs.pypi_exists == 'false' |
| 102 | + run: | |
| 103 | + sleep 10 # Wait for PyPI to index the package |
| 104 | + uv run --with pytest-api-cov --no-project -- python -c \ |
| 105 | + "import pytest_api_cov; print(f'✅ Published version: {pytest_api_cov.__version__}')" |
| 106 | + |
| 107 | + - name: Skip publish - already published |
| 108 | + if: steps.check_published.outputs.pypi_exists == 'true' |
| 109 | + run: | |
| 110 | + echo "✅ Version ${{ steps.version.outputs.version }} already published to PyPI. Skipping publish." |
| 111 | +
|
0 commit comments