Sub topic 4
Finding and verifying the SHA for a release
This chapter assumes you can read a GitHub Actions workflow and know what a uses: line does. It also assumes you know the difference between a commit, a tag, and a branch, and that you have git installed. The gh examples need the GitHub CLI, signed in with gh auth login.
Pinning an action to a commit SHA only helps if the SHA is the right one. A pin to the wrong commit is worse than a tag, because it looks safe and nobody questions it. This chapter shows how to turn a release tag into a full commit SHA and how to check that the SHA belongs to the repository you meant.
Why a tag name is not a SHA
A Git tag is a name that points at an object. There are two kinds, and they behave differently when you look them up.
- A lightweight tag points straight at a commit.
- An annotated tag is its own object, with a message, an author, and a date. That object points at a commit.
If you copy the SHA of an annotated tag, you get the SHA of the tag object, not of the commit. The uses: key needs the commit SHA. The tag object's SHA is a valid Git object, but a workflow that references it fails to resolve. You must follow the tag to the commit it points at. Git calls this peeling the tag.
| Tag kind | `refs/tags/v4.2.2` points to | `refs/tags/v4.2.2^{}` returns | SHA to use in `uses:` |
|---|---|---|---|
| Lightweight | The commit | Nothing, because there is nothing to peel | The only SHA returned |
| Annotated | The tag object | The commit | The SHA from the `^{}` line |
Choose the exact release, not the moving tag
Many actions publish a major-version tag such as v4, and the maintainer moves it with each release. It points at whatever the latest 4.x release is. If you resolve v4 today and v4 next month, you may get different commits.
Resolve an exact release tag such as v4.2.2 instead. It names one release, and you record it in a comment next to the SHA. The next chapter covers those comments in detail. Most maintainers do not move exact tags, but they can, and that is the reason you pin by SHA.
Some repositories use GitHub's immutable releases feature, which prevents a published release's tag from being moved or deleted. This helps, but you can't assume it. Pinning works the same way regardless of how the upstream repository is configured.
Method 1: resolve with `git ls-remote`
git ls-remote asks a remote repository for its refs without cloning it. Ask for the tag and for its peeled form together:
git ls-remote https://github.com/actions/checkout 'refs/tags/v4.2.2' 'refs/tags/v4.2.2^{}'The quotes stop your shell from interpreting the braces. For an annotated tag the output has two lines:
<tag-object-sha> refs/tags/v4.2.2
11bd71901bbe5b1630ceea73d27597364c9af683 refs/tags/v4.2.2^{}Use the SHA on the ^{} line. For a lightweight tag you get one line, and that SHA is already the commit.
You can wrap this in a small script that picks the right line. It prefers the peeled SHA and falls back to the plain one:
#!/usr/bin/env bash
# usage: resolve-action.sh owner/repo tag
set -euo pipefail
repo="$1"
tag="$2"
out=$(git ls-remote "https://github.com/${repo}" \
"refs/tags/${tag}" "refs/tags/${tag}^{}")
if [ -z "$out" ]; then
echo "No tag '${tag}' found in ${repo}" >&2
exit 1
fi
sha=$(printf '%s\n' "$out" | awk '
/\^\{\}$/ { peeled = $1 }
!/\^\{\}$/ { direct = $1 }
END { print (peeled != "" ? peeled : direct) }')
echo "uses: ${repo}@${sha} # ${tag}"Running ./resolve-action.sh actions/checkout v4.2.2 prints a line you can paste into a workflow. The script fails loudly on a misspelled tag instead of printing an empty SHA.
Method 2: resolve with the GitHub API
The gh CLI can call the REST API. The commits endpoint accepts a tag name and returns the commit it resolves to, peeling annotated tags for you:
gh api repos/actions/checkout/commits/v4.2.2 --jq '.sha'If you want to see the two steps separately, read the ref first:
gh api repos/actions/checkout/git/ref/tags/v4.2.2 --jq '.object'The result has a type and a sha. If type is commit, the tag is lightweight and the SHA is final. If type is tag, the SHA belongs to a tag object. Fetch it to find the commit:
gh api repos/actions/checkout/git/tags/<tag-object-sha> --jq '.object.sha'The API route is useful in scripts that already authenticate with gh. It also avoids git's output format, so you don't have to parse ^{} lines.
Method 3: read it from the web interface
On the repository's Releases page, open the release and click the tag name. The tag page shows the commit it points at, but with an abbreviated 7-character hash. Click through to the commit page and copy the full 40-character SHA from the address bar or the page itself.
This is fine for a one-off pin. It is the easiest method to get wrong, though. Truncated hashes are not accepted in uses:, and copying from a search result or a pull request page can give you a commit that isn't on the release tag.
Verify the SHA belongs to the repository
Resolving the SHA is half the job. The other half is confirming that it belongs to the repository named in uses:. This matters because GitHub stores a repository and all its forks in one shared object store. A commit that exists only in a fork can be addressed through the parent repository's URL, and uses: owner/repo@<sha> may resolve it. Someone can therefore hand you a SHA that looks legitimate but comes from a fork. A later chapter covers these impostor commits in full. Here you only need the habit of checking.
Be careful about what counts as proof. A successful response from gh api repos/owner/repo/commits/<sha> does not prove the commit is in that repository's history. The fork network can answer that call too. Check reachability from a ref that the repository itself owns.
A reliable check is a blobless clone, which downloads history but not file contents. A plain clone fetches only the refs of that one repository, so a fork-only commit is absent:
git clone --quiet --filter=blob:none --no-checkout \
https://github.com/actions/checkout.git /tmp/checkout-verify
cd /tmp/checkout-verify
sha=11bd71901bbe5b1630ceea73d27597364c9af683
# 1. The tag resolves to the SHA you intend to pin
git rev-parse 'v4.2.2^{commit}'
# 2. The commit object exists in this repository's own history
git cat-file -e "${sha}^{commit}" && echo "commit present"
# 3. A tag or branch of this repository contains the commit
git tag --contains "$sha"
git branch -r --contains "$sha"Read the results this way:
- The first command must print exactly the SHA you plan to use. If it prints something else, you copied the wrong value, or the tag has moved since you looked.
- The second command must print
commit present. If it fails, the commit isn't in the repository's own refs. - The last two commands must list at least one tag or branch. A SHA that no ref reaches is suspicious. The GitHub commit page shows a banner in this case: "This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository."
The repository does not have to be one you trust blindly. Verification confirms that the SHA matches the release you intended. It does not confirm that the release is free of malicious code. For anything you depend on heavily, skim the diff between the previous pin and the new one before you commit the change.
You can also check whether the maintainer signed the commit. Run git log --show-signature -1 <sha> after importing the key, or look for the "Verified" badge on the commit page. Many maintainers do not sign, so treat a verified signature as extra evidence and an unsigned commit as normal.
The full workflow
Common mistakes
- Pinning the tag object SHA. The workflow run fails to resolve the action. Use the
^{}line, or the commits endpoint. - Resolving a moving tag.
v4today is notv4next month. Resolve the exact release tag. - Using a short SHA. Only a full-length 40-character SHA is a valid pin.
- Trusting a SHA from a blog post or pull request. Resolve it yourself from the tag, then compare it with what you were given.
- Treating API success as proof. The fork network can serve commits through the parent's URL, so check reachability from the repository's own refs.
Exercises
Exercise
You run git ls-remote https://github.com/example/tool 'refs/tags/v2.1.0' 'refs/tags/v2.1.0^{}' and get two lines, with different SHAs. Which one goes into uses:, and why?
Show answer
The SHA on the line ending in refs/tags/v2.1.0^{}. The tag is annotated, so the first line is the tag object and the ^{} line is the commit it points at. The uses: key needs a commit SHA.
Exercise
gh api repos/owner/repo/commits/<sha> returns the commit's details without an error. Does that prove the commit is part of owner/repo's history? What check would?
Show answer
No. A repository and its forks share one object store, so a commit that exists only in a fork can answer through the parent's URL. To check, clone the repository and confirm the commit exists there (git cat-file -e <sha>^{commit}). Then confirm that a tag or branch contains it (git tag --contains <sha> or git branch -r --contains <sha>).