Skip to content

Commit 399cc2b

Browse files
ci(release): gate RubyGems publishing behind an environment and verify publish_tag (#132)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent de153e0 commit 399cc2b

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

‎.github/workflows/release.yml‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ jobs:
210210
(inputs.publish_tag != '' || needs.release.outputs.release_created == 'true')
211211
runs-on: { group: github-public-amd64 }
212212
timeout-minutes: 15
213+
# The RubyGems Trusted Publishing token is minted in this job. The environment is where its
214+
# reviewers and deployment branches are enforced, and the trusted publisher on rubygems.org
215+
# can pin it, so a workflow on any other branch cannot mint a usable token.
216+
environment: rubygems
213217
permissions:
214218
contents: read
215219
id-token: write
@@ -226,11 +230,41 @@ jobs:
226230
227231
- uses: actions/checkout@v4
228232
with:
229-
ref: ${{ steps.target.outputs.tag }}
233+
# refs/tags/ so that a publish_tag naming a branch or a SHA is not checked out.
234+
ref: refs/tags/${{ steps.target.outputs.tag }}
235+
# Full history, for the ancestry check below.
236+
fetch-depth: 0
230237
# gem build runs the gemspec, which is project code, and nothing here writes
231238
# to the repository, so do not leave GITHUB_TOKEN in .git/config.
232239
persist-credentials: false
233240

241+
# publish_tag is free text, so without this any tag, including one pushed on a commit
242+
# that was never reviewed, could be published. Only publish a tag whose commit is
243+
# already on the release branch this run belongs to.
244+
- name: Verify the tag is on this release branch
245+
env:
246+
TAG: ${{ steps.target.outputs.tag }}
247+
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
248+
run: |
249+
# The environment's deployment branches are what enforce this; the check here only
250+
# gives a clear error before they are configured.
251+
if [ "$GITHUB_REF" != "refs/heads/${DEFAULT_BRANCH}" ] && [[ ! "$GITHUB_REF" =~ ^refs/heads/[0-9]+(\.[0-9]+)*\.x$ ]]; then
252+
echo "::error::Publishing runs only from ${DEFAULT_BRANCH} or an N.x release branch, not ${GITHUB_REF}."
253+
exit 1
254+
fi
255+
# Exactly a tag. Revision syntax such as v1.2.3~1 would otherwise resolve to a commit
256+
# that was never tagged.
257+
if ! git show-ref --verify --quiet "refs/tags/${TAG}"; then
258+
echo "::error::${TAG} is not a tag of this repository."
259+
exit 1
260+
fi
261+
commit="$(git rev-parse "refs/tags/${TAG}^{commit}")"
262+
if ! git merge-base --is-ancestor "$commit" "$GITHUB_SHA"; then
263+
echo "::error::Tag ${TAG} points at ${commit}, which is not on ${GITHUB_REF_NAME}. Dispatch the workflow from the branch that release was cut from."
264+
exit 1
265+
fi
266+
echo "Tag ${TAG} (${commit}) is on ${GITHUB_REF_NAME}."
267+
234268
- name: Set up Ruby
235269
uses: ruby/setup-ruby@v1
236270
with:

0 commit comments

Comments
 (0)