Sub topic 10
Pinning to tags, branches and commit SHAs
This chapter assumes you can write a basic workflow, and that you know how to call a reusable workflow with jobs.<job_id>.uses and pass with: and secrets:. Here you will learn how the @ref part of a call works, which kinds of refs are available, and how to choose between them.
What the ref does
A call to a reusable workflow in another repository has this form:
{owner}/{repo}/.github/workflows/{filename}@{ref}The {ref} is required for calls to other repositories. It tells GitHub which version of the called workflow file to run. It can be a tag, a branch name or a commit SHA. The ref is resolved when the caller's run starts, and the called file is taken from that point in the called repository's history.
A call in the same repository uses a relative path, such as ./.github/workflows/node-test.yml. It has no @ref. It runs the file from the same commit as the caller, so both always move together.
The three kinds of ref
jobs:
by-tag:
uses: my-org/shared-workflows/.github/workflows/node-test.yml@v1
by-branch:
uses: my-org/shared-workflows/.github/workflows/node-test.yml@main
by-sha:
uses: my-org/shared-workflows/.github/workflows/node-test.yml@8f3c2a1d4b5e6f708192a3b4c5d6e7f8091a2b3c| Ref type | Moves over time? | Trade-off |
|---|---|---|
| Branch such as `main` | Yes, on every push | Callers get fixes at once, and also get breaking changes at once |
| Tag such as `v1` | Only if someone moves or re-creates it | Readable, and easy to update for all callers |
| Full commit SHA | No | Exact and reproducible, but harder to read and update |
Use the full 40-character SHA. GitHub does not accept abbreviated SHAs in a uses: reference. If a tag and a branch share a name, the result can be ambiguous, so avoid giving them the same name.
Choosing a ref
Think about who controls the called workflow and how much you trust it.
- Within one team, a major-version tag such as
@v1is usually enough. The workflow author publishes fixes by movingv1to the newest compatible commit. - For workflows owned by another team or organization, pin to a commit SHA. A tag can be moved, so a SHA is the only ref that guarantees the code you reviewed is the code that runs.
- Use a branch only while developing. A branch is useful for testing a change from a pull request before you tag it.
A release flow for the workflow author
If you maintain the shared workflow, tag releases so callers can opt in to changes:
- Merge a change to
mainand test it from a caller that uses@mainor a feature branch. - Create an immutable version tag, for example
v1.2.0. - Move the floating major tag
v1to the same commit.
git tag v1.2.0
git push origin v1.2.0
git tag -f v1 v1.2.0
git push --force origin v1Callers on @v1 get the new behavior. Callers on @v1.2.0 or on a SHA do not. Treat a breaking change, such as a renamed input or a removed output, as a new major version (v2), so existing callers keep working.
Common mistakes
- Omitting the ref on a cross-repository call. The workflow fails validation.
- Using
@mainin production and then being surprised when a change in the shared repository breaks every caller at once. - Using a short SHA. Use all 40 characters.
- Changing inputs or outputs under an existing tag. Callers pinned to that tag get errors with no change on their side.
Exercise
Your team calls a reusable workflow owned by another organization, and your security policy requires that reviewed code is the code that runs. Which ref type do you use, and why is a tag not enough?
Show answer
Use a full 40-character commit SHA. A tag can be deleted and re-created or force-moved to a different commit, so the code behind it can change without any change in your repository. A commit SHA always identifies the same content.
Exercise
You are the author of a shared workflow at v1. You want to rename an input from node-version to node. How do you release this without breaking existing callers?
Show answer
Treat it as a breaking change and publish it under a new major tag, such as v2. Leave v1 pointing at the old behavior so callers on @v1 keep working, and let callers move to @v2 when they update their with: blocks.