Sub topic 5
Actions you will use most often
This chapter assumes you can read a workflow file (events, jobs, steps) and know the uses and with syntax from earlier chapters. Here you will meet the actions that appear in most real workflows and learn what each one is for.
Why a short list covers most workflows
Most workflows repeat the same sequence: get the code, install a toolchain, restore saved files, run commands, and save results. A small set of actions handles each stage. Most of them are published by the actions organization on GitHub, so they carry the same ownership as the platform itself.
actions/checkout
A job starts on a clean runner with no copy of your repository. actions/checkout downloads the code into the workspace so later steps can use it. Put it first in almost every job.
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0By default, checkout fetches only the latest commit (fetch-depth: 1). Setting fetch-depth: 0 fetches the full history. You need that for tools that read tags or compare commits.
The setup actions
Setup actions install a language runtime and put it on the PATH. Examples are actions/setup-node, actions/setup-python, and actions/setup-java. Each takes a version input, such as node-version, python-version, or java-version.
Many setup actions also offer a built-in cache input. With cache: npm, setup-node caches the npm download directory for you, keyed on your lock file. Use this first before you write a separate cache step.
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm testactions/cache
When no built-in option fits, actions/cache saves and restores any directory. You give it a path and a key. If a cache with the exact key exists, it is restored. If not, the action saves the path at the end of the job.
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
id: npm-cache
with:
path: ~/.npm
key: npm-${{ runner.os }}-${{ hashFiles('**/package-lock.json') }}
- if: steps.npm-cache.outputs.cache-hit != 'true'
run: echo "Cache miss, dependencies will be downloaded"The key contains hashFiles(...), so it changes whenever the lock file changes. A changed key means a cache miss and a fresh download. The id on the step lets the last step read the cache-hit output through the steps context. Note that the output is a string, so compare it with 'true'.
Artifacts: upload-artifact and download-artifact
A cache speeds up future runs. An artifact keeps files produced by this run, such as test reports or build output, so you can download them from the run page or pass them to another job.
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: mkdir -p dist && echo "built" > dist/result.txt
- uses: actions/upload-artifact@v4
with:
name: build-output
path: dist/
inspect:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
name: build-output
path: dist/
- run: cat dist/result.txtJobs run on separate machines, so files do not carry over between them. Upload in one job, download in the next, and declare the order with needs.
Cache or artifact?
| Question | Cache | Artifact |
|---|---|---|
| Purpose | Speed up later runs | Keep or share results of this run |
| Typical content | Dependencies | Reports, binaries, logs |
| Lookup | By key, across runs | By name, within a run or from the run page |
| Safe to lose? | Yes, the job still works | No, the output is the point |
Common mistakes
- Running
npm cibeforeactions/checkout. There is nopackage-lock.jsonyet, so the command fails. - Using the same artifact
nametwice in one run withupload-artifact@v4. The second upload fails because names must be unique. - Building a cache key that never changes, so stale content is restored forever.
- Comparing
cache-hitto a boolean instead of the string'true'.
Exercise
A job has two steps in this order: run: npm ci and then uses: actions/checkout@v4. The first step fails. Why, and how do you fix it?
Show answer
The runner starts with an empty workspace, so npm ci finds no package-lock.json or package.json. Move actions/checkout before npm ci so the repository is downloaded first.
Exercise
You build a binary in one job and need to run it in a second job. Do you use actions/cache or artifacts, and what keyword makes the second job wait for the first?
Show answer
Use upload-artifact in the first job and download-artifact in the second. Add needs: <first-job-id> to the second job so it starts after the first finishes.