From 69a660fa4c7a926c56b324703fb1fd4b7b573910 Mon Sep 17 00:00:00 2001 From: Mert Bekci Date: Thu, 19 Nov 2026 12:24:24 +0300 Subject: [PATCH] ci: tag releases and create GitHub releases automatically Signed-off-by: Mert Bekci --- .github/workflows/publish.yml | 66 ++++++++++++++++++++++ .github/workflows/tag.yml | 103 ++++++++++++++++++++++++++++++++++ CONTRIBUTING.md | 56 ++++++++---------- 3 files changed, 192 insertions(+), 33 deletions(-) create mode 100644 .github/workflows/tag.yml diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 6cbdbaa..6f7e734 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -69,6 +69,27 @@ jobs: with: name: dist path: dist/ + # Notes for the GitHub release: an install line and the version's + # CHANGELOG section without its heading (a candidate gets none). + - name: Write the release notes + if: steps.check.outputs.channel == 'live' + run: | + set -euo pipefail + v="${GITHUB_REF_NAME#v}" + mkdir -p "$RUNNER_TEMP/release-notes" + # shellcheck disable=SC2016 # the backticks are Markdown, not a substitution + { printf '```sh\npip install sdo-client==%s\n```\n\n' "$v" + awk -v h="## [$v]" 'index($0, h) == 1 {on=1; next} on && /^(## |\[Unreleased\]:)/ {exit} on' CHANGELOG.md + } > "$RUNNER_TEMP/release-notes/notes.md" + if [ "$(grep -c . "$RUNNER_TEMP/release-notes/notes.md")" -le 3 ]; then + echo "::error::CHANGELOG.md has no '## [$v]' section for the release notes" + exit 1 + fi + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + if: steps.check.outputs.channel == 'live' + with: + name: release-notes + path: ${{ runner.temp }}/release-notes/notes.md publish-test: needs: build @@ -184,3 +205,48 @@ jobs: PY rm -f mint.json [ "$code" = "200" ] + + # Once a final release installs from PyPI, create the GitHub release for its + # tag with the notes the build job wrote. Release candidates get none. On the + # org's self-hosted runners, because the org's IP allow list rejects GitHub + # API calls from GitHub-hosted runners; it checks out and runs no repository + # code. + github-release: + needs: [build, smoke] + # always(): smoke's own needs include a skipped publish job. + if: ${{ always() && needs.build.outputs.channel == 'live' && needs.smoke.result == 'success' }} + runs-on: self-hosted + permissions: + contents: write # create the GitHub release + steps: + # A persistent runner keeps its temp directory between runs. + - name: Start without old notes + run: rm -rf "$RUNNER_TEMP/release-notes" + # The self-hosted runners do not ship the GitHub CLI: install a pinned, + # checksum-verified release. + - name: Install gh + env: + GH_VERSION: 2.102.0 + GH_SHA256: bb766f710eef8ede859c18578c72c327597cd4c8a85b06001b1f3843c6019386 + run: | + set -euo pipefail + curl -sSfL --retry 5 --retry-delay 10 --retry-all-errors -o "$RUNNER_TEMP/gh.tar.gz" \ + "https://github.com/cli/cli/releases/download/v${GH_VERSION}/gh_${GH_VERSION}_linux_amd64.tar.gz" + echo "${GH_SHA256} $RUNNER_TEMP/gh.tar.gz" | sha256sum -c - + tar -xzf "$RUNNER_TEMP/gh.tar.gz" -C "$RUNNER_TEMP" + echo "$RUNNER_TEMP/gh_${GH_VERSION}_linux_amd64/bin" >> "$GITHUB_PATH" + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: release-notes + path: ${{ runner.temp }}/release-notes + - name: Create the GitHub release + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + if gh release view "$GITHUB_REF_NAME" --repo "$GITHUB_REPOSITORY" >/dev/null 2>&1; then + echo "::notice::the GitHub release $GITHUB_REF_NAME already exists." + exit 0 + fi + gh release create "$GITHUB_REF_NAME" --repo "$GITHUB_REPOSITORY" --verify-tag \ + --title "$GITHUB_REF_NAME" --notes-file "$RUNNER_TEMP/release-notes/notes.md" diff --git a/.github/workflows/tag.yml b/.github/workflows/tag.yml new file mode 100644 index 0000000..ed8c3cf --- /dev/null +++ b/.github/workflows/tag.yml @@ -0,0 +1,103 @@ +# SPDX-License-Identifier: Apache-2.0 +# Tags a release once its release PR is on main, so nobody pushes tags by hand. +# +# On every push to main that touches the version or the changelog, `plan` +# reads src/sdo/_version.py and picks the tag: +# X.Y.Z -> vX.Y.Z only when CHANGELOG.md has a `## [X.Y.Z] - ` section +# X.Y.ZrcN -> vX.Y.Z-rc.N +# If that tag does not exist yet, `tag` creates it on the pushed commit. The +# tag starts publish.yml, which still waits for approval in the `release` (or +# `prerelease`) environment before anything is uploaded. +# +# The tag is created with a token of the scrape-do-publisher GitHub App, +# limited to contents: write on this repository and revoked when the job ends: +# a tag created with GITHUB_TOKEN would not start publish.yml, and the "release +# tags" ruleset lets only admins and this App create `v*` tags. `tag` runs on +# the org's self-hosted runners, because the org's IP allow list rejects GitHub +# API calls from GitHub-hosted runners; it checks out and runs no repository +# code. +name: tag + +on: + push: + branches: [main] + paths: + - src/sdo/_version.py + - CHANGELOG.md + # Manual run: reports what a push would do and checks that the App token can + # be minted, without creating a tag. + workflow_dispatch: + +permissions: {} + +concurrency: + group: tag + cancel-in-progress: false + +jobs: + plan: + if: github.repository_owner == 'scrape-do' + runs-on: ubuntu-latest + permissions: + contents: read + outputs: + tag: ${{ steps.plan.outputs.tag }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - name: Work out the tag for this version + id: plan + run: | + set -euo pipefail + version="$(sed -n 's/^__version__ = "\(.*\)"$/\1/p' src/sdo/_version.py)" + if [[ "$version" =~ ^([0-9]+\.[0-9]+\.[0-9]+)rc([0-9]+)$ ]]; then + tag="v${BASH_REMATCH[1]}-rc.${BASH_REMATCH[2]}" + elif [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + tag="v${version}" + if ! grep -q "^## \[${version}\] - " CHANGELOG.md; then + echo "::notice::CHANGELOG.md has no '## [${version}] - ' section yet; not tagging ${tag}." + exit 0 + fi + else + echo "::error::src/sdo/_version.py has '${version}', which is neither X.Y.Z nor X.Y.ZrcN" + exit 1 + fi + echo "tag=${tag}" >> "$GITHUB_OUTPUT" + + tag: + needs: plan + if: needs.plan.outputs.tag != '' + runs-on: self-hosted + permissions: {} # writes with the App's token, never with GITHUB_TOKEN + steps: + - uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 + id: app-token + with: + app-id: ${{ secrets.PUBLISHER_APP_ID }} + private-key: ${{ secrets.PUBLISHER_APP_PRIVATE_KEY }} + owner: scrape-do + repositories: sdo-sdk-python + permission-contents: write + - name: Create the tag unless it exists + env: + GH_TOKEN: ${{ steps.app-token.outputs.token }} + TAG: ${{ needs.plan.outputs.tag }} + run: | + set -euo pipefail + api="https://api.github.com/repos/${GITHUB_REPOSITORY}/git" + auth=(-H "Authorization: Bearer ${GH_TOKEN}" -H "Accept: application/vnd.github+json") + code="$(curl -sS -o /dev/null -w '%{http_code}' "${auth[@]}" "${api}/ref/tags/${TAG}")" + case "$code" in + 200) echo "::notice::${TAG} already exists; nothing to tag."; exit 0 ;; + 404) ;; + *) echo "::error::looking up ${TAG} returned HTTP ${code}"; exit 1 ;; + esac + if [ "$GITHUB_EVENT_NAME" = workflow_dispatch ]; then + echo "::notice::a push would tag ${GITHUB_SHA} as ${TAG} (manual run: not tagging)." + exit 0 + fi + # TAG matched a strict version pattern in `plan`, so it is safe inside JSON. + curl -sSf -o /dev/null "${auth[@]}" -X POST "${api}/refs" \ + -d "{\"ref\":\"refs/tags/${TAG}\",\"sha\":\"${GITHUB_SHA}\"}" + echo "::notice::tagged ${GITHUB_SHA} as ${TAG}; publish.yml now waits for approval." diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 46c7e48..83d72da 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -212,19 +212,21 @@ This repository is public. Practically, for you as a contributor, this means: ke | `ci.yml` | every push to `main` and every pull request | `ruff` lint and format check; `mypy --strict`; the test suite with the 99% line + branch coverage floor on Python 3.11–3.14 (Linux), plus macOS and Windows on 3.13, including the cross-language conformance suite and its integrity check; the public-API snapshot (`api-surface.txt`) must be up to date; CodeQL code scanning. | | `commits.yml` | every pull request: opened, updated, reopened, or its title or description edited | the `commits` job checks every commit's subject and sign-off and the pull request title, which becomes the squash commit's subject. A workflow of its own, so editing a pull request re-runs only this check. | | `security.yml` | push to `main`, pull requests, and weekly | secret scanning over the full git history (gitleaks), OSV-Scanner over the pinned requirement files, `pip-audit`, and an SBOM uploaded as a build artifact. OpenSSF Scorecard runs on pushes to `main` and weekly, with results in code scanning. | -| `publish.yml` | a version tag, or a manual run | A tag `vX.Y.Z-rc.N` publishes a release candidate to **TestPyPI**; a tag `vX.Y.Z` publishes to **PyPI**. The tag must be on `main` and match `src/sdo/_version.py` (`X.Y.ZrcN` for a candidate). The package is built once with hash-pinned tooling, checked with `twine check`, published by trusted publishing (no stored token) after approval in the `prerelease` or `release` environment, and then installed from the index as a smoke test. A manual run (Actions → publish → Run workflow) uploads nothing: its `publisher-check` job only confirms that PyPI accepts this workflow's OIDC token, after approval in the `release` environment. | +| `publish.yml` | a version tag, or a manual run | A tag `vX.Y.Z-rc.N` publishes a release candidate to **TestPyPI**; a tag `vX.Y.Z` publishes to **PyPI**. The tag must be on `main` and match `src/sdo/_version.py` (`X.Y.ZrcN` for a candidate). The package is built once with hash-pinned tooling, checked with `twine check`, published by trusted publishing (no stored token) after approval in the `prerelease` or `release` environment, and then installed from the index as a smoke test; for a final release, the GitHub release is then created with the version's CHANGELOG section as notes. A manual run (Actions → publish → Run workflow) uploads nothing: its `publisher-check` job only confirms that PyPI accepts this workflow's OIDC token, after approval in the `release` environment. | +| `tag.yml` | push to `main` touching `src/sdo/_version.py` or `CHANGELOG.md`, or a manual run | Creates the release tag when it does not exist yet: `vX.Y.Z` once `CHANGELOG.md` has a `## [X.Y.Z] - ` section, `vX.Y.Z-rc.N` for `X.Y.ZrcN`. The tag is created with a token of the `scrape-do-publisher` GitHub App, so it starts `publish.yml`. A manual run creates nothing: it reports the tag a push would create and checks that the App token can be minted. | | Dependabot | weekly | update pull requests for the GitHub Actions, taken only after a 7-day cooldown, and security-advisory pull requests for the pinned Python requirements, which the cooldown does not delay (version bumps of those are recompiled by hand with uv, see [Dev setup](#dev-setup)); each one runs the checks above. | -Every job runs on GitHub-hosted runners except CodeQL and Scorecard, which run on the organisation's self-hosted runners: their uploads are GitHub API calls that the organisation's IP allow list only accepts from there. CodeQL runs there for same-repository events only, so a pull request from a fork is analysed once merged; Scorecard never runs on a pull request. Every action is pinned to a full commit SHA and every job declares least-privilege `permissions:`. Publishing never happens outside the `scrape-do` organisation, never from a commit that is not on `main`, and never without a maintainer's approval. +Every job runs on GitHub-hosted runners except CodeQL, Scorecard, the `tag` job and the GitHub release job, which run on the organisation's self-hosted runners: their uploads and writes are GitHub API calls that the organisation's IP allow list only accepts from there. CodeQL runs there for same-repository events only, so a pull request from a fork is analysed once merged; Scorecard never runs on a pull request. Every action is pinned to a full commit SHA and every job declares least-privilege `permissions:`. Publishing never happens outside the `scrape-do` organisation, never from a commit that is not on `main`, and never without a maintainer's approval. --- ## Releasing Development is trunk-based: every change lands on `main` through a pull request -with CI green. A release is a tag on `main`; `.github/workflows/publish.yml` -checks it, builds once with the hash-pinned toolchain and publishes after -approval: +with CI green. A release is a tag on `main`. `.github/workflows/tag.yml` +creates it when the release pull request lands, and +`.github/workflows/publish.yml` checks it, builds once with the hash-pinned +toolchain and publishes after approval: | Tag | `src/sdo/_version.py` | Channel | Environment | |-----|-----------------------|---------|-------------| @@ -291,40 +293,28 @@ and `job_workflow_ref` with the table above. [X.Y.Z]: https://github.com/scrape-do/sdo-sdk-python/releases/tag/vX.Y.Z ``` -2. Squash-merge it once CI is green, then tag the merge commit on `main` with a - signed tag and push it: - - ```sh - git switch main && git pull --ff-only - git tag -s vX.Y.Z -m vX.Y.Z - git push origin vX.Y.Z - ``` - - The organization's tag ruleset restricts creating `v*` tags, so only a - maintainer allowed to bypass it can push one; the push reports the bypass. - -3. `publish.yml` builds, then waits for approval in `release` (`prerelease` for - a candidate). Approve it under the run's **Review deployments**. -4. The `smoke` job installs the version from the index and imports it. Check it - yourself in a clean environment too, with Python 3.11 or newer (an older - interpreter finds no installable version, `from versions: none`): +2. Squash-merge it once CI is green. `tag.yml` then creates `vX.Y.Z` on the + merge commit (it skips a final version without its CHANGELOG section, and a + version whose tag exists). Nobody pushes a tag by hand; the "release tags" + ruleset lets only admins and the `scrape-do-publisher` App create `v*` + tags, and the App needs to stay installed on this repository with the org + secrets `PUBLISHER_APP_ID` and `PUBLISHER_APP_PRIVATE_KEY` shared with it. +3. The tag starts `publish.yml`, which builds and then waits for approval in + `release` (`prerelease` for a candidate). Approve it under the run's + **Review deployments**. +4. The `smoke` job installs the version from the index and imports it, and for + a final release `github-release` creates the GitHub release with the + version's CHANGELOG section as notes. To check the install yourself, use + Python 3.11 or newer (an older interpreter finds no installable version, + `from versions: none`): ```sh python3.12 -m venv /tmp/sdo-check && /tmp/sdo-check/bin/pip install sdo-client==X.Y.Z /tmp/sdo-check/bin/python -c "import sdo; print(sdo.__version__)" ``` -5. Create the GitHub release for the tag. The notes are an install line and - the version's CHANGELOG section, without its heading: - - ```sh - v=X.Y.Z - { printf '```sh\npip install sdo-client==%s\n```\n\n' "$v" - awk -v h="## [$v]" 'index($0, h) == 1 {on=1; next} on && /^(## |\[Unreleased\]:)/ {exit} on' CHANGELOG.md - } > notes.md - gh release create vX.Y.Z --repo scrape-do/sdo-sdk-python --verify-tag \ - --title vX.Y.Z --notes-file notes.md - ``` +If `tag.yml` cannot run, an admin can still tag by hand on `main` +(`git tag -s vX.Y.Z -m vX.Y.Z && git push origin vX.Y.Z`); the rest is the same. A version on PyPI can never be uploaded again, even after it is deleted, so a broken release is fixed with a new patch version (and the broken one yanked).