From 7fe81e7d98f938abe18f076c1866c230a974f308 Mon Sep 17 00:00:00 2001 From: Randy Westergren Date: Wed, 25 Nov 2026 10:07:59 -0500 Subject: [PATCH] Add one-click release automation Calculate release versions and require successful CI for the selected main commit. Dispatch publishing workflows explicitly so releases can use GITHUB_TOKEN without a PAT or GitHub App. --- .github/workflows/docker.yml | 9 +++ .github/workflows/publish.yml | 11 +++ .github/workflows/release.yml | 132 ++++++++++++++++++++++++++++++++++ README.md | 25 +++++++ 4 files changed, 177 insertions(+) create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 9f55e0c..52a64ca 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -5,6 +5,7 @@ on: branches: [main] release: types: [published] + workflow_dispatch: concurrency: group: docker-${{ github.ref }} @@ -21,6 +22,14 @@ jobs: attestations: write steps: + - name: Require a release tag for manual dispatch + if: github.event_name == 'workflow_dispatch' + run: | + if [[ ! "$GITHUB_REF" =~ ^refs/tags/v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$ ]]; then + echo "::error::Dispatch publishing at a vX.Y.Z tag, not a branch." + exit 1 + fi + - uses: actions/checkout@v4 - name: Set up QEMU diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 327d077..940fc02 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -3,6 +3,7 @@ name: Publish to PyPI on: release: types: [published] + workflow_dispatch: jobs: publish: @@ -14,6 +15,14 @@ jobs: contents: read steps: + - name: Require a release tag for manual dispatch + if: github.event_name == 'workflow_dispatch' + run: | + if [[ ! "$GITHUB_REF" =~ ^refs/tags/v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$ ]]; then + echo "::error::Dispatch publishing at a vX.Y.Z tag, not a branch." + exit 1 + fi + - uses: actions/checkout@v4 - name: Install uv @@ -33,6 +42,8 @@ jobs: - name: Publish to PyPI uses: pypa/gh-action-pypi-publish@release/v1 + with: + skip-existing: true - name: Install mcp-publisher run: | diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..fd22da6 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,132 @@ +name: Release + +on: + workflow_dispatch: + inputs: + bump: + description: Version bump + type: choice + required: true + default: patch + options: [patch, minor, major] + +permissions: + contents: read + +concurrency: + group: release + cancel-in-progress: false + +jobs: + release: + runs-on: ubuntu-latest + permissions: + contents: write + actions: read + outputs: + tag: ${{ steps.version.outputs.tag }} + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GH_REPO: ${{ github.repository }} + + steps: + - name: Require main branch + if: github.ref != 'refs/heads/main' + run: | + echo "::error::Releases must run from the main branch." + exit 1 + + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + persist-credentials: false + + - name: Require successful CI for this commit + run: | + ci=$(gh run list --workflow ci.yml --branch main --commit "$GITHUB_SHA" \ + --event push --limit 1 --json status,conclusion,url --jq '.[0] // {}') + if [[ "$(jq -r '.status' <<< "$ci")" != completed || \ + "$(jq -r '.conclusion' <<< "$ci")" != success ]]; then + echo "::error::CI must pass for $GITHUB_SHA before releasing. $(jq -r '.url // empty' <<< "$ci")" + exit 1 + fi + + - name: Compute next version + id: version + env: + BUMP: ${{ inputs.bump }} + run: | + python3 - <<'PY' + import os + import re + import subprocess + + def git(*args): + return subprocess.check_output(["git", *args], text=True).strip() + + pattern = r"v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)" + versions = [ + tuple(map(int, match.groups())) + for tag in git("tag", "--list").splitlines() + if (match := re.fullmatch(pattern, tag)) + ] + major, minor, patch = max(versions, default=(0, 0, 0)) + latest = f"v{major}.{minor}.{patch}" if versions else "" + if latest: + sha = os.environ["GITHUB_SHA"] + if git("rev-parse", f"{latest}^{{commit}}") == sha: + raise SystemExit("::error::This commit is already released. Retry the failed publishing run instead.") + if subprocess.run(["git", "merge-base", "--is-ancestor", latest, sha]).returncode: + raise SystemExit("::error::The release commit must include the latest stable tag.") + + match os.environ["BUMP"]: + case "major": + version = (major + 1, 0, 0) + case "minor": + version = (major, minor + 1, 0) + case "patch": + version = (major, minor, patch + 1) + case _: + raise SystemExit("::error::Expected a patch, minor, or major bump.") + + tag = "v" + ".".join(map(str, version)) + with open(os.environ["GITHUB_OUTPUT"], "a") as output: + output.write(f"tag={tag}\nlatest={latest}\n") + print(f"Next release: {tag}") + PY + + - name: Create GitHub Release + env: + TAG: ${{ steps.version.outputs.tag }} + LATEST: ${{ steps.version.outputs.latest }} + run: | + args=(--target "$GITHUB_SHA" --title "$TAG" --generate-notes) + if [[ -n "$LATEST" ]]; then + args+=(--notes-start-tag "$LATEST") + fi + gh release create "$TAG" "${args[@]}" + echo "Created [$TAG]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/releases/tag/$TAG) at \`$GITHUB_SHA\`." >> "$GITHUB_STEP_SUMMARY" + + dispatch: + name: Dispatch ${{ matrix.workflow }} + needs: release + runs-on: ubuntu-latest + permissions: + contents: read + actions: write + strategy: + fail-fast: false + matrix: + workflow: [publish.yml, docker.yml] + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GH_REPO: ${{ github.repository }} + TAG: ${{ needs.release.outputs.tag }} + WORKFLOW: ${{ matrix.workflow }} + + steps: + # GITHUB_TOKEN-created releases do not trigger release-event workflows. + - name: Start publishing at the release tag + run: | + gh workflow run "$WORKFLOW" --ref "$TAG" + echo "Dispatched [$WORKFLOW]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/workflows/$WORKFLOW?query=branch%3A$TAG) at \`$TAG\`. Check that workflow's run for publishing completion." >> "$GITHUB_STEP_SUMMARY" diff --git a/README.md b/README.md index fabfdfe..5cf5729 100644 --- a/README.md +++ b/README.md @@ -224,6 +224,31 @@ uv run cronometer-api-mcp The CLI auto-loads `.env` on startup (dev convenience only). Real environment variables always win over `.env`, so production deployments and MCP client `env` blocks are unaffected. +### Releases + +Open **Actions → Release → Run workflow**, select **main**, and choose **patch** +(default), **minor**, or **major**. CI must have passed for the selected commit; +if it is still running, wait for it to finish before starting the release. + +The workflow increments the highest stable `vX.Y.Z` tag, tags the selected commit, +and creates a GitHub Release with generated notes. Prerelease tags are ignored. +For example, `v0.2.7` becomes `v0.2.8` for a patch or `v0.3.0` for a minor release. +Release runs are serialized, and an already-released commit cannot be released again. + +The release workflow then dispatches **Publish to PyPI** (including the MCP Registry) +and **Build Docker image** at the new tag. Check their separate runs for publishing +completion; a successful Release run only confirms they were dispatched. Version +fields in `pyproject.toml` and `server.json` are set from the tag during publishing. + +This uses the built-in `GITHUB_TOKEN` and existing OIDC publishing configuration; +no PAT or GitHub App is needed. Explicit dispatch is necessary because releases +created with `GITHUB_TOKEN` do not trigger `release: published` workflows. + +If dispatch or publishing fails, rerun the failed jobs in that workflow run rather +than starting a new release. Existing PyPI uploads are skipped on retry so an MCP +Registry failure can be retried. Manually published GitHub Releases also continue +to trigger publishing. + ## How It Works This server communicates with `mobile.cronometer.com` -- the same REST API used by the Cronometer Android/Flutter app. The API was reverse-engineered through: