|
| 1 | +name: Publish dev build to PyPI |
| 2 | + |
| 3 | +# Every merge to main publishes a PEP 440 dev release (e.g. 0.3.10.dev7) to |
| 4 | +# PyPI, so the latest main is always installable from the real index with real |
| 5 | +# dependency resolution. Dev releases are pre-releases: pip and uv skip them |
| 6 | +# unless asked, so ordinary installs keep getting the latest stable. |
| 7 | +# |
| 8 | +# Tagged releases (including rc/beta/alpha) are handled by publish-to-pypi.yml. |
| 9 | +on: |
| 10 | + push: |
| 11 | + branches: [main] |
| 12 | + # Ad-hoc runs, e.g. re-publishing after a PyPI outage. |
| 13 | + workflow_dispatch: |
| 14 | + |
| 15 | +# Least privilege by default: jobs get no GITHUB_TOKEN scopes unless they |
| 16 | +# opt in below. |
| 17 | +permissions: {} |
| 18 | + |
| 19 | +# Serialize publishes so versions land in merge order. Queued runs are not |
| 20 | +# cancelled — each merge should still get its own dev build. |
| 21 | +concurrency: |
| 22 | + group: publish-dev-to-pypi |
| 23 | + cancel-in-progress: false |
| 24 | + |
| 25 | +jobs: |
| 26 | + build: |
| 27 | + name: Build dev distribution |
| 28 | + runs-on: ubuntu-latest |
| 29 | + permissions: |
| 30 | + contents: read |
| 31 | + outputs: |
| 32 | + should-publish: ${{ steps.version.outputs.should-publish }} |
| 33 | + |
| 34 | + steps: |
| 35 | + - uses: actions/checkout@v4 |
| 36 | + with: |
| 37 | + # hatch-vcs derives the version from git tags, so we need full history. |
| 38 | + fetch-depth: 0 |
| 39 | + |
| 40 | + - name: Install uv |
| 41 | + # uv provisions its own Python (honoring requires-python), so no |
| 42 | + # separate actions/setup-python step is needed. |
| 43 | + run: | |
| 44 | + curl -LsSf https://astral.sh/uv/install.sh | sh |
| 45 | + echo "$HOME/.local/bin" >> $GITHUB_PATH |
| 46 | +
|
| 47 | + - name: Compute dev version |
| 48 | + id: version |
| 49 | + # Between releases hatch-vcs produces something like |
| 50 | + # "0.3.10.dev16+gb710c2982". The "+g..." local version segment is valid |
| 51 | + # PEP 440 but is rejected by PyPI on upload, so it has to go. We keep |
| 52 | + # setuptools-scm's own next-version guess (0.3.10 — the patch bump |
| 53 | + # after the latest tag) rather than re-deriving it here, and use the |
| 54 | + # run number as the dev counter: it is strictly increasing, so each |
| 55 | + # merge sorts after the last and re-runs never collide. |
| 56 | + run: | |
| 57 | + RAW=$(uvx --from setuptools-scm python -c \ |
| 58 | + "from setuptools_scm import get_version; print(get_version())") |
| 59 | + echo "setuptools-scm reports: $RAW" |
| 60 | +
|
| 61 | + # On a commit that IS a release tag, setuptools-scm returns a clean |
| 62 | + # version with no .dev component (e.g. "0.3.9" or "0.4.0rc1"). |
| 63 | + # Appending ".dev<run>" to that yields 0.3.9.dev42, which sorts BELOW |
| 64 | + # the released 0.3.9 — newer code would look older. A tagged commit |
| 65 | + # is a real release and is published by publish-to-pypi.yml, so there |
| 66 | + # is no dev build to make here. |
| 67 | + case "$RAW" in |
| 68 | + *.dev*) ;; |
| 69 | + *) |
| 70 | + echo "::notice::$RAW is a tagged release; skipping dev build." |
| 71 | + echo "should-publish=false" >> $GITHUB_OUTPUT |
| 72 | + exit 0 |
| 73 | + ;; |
| 74 | + esac |
| 75 | +
|
| 76 | + BASE=${RAW%%+*} |
| 77 | + BASE=${BASE%%.dev*} |
| 78 | + VERSION="${BASE}.dev${GITHUB_RUN_NUMBER}" |
| 79 | + echo "Derived version: $RAW -> $VERSION" |
| 80 | + echo "SETUPTOOLS_SCM_PRETEND_VERSION=$VERSION" >> $GITHUB_ENV |
| 81 | + echo "should-publish=true" >> $GITHUB_OUTPUT |
| 82 | +
|
| 83 | + - name: Build package |
| 84 | + if: steps.version.outputs.should-publish == 'true' |
| 85 | + # hatch-vcs reads SETUPTOOLS_SCM_PRETEND_VERSION in place of the tag. |
| 86 | + run: | |
| 87 | + uv build |
| 88 | + echo "Built distributions:" |
| 89 | + ls -l dist/ |
| 90 | +
|
| 91 | + - name: Store the distribution packages |
| 92 | + if: steps.version.outputs.should-publish == 'true' |
| 93 | + uses: actions/upload-artifact@v4 |
| 94 | + with: |
| 95 | + name: dev-package-distributions |
| 96 | + path: dist/ |
| 97 | + |
| 98 | + publish-dev: |
| 99 | + name: Publish dev build to PyPI |
| 100 | + needs: build |
| 101 | + if: needs.build.outputs.should-publish == 'true' |
| 102 | + runs-on: ubuntu-latest |
| 103 | + environment: |
| 104 | + name: pypi |
| 105 | + url: https://pypi.org/p/codeplain |
| 106 | + permissions: |
| 107 | + # Mint a short-lived OIDC token so PyPI Trusted Publishing can |
| 108 | + # authenticate this run — no long-lived API token needed. |
| 109 | + id-token: write |
| 110 | + |
| 111 | + env: |
| 112 | + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} |
| 113 | + |
| 114 | + steps: |
| 115 | + - name: Download distribution packages |
| 116 | + uses: actions/download-artifact@v4 |
| 117 | + with: |
| 118 | + name: dev-package-distributions |
| 119 | + path: dist/ |
| 120 | + |
| 121 | + - name: Publish to PyPI |
| 122 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 123 | + with: |
| 124 | + # PyPI uploads are immutable, so a re-run of an already-published |
| 125 | + # version should be a no-op, not a red build. |
| 126 | + skip-existing: true |
| 127 | + |
| 128 | + # Success is deliberately quiet: this runs on every merge to main, which |
| 129 | + # is already announced elsewhere. Only failures ping. |
| 130 | + - name: Notify Slack (failure) |
| 131 | + if: ${{ failure() && env.SLACK_WEBHOOK_URL != '' }} |
| 132 | + run: | |
| 133 | + curl -s -X POST "$SLACK_WEBHOOK_URL" \ |
| 134 | + -H 'Content-type: application/json' \ |
| 135 | + --data "{\"text\":\"*Codeplain Client*: Publishing dev build to PyPI FAILED :x: (commit \`${{ github.sha }}\`). See <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|the run log>.\"}" |
0 commit comments