Sub topic 5
Keeping pins readable with version comments
A full-length commit SHA is exact, but it tells a human nothing. This chapter covers how a trailing version comment fixes that, what the comment does and doesn't guarantee, and how to check that it matches the SHA beside it.
This chapter assumes you can read a GitHub Actions workflow, know the difference between a tag and a commit, and have already pinned an action to a SHA as described in the previous chapter.
The readability problem
Compare these two lines:
- uses: actions/checkout@v4
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683The first line tells you the intent: major version 4 of checkout. The second is safer, but a reviewer looking at a pull request cannot tell whether it is version 4.2.2, version 3.6.0, or a commit from nowhere. A pull request that changes one 40-character hex string to another is hard to review.
The convention that solves this is a trailing YAML comment naming the release the SHA was taken from:
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2The SHA is what the runner executes. The comment is only for people and for tools that read it.
What the comment is, and is not
YAML ignores everything after #. GitHub never reads the comment, so the comment has no effect on which code runs.
This has a consequence that is easy to miss. The comment is a claim, and nothing in GitHub checks it. If someone edits the SHA and leaves # v4.2.2 in place, the workflow still runs and the comment is now wrong. An attacker, or a careless contributor, can use this: a pull request that swaps in a different SHA while keeping a familiar comment looks like a no-op to a reviewer who trusts the comment.
Treat the comment as documentation that must be verified, not as evidence.
Choosing what to write in the comment
The comment should name the most specific release that points at the pinned commit. Compare the options:
| Comment | Precision | Problem |
|---|---|---|
| `# v4.2.2` | Exact release | None. This is the recommended form. |
| `# v4` | Major version only | The `v4` tag moves with each release, so it doesn't identify one commit. You can't tell later whether the pin is current. |
| `# main` | A branch name | A branch moves constantly. The comment says almost nothing about the pinned commit. |
| `# latest as of 2026-01` | Free text | Tools can't parse it. A reader has to find the date's context. |
| (none) | None | Reviewers and update tools lose the link to a release. |
Use the full vMAJOR.MINOR.PATCH form as the tag name appears in the action's repository. If the action tags releases without a leading v, copy the tag as written. Keeping the exact tag name matters because tools and scripts, including the one later in this chapter, look the tag up by that name.
If you have to pin a commit that is not a release, for example an unreleased fix, say so plainly:
- uses: some-org/some-action@0123456789abcdef0123456789abcdef01234567 # unreleased fix for issue 214, after v2.3.1A comment like this will not be recognised by update tooling. That is a reasonable trade for being honest about the pin, but it also means you will update that line by hand.
How tools use the comment
Update tools read the comment to keep pins current. When Dependabot sees an action pinned to a SHA with a version comment, it can propose a pull request that changes both the SHA and the comment to the newer release. The configuration appears later in this tutorial, in the chapter on Dependabot. Without the comment, a reviewer sees only two hex strings, and a tool has less information about which release you meant to track.
The flow looks like this:
The diagram shows why the comment matters at the review step. The reviewer reads v4.3.0 and can open the action's release page to see what changed. With only a SHA, that step is much harder.
Writing a good comment in practice
Follow these habits so that comments stay useful:
- Put the comment on the same line as
uses:, separated by one space, then#, then one space, then the tag. Tools that parse the comment expect this layout, so a consistent shape also keeps diffs predictable. - Update the comment in the same commit as the SHA. A commit that changes only one of them is a red flag in review.
- Don't add extra prose to the same line when a plain tag will do. A comment like
# v4.2.2 (stable, we trust it)is harder for tools and scripts to parse. - Pin the same release of the same action everywhere in the repository. If five workflows use
actions/checkout, five lines carry the same SHA and comment, and an update pull request touches all five.
Getting the SHA for the comment
You write the comment by looking up the release. The command below, from the previous chapter, prints the commit that a tag resolves to:
git ls-remote https://github.com/actions/checkout 'refs/tags/v4.2.2' 'refs/tags/v4.2.2^{}'If the tag is annotated, the output has two lines, and the one ending in ^{} is the commit. If the tag is lightweight, there is only one line, and it is the commit. In both cases the last line printed is the value to paste after @. Then write the same tag name in the comment.
Verifying comments automatically
Because nothing enforces the comment, you can add a check yourself. The script below reads every uses: line in .github/workflows that has a 40-character SHA and a # vX comment. For each one it asks the action's repository which commit that tag resolves to and compares the result with the pinned SHA.
#!/usr/bin/env bash
set -euo pipefail
status=0
while read -r _ ref _ version; do
repo=$(printf '%s' "${ref%@*}" | cut -d/ -f1-2)
pinned=${ref#*@}
actual=$(git ls-remote "https://github.com/${repo}" \
"refs/tags/${version}" "refs/tags/${version}^{}" | awk 'END { print $1 }')
if [ "$actual" != "$pinned" ]; then
echo "MISMATCH: ${ref} is commented ${version}, tag resolves to ${actual:-nothing}"
status=1
fi
done < <(grep -rhoE 'uses: [^@ ]+@[0-9a-f]{40} +# v[0-9][^ ]*' .github/workflows || true)
exit "$status"A few details are worth knowing:
cut -d/ -f1-2keeps onlyowner/repo. This handles actions that live in a subdirectory, such asgithub/codeql-action/init, and reusable workflows referenced by a path.- The
awk 'END { print $1 }'step prints the last line's commit. For an annotated tag that is the peeled^{}line, which is the commit rather than the tag object. - Local actions such as
./.github/actions/setup-toolshave no@, so the pattern skips them. - The script only checks pins that have a comment starting with
# vfollowed by a digit. A pin with no comment, or a comment in another style, is silently skipped. Pair this script with a rule that every pin must have a comment, or you will have gaps.
Run it locally from the repository root, and in CI as a pull-request check. A tag that was moved after you pinned shows up as a mismatch, which is also useful: it tells you a maintainer retagged a release, and that is worth investigating.
Common errors
- Pasting the tag object SHA instead of the commit SHA. For an annotated tag, the first
ls-remoteline is the tag object. Using it inuses:fails, because the runner expects a commit. Use the^{}line. - Copying the comment from another repository. Pin lines for
actions/checkoutandactions/cachecan both sayv4.2.2only if both repositories have that tag. Look up each action separately. - Using a short SHA. Short SHAs are not accepted as pins. Use all 40 characters.
- Trusting a comment in a pull request. An update pull request from a bot is generally reliable, but a human-authored change to a SHA needs the verification step above.
- Leaving the comment on a major tag.
# v4goes stale in a way you can't see. The SHA stays fixed while thev4tag moves on, so the comment can describe a version you are not running. - Forgetting the comment in copied snippets. Many README examples show
@v4. When you convert one to a SHA, add the comment at the same time, not later.
Summary of the working model
The SHA decides which code runs. The comment tells people and tools which release you meant. Because the two are linked only by convention, you keep them honest in three ways: write the exact release tag, change both together in one commit, and run a check that resolves the tag and compares it to the SHA.
Exercise
A pull request changes uses: actions/cache@0123456789abcdef0123456789abcdef01234567 # v4.1.0 to a different SHA and leaves the comment as # v4.1.0. The workflow still runs without errors. Why is this a problem, and what two checks would catch it?
Show answer
GitHub ignores the comment, so the workflow runs whatever the new SHA contains, while the unchanged comment still claims it is v4.1.0. A reviewer who trusts the comment could approve code from an unknown commit. Two checks catch it: resolve the tag yourself with git ls-remote against refs/tags/v4.1.0 and refs/tags/v4.1.0^{} and compare the last commit to the new SHA, or run the comment-verification script in CI, which reports a MISMATCH when the tag does not resolve to the pinned SHA.
Exercise
You find these three pins in a repository: # v4, # main, and # v4.2.2. Which one is written correctly, and why do the other two fall short?
Show answer
# v4.2.2 is correct because it names one exact release whose tag can be looked up and compared with the SHA. # v4 names a tag that moves with each release, so it doesn't identify one commit and hides staleness. # main names a branch that changes constantly, so it says almost nothing about the pinned commit, and update tools can't use it to track a release.