What the run takes#
GitHub's event table gives workflow_dispatch a GITHUB_SHA of "Last commit on the GITHUB_REF branch or tag", where the ref is the "Branch or tag that received dispatch". Events that trigger workflows
So gh workflow run deploy.yml --ref main names a branch, not a commit. GitHub resolves the branch when it creates the run, and actions/checkout then fetches that commit. Nothing in the form shows which commit it will be. The same holds when the trigger is a chat bot or a script that calls the dispatch API with a branch name.
Read which commit a past deploy shipped#
The run records the commit it resolved:
gh api repos/OWNER/REPO/actions/runs/RUN_ID \
--jq '.event, .head_branch, .head_sha, .created_at'
Compare head_sha with the commit in the approval, the change ticket or the release note. If they differ, list what the deploy added:
git log --oneline REVIEWED_SHA..HEAD_SHA
An empty list with different IDs means the reviewed commit is not an ancestor of the shipped one. Check git log --oneline HEAD_SHA..REVIEWED_SHA too: a force-push may have replaced the reviewed commit.
Pin the commit in the workflow#
Take the commit as a required input, check it out, and confirm it belongs to the branch and passed its checks before anything deploys:
on:
workflow_dispatch:
inputs:
sha:
description: Full commit SHA to deploy
required: true
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
with:
ref: ${{ inputs.sha }}
fetch-depth: 0
- name: Commit is on the dispatched branch
run: git merge-base --is-ancestor "$SHA" "origin/$GITHUB_REF_NAME"
env:
SHA: ${{ inputs.sha }}
- name: Required check passed on this commit
run: |
gh api "repos/$GITHUB_REPOSITORY/commits/$SHA/check-runs?check_name=test" \
--jq '[.check_runs[] | select(.conclusion == "success")] | length' \
| grep -qx '[1-9][0-9]*'
env:
SHA: ${{ inputs.sha }}
GH_TOKEN: ${{ github.token }}
The input goes through env, not straight into the script, so a crafted value cannot inject shell. Replace test with your required check's name. actions/checkout documents ref as "The branch, tag or SHA to checkout". actions/checkout Pass the full 40-character SHA: an abbreviated one can become ambiguous as the repository grows.
The check-run lookup proves that a check of that name passed on that commit. It does not prove which workflow reported it. If another app could post the same name, pin the source as a required check pinned to its app describes.
Decide#
head_sha vs reviewed commit | What shipped |
|---|---|
| Equal | What you reviewed |
| Reviewed is an ancestor | Your change plus the listed later commits, none of which you approved for this deploy |
| Not an ancestor | A branch rewritten after the review. Find where the reviewed commit went before you deploy again |
Worked example#
This example is fictional. Ana reviews release-2.4 at 8c1f0e2 and posts "ship it" at 14:02 UTC. At 14:05 Ben merges a config change into release-2.4. At 14:09 Chen presses Run workflow on release-2.4. The run's head_sha is d47a913, and git log 8c1f0e2..d47a913 lists Ben's commit. Its tests passed, so nothing went red. Ben's change had no review for this release. With the sha input, Chen would have pasted 8c1f0e2 and shipped what Ana approved, and Ben's change would wait for its own deploy.
A deploy that downloads a build has the same gap one step later: see an artifact from another run and promoting the tested artifact.