Sub topic 3
Pinning an action to a full-length commit SHA
This chapter assumes you can read a workflow file with jobs, steps, and uses:. It also assumes you know that a Git tag is a named pointer to a commit. The previous chapter showed that a tag such as v4 can be moved. Here you fix that by referencing a commit directly.
What pinning means
A commit SHA is a 40-character hexadecimal hash computed from a commit's contents, its parent, and its metadata. You can't change a commit without changing its SHA. If a uses: line names a full SHA, the runner fetches that exact commit. A maintainer can't alter it later, and neither can an attacker with write access to the repository.
Pinning is the practice of writing that SHA in place of a tag or branch.
steps:
# Mutable: the tag can be moved to a different commit
- uses: actions/checkout@v4
# Immutable: a full-length commit SHA identifies exactly one commit
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2The first step runs whatever commit v4 points to at the moment the job starts. The second step runs one commit every time. The trailing comment is for people. GitHub ignores it, but without it nobody can tell which release the hash refers to.
Rules that cause common errors
- The SHA must be the full 40 characters. An abbreviated form such as
11bd719is not accepted as a pin. Copy the whole value. - It must be a commit SHA, not a tag object SHA. An annotated tag is its own Git object with its own hash, and that object points to a commit. If you paste the tag object's hash, the reference fails to resolve. Lightweight tags point straight at the commit, so they don't have this problem. The next chapter shows how to find the commit behind a tag.
- The commit must belong to the action's repository network. GitHub resolves commits across a repository and its forks. A SHA that exists only in a fork can still resolve under the upstream repository's name. This is the impostor commit problem, covered later in this tutorial.
- Pin each
uses:line separately. A workflow with ten steps has ten references to update.
What you give up
Pinning trades convenience for predictability. With @v4 you receive bug fixes and security patches automatically. With a SHA you receive nothing until someone changes the line. A pin that is never updated will keep running an old version, including any known vulnerabilities in it.
For that reason a pin is only half of a workflow. The other half is a way to refresh it, which the Dependabot chapter covers. Pins also don't change what the action does. If the pinned commit itself contains malicious code, pinning preserves it faithfully. It guards against changes after you reviewed the code, not against code you never reviewed.
| Reference | Changes without your edit? | Readable at a glance | Gets updates automatically |
|---|---|---|---|
| Branch (`@main`) | Yes, on every push | Yes | Yes |
| Tag (`@v4`) | Yes, if the tag is moved or the major line advances | Yes | Within that major version |
| Full SHA | No | Only with a comment | No |
Applying it to a workflow
A practical routine for a single action:
- Pick the release you have decided to use, for example
v4.2.2. - Find the commit that tag resolves to in the action's repository.
- Replace the tag in
uses:with the 40-character hash. - Add a comment with the exact version after the hash.
- Run the workflow once to confirm that the step still resolves and runs.
If a run fails with an error that the action can't be found or resolved, check for a shortened hash, a tag object hash, or a typo in the owner or repository name.
Exercise
Your workflow contains uses: actions/setup-node@a1b2c3d. A teammate says it is pinned. Is it, and why?
Show answer
No. a1b2c3d is an abbreviated SHA, and a pin requires the full 40-character commit SHA. The reference should be replaced with the full hash, followed by a comment naming the version.
Exercise
You paste the hash of an annotated tag into uses: and the step fails to resolve. What is the likely cause, and what should you use instead?
Show answer
An annotated tag is a separate Git object that points to a commit, so its hash is not a commit SHA. Use the commit the tag resolves to, which is the value shown for the ^{} form of the tag.