Sub topic 4
Using actions
This chapter assumes you can read a workflow file, know what a job and a step are, and have used uses: and run: at least once. It covers what an action is, how to reference one safely, and the mistakes that most often break a first deployment.
What an action is
An action is a reusable unit of work that a step can call. A run: step executes shell commands you write. A uses: step executes code that someone else packaged, and it takes its settings through with:.
Actions come from three places:
- The Marketplace and public repositories, such as
actions/checkout. - Your own repository, referenced by path, such as
./.github/actions/notify. - Other repositories in your organization.
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ciThe reference owner/repo@ref names the repository and the version to run. Here actions/setup-node@v4 means the setup-node action from the actions organization, at the v4 tag.
Checkout comes first
A runner starts with an empty workspace. Your code is not there until a step fetches it. Any job that builds, tests, or reads files from the repository needs actions/checkout as its first step. A job that only downloads an artifact, like the deploy job in this tutorial, does not need it.
Pinning versions
The part after @ decides what code runs in your pipeline, and a deploy job holds cloud credentials. You have three options:
| Reference | Example | Trade-off |
|---|---|---|
| Major tag | `@v4` | Gets fixes automatically. The tag can move. |
| Exact tag | `@v4.1.7` | Predictable, but tags can still be re-pointed. |
| Commit SHA | `@<full 40-character SHA>` | Immutable. Needs a tool such as Dependabot to update. |
For actions published by GitHub or your own organization, major tags are a reasonable default. For third-party actions that touch credentials or deploy code, pin to a full commit SHA and note the version in a comment.
Inputs and outputs
Each action declares its inputs in its action.yml file, and its README lists them. Pass inputs with with:. Some actions also produce outputs. Give the step an id and read the output through the steps context.
steps:
- id: version
run: echo "tag=$(date +%Y%m%d)-${GITHUB_SHA::7}" >> "$GITHUB_OUTPUT"
- run: echo "Deploying ${{ steps.version.outputs.tag }}"The first step is a run: step, and it shows the output mechanism: write name=value to the file named by $GITHUB_OUTPUT. Actions use the same mechanism internally.
Common errors
- Missing checkout. Commands such as
npm cifail with "no package.json" because the workspace is empty. - Misspelled input. An unknown key under
with:produces only a warning, and the action then uses its default. Check the spelling against the action'saction.yml. - Wrong ref.
Unable to resolve actionmeans the tag or SHA does not exist, or the repository is private and not shared with yours. - Unreviewed third-party action. It runs with the job's permissions and can read any secret the job can see. Read its source before you trust it.
Exercise
Your deploy job runs uses: some-user/deploy-tool@main and receives cloud credentials. Name the risk and give one change that reduces it.
Show answer
A branch such as main changes whenever the author pushes, so the job can run code you never reviewed, with your credentials. Pin the action to a full commit SHA after reviewing that version, and let Dependabot propose updates.