diff --git a/.github/release-merge-back.sh b/.github/release-merge-back.sh new file mode 100755 index 000000000..15d60ae8f --- /dev/null +++ b/.github/release-merge-back.sh @@ -0,0 +1,62 @@ +#!/usr/bin/env bash +# Make a merged release commit an ancestor of `dev`. +# +# Merging a release PR leaves a squash or merge commit on `master` that `dev` +# lacks. Until `dev` contains it, the next release PR is out of date with its +# base and, after a squash, shows conflicts that aren't real. A `-s ours` merge +# fixes the ancestry without touching a file, which is correct only when `dev` +# already has everything the release shipped, so that is checked first: the +# release must have the same tree as the PR head, and `dev` must contain the PR +# head. Anything else means `master` carries changes of its own, which need a +# person to merge them, so the script stops with an error. +# +# Usage: release-merge-back.sh +# Runs from a checkout with full history whose `origin` it can push `dev` to. +# Safe to re-run: it does nothing once `dev` contains the release. +set -euo pipefail + +version="$1" +pr="$2" +release="$3" +head="$4" + +fail() { + echo "::error title=Merge-back to dev failed::$1 Follow the merge-back step in RELEASE.md by hand, or the next release PR will be out of date with master." + exit 1 +} + +git fetch --quiet origin dev master + +git cat-file -e "$release^{commit}" 2>/dev/null || fail "Release commit $release not found." +git cat-file -e "$head^{commit}" 2>/dev/null || fail "PR head $head not found." + +if ! git diff --quiet "$head" "$release"; then + fail "Release $release differs from PR head $head, so master has changes of its own." +fi + +for attempt in 1 2 3 4 5; do + git fetch --quiet origin dev + + if git merge-base --is-ancestor "$release" origin/dev; then + echo "dev already contains $release." + exit 0 + fi + if ! git merge-base --is-ancestor "$head" origin/dev; then + fail "dev does not contain PR head $head." + fi + + git checkout --quiet --detach origin/dev + git merge --quiet --no-ff -s ours "$release" \ + -m "chore: merge master back into dev after the v$version release (#$pr)" + git diff --quiet origin/dev HEAD || fail "The merge changed files on dev." + + # dev may have moved since the fetch; start over from its new tip. + if git push --quiet origin HEAD:refs/heads/dev; then + echo "Merged $release back into dev." + exit 0 + fi + echo "Push to dev failed (attempt $attempt of 5)." + [ "$attempt" = 5 ] || sleep "$((attempt * 5))" +done + +fail "Could not push to dev. Check that the PAT secret is valid and may bypass dev's required checks." diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4787b7b0b..a135ac73f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -6,6 +6,7 @@ name: Release # compile time (make ... RAY_VERSION=X.Y.Z) and publishes a GitHub Release. No # source file is bumped and nothing is pushed to the protected master branch — # only a tag ref and a release, which GITHUB_TOKEN can do without a bypass token. +# The one push is the merge-back job, which updates dev and needs the PAT secret. # Non-release merges to master (title not vX.Y.Z) are ignored. # # IMPORTANT: a DRAFT release does NOT create its git tag yet — the tag is only @@ -179,6 +180,41 @@ jobs: GH_REPO: ${{ github.repository }} run: gh release edit "v${{ needs.prepare.outputs.version }}" --draft=false + # Merge the release commit back into dev, so the next release PR isn't out of + # date with master or full of phantom conflicts after a squash. It needs only + # `prepare`: the commit is on master whether or not the build succeeds. It is + # NOT best-effort: a failure here blocks the next release, so it must show red. + # Manual dispatch has no PR head to verify against, so it skips this job. + merge-back: + needs: prepare + if: github.event_name == 'pull_request' + runs-on: ubuntu-latest + steps: + # The helper runs from the release commit, not from whatever dev holds + # when the job starts: the release is immutable and was reviewed as part + # of the release PR, and dev is the branch the helper is about to write. + # GITHUB_TOKEN can't push past dev's required checks; the PAT's owner can. + - uses: actions/checkout@v5 + with: + ref: ${{ github.event.pull_request.merge_commit_sha }} + fetch-depth: 0 + token: ${{ secrets.PAT }} + + - name: Merge the release into dev + env: + VERSION: ${{ needs.prepare.outputs.version }} + PR: ${{ github.event.pull_request.number }} + RELEASE_SHA: ${{ github.event.pull_request.merge_commit_sha }} + HEAD_SHA: ${{ github.event.pull_request.head.sha }} + run: | + set -euo pipefail + git config user.name "rayforce-release" + git config user.email "release@rayforcedb.com" + # The helper switches the working tree to dev, so run a copy that + # the switch can't replace while bash is still reading it. + cp .github/release-merge-back.sh "$RUNNER_TEMP/merge-back.sh" + bash "$RUNNER_TEMP/merge-back.sh" "$VERSION" "$PR" "$RELEASE_SHA" "$HEAD_SHA" + # Post the release announcement to Zulip (#Announcements > Rayforce) once the # release is public. Best-effort: skipped when ZULIP_API_KEY isn't configured, # and continue-on-error so a Zulip hiccup never reds an already-shipped release. diff --git a/RELEASE.md b/RELEASE.md index 22f526f54..596cb316f 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -28,27 +28,45 @@ truth for the version** — no version literal is ever hand-edited in source. at compile time (`make dist RAY_VERSION=X.Y.Z`), - packages `rayforce-X.Y.Z--.tar.gz` + a `.sha256` checksum, - publishes a GitHub Release with a feature-oriented changelog and the - artifacts. -5. **Merge `master` back into `dev`** right after the release PR merges: - - ```sh - git fetch origin - git switch dev && git pull --ff-only - git merge --no-ff -s ours origin/master \ - -m "chore: merge master back into dev after the vX.Y.Z release (#N)" - git diff origin/dev --stat # must print nothing - git push origin dev - ``` - - Merging the release PR leaves a squash or merge commit on `master` that `dev` - doesn't contain. `master` requires branches to be up to date before merging, - so without this step the next release PR is blocked as "out of date with the - base". After a squash, it also shows conflicts that aren't real. Copying the - release commit onto `dev` doesn't help: only a merge makes `master`'s commit - an ancestor of `dev`. `-s ours` is correct because `dev` already contains - everything the release shipped, so the empty `git diff` proves the merge - changed no files. If `master` ever carries a hotfix that isn't on `dev`, - drop `-s ours` and do a normal merge instead. + artifacts, + - merges the release commit back into `dev`, without changing any files. + +### Merge-back into `dev` + +Merging the release PR leaves a squash or merge commit on `master` that `dev` +doesn't contain. `master` requires branches to be up to date before merging, so +without a merge-back the next release PR is blocked as "out of date with the +base". After a squash, it also shows conflicts that aren't real. Copying the +release commit onto `dev` doesn't help: only a merge makes `master`'s commit an +ancestor of `dev`. It also has to happen *after* the release PR merges, because +the commit doesn't exist before then. + +The Release workflow's **merge-back** job does this with +`.github/release-merge-back.sh`, run from the release commit and pushing with the +`PAT` secret. A manual dispatch skips the job. If the job failed only because it +could not push, or was skipped, run the same helper by hand with the release PR's +own SHAs, so it applies the same checks: + +```sh +pr=N # the release PR +ver=X.Y.Z +release=$(gh pr view "$pr" --json mergeCommit -q .mergeCommit.oid) +head=$(gh pr view "$pr" --json headRefOid -q .headRefOid) +git fetch origin +git worktree add --detach /tmp/merge-back "$release" +cp /tmp/merge-back/.github/release-merge-back.sh /tmp/merge-back.sh +(cd /tmp/merge-back && bash /tmp/merge-back.sh "$ver" "$pr" "$release" "$head") +git worktree remove /tmp/merge-back +``` + +The helper does a `-s ours` merge, which is correct only when `dev` already +contains everything the release shipped. It checks that first: the release +commit must have the same tree as the PR head, and `dev` must contain the PR +head. If either check fails (the error annotation says which), `master` carries +changes of its own, such as a hotfix, and a `-s ours` merge would silently drop +them. Do not work around it: merge `master` into `dev` normally through a PR, +review the diff, and land it with a merge commit, since a squash loses the +ancestry. That's the whole ritual. **Never edit the version in source to make a release** — the tag is authoritative.