Skip to content

Commit d14bc30

Browse files
committed
tag-and-release: Fix git history guardrails
1 parent 1a38cb8 commit d14bc30

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

.github/workflows/tag-and-release.yml

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ on:
100100
jobs:
101101
tag-and-release:
102102
runs-on: ubuntu-latest
103-
concurrency: tag-and-release # prevent any possible race conditions
103+
concurrency: # prevent any possible race conditions
104+
group: ${{ github.workflow }}:${{ github.repository }}:${{ github.ref }} # branch-specific
105+
cancel-in-progress: true
104106
steps:
105107
- name: Validate inputs
106108
run: |
@@ -134,22 +136,45 @@ jobs:
134136
fi
135137
fi
136138
139+
# CHECKOUT #1 — EXACT EVENT SHA (full git history for validation)
137140
- uses: actions/checkout@v4
138141
with:
139142
fetch-depth: 0 # required to see tags and commits
140-
ref: ${{ github.ref }} # dont lock to sha (action needs to push)
141-
token: ${{ secrets.TOKEN || github.token }}
142-
143+
ref: ${{ github.sha }} # lock to triggered commit (github.ref is dynamic)
144+
- name: Was this triggered by a branch push?
145+
run: |
146+
set -euo pipefail; echo "now: $(date -u +"%Y-%m-%dT%H:%M:%S.%3N")"
147+
if [[ ! "${{ github.ref }}" =~ ^refs/heads/ ]]; then
148+
echo "::error::This workflow only supports branch pushes (got ref='${{ github.ref }}')."
149+
exit 1
150+
else
151+
echo "OK: Running on branch ${{ github.ref_name }}"
152+
exit 0
153+
fi
143154
- name: Is this most recent commit?
144155
run: |
145156
set -euo pipefail; echo "now: $(date -u +"%Y-%m-%dT%H:%M:%S.%3N")"
146157
147-
git fetch &> /dev/null
148-
if [[ $(git status -sb | grep behind) ]]; then
149-
echo "::error::This commit is not most recent on this branch -- rest of workflow will be skipped"
158+
# Fetch just the branch tip from origin
159+
git fetch --no-tags origin "${{ github.ref_name }}" --depth=1
160+
tip_sha="$(git rev-parse "origin/${{ github.ref_name }}")"
161+
162+
# Require the workflow's commit to be the current branch tip
163+
if [[ "${tip_sha}" != "${{ github.sha }}" ]]; then
164+
echo "::error::This run's commit ${{ github.sha }} is not the tip of ${{ github.ref_name }} (tip is ${tip_sha:0:7}). Skipping."
150165
exit 1
166+
else
167+
echo "OK: ${{ github.sha }} is the current tip of ${{ github.ref_name }}."
168+
exit 0
151169
fi
152170
171+
# CHECKOUT #2 — BRANCH REF (for tag/push/release operations)
172+
- uses: actions/checkout@v4
173+
with:
174+
fetch-depth: 0 # required to see tags and commits
175+
ref: ${{ github.ref }} # dont lock to sha (action needs to push)
176+
token: ${{ secrets.TOKEN || github.token }}
177+
153178
# PYTHON ONLY: setup python
154179
- if: inputs.project-type == 'python'
155180
uses: actions/setup-python@v5 # needed for building Python project

0 commit comments

Comments
 (0)