Sub topic 6
Passing the build to the deploy job and caching dependencies
This chapter assumes you can read a workflow file, know that each job runs on its own runner, and have a build job that runs npm ci, npm test, and npm run build. Here you will move the build output to a deploy job and speed up installs with a cache.
Why jobs need artifacts
Each job starts on a fresh virtual machine. Files created in the build job do not exist in the deploy job, even when needs: build makes it wait. Two features cover the gap, and they solve different problems:
| Feature | Purpose | Lifetime | Scope |
|---|---|---|---|
| Artifact | Hand files from one job to another, or keep them for download | 90 days by default (configurable) | One workflow run |
| Cache | Reuse files that rarely change, such as downloaded dependencies | Evicted after 7 days without use | Shared across runs |
An artifact carries the result of this run. A cache carries inputs that are expensive to fetch again.
Uploading the build output
At the end of the build job, upload the dist folder:
- uses: actions/upload-artifact@v4
with:
name: site
path: dist
if-no-files-found: error
retention-days: 7name identifies the artifact. path accepts a file, a directory, or a glob. Setting if-no-files-found: error makes the step fail when the build produced nothing. Without it, the default is a warning, and you could deploy an empty site. retention-days shortens the default retention, which is useful for build output you do not need to keep.
Downloading it in the deploy job
The deploy job lists build in needs, then downloads the artifact by name:
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
name: site
path: distThe download step does not check out your repository. It only restores the files you uploaded, which is why the deploy job above needs no actions/checkout. Deploying the exact files that passed testing, instead of rebuilding them, means production runs what CI verified.
Caching dependencies
Installing packages is often the slowest part of the build job. The built-in cache in actions/setup-node handles the common case:
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ciThe action caches the npm download directory, keyed on a hash of package-lock.json. When the lockfile changes, the key changes and a new cache is saved. When it does not, the next run restores the old one and npm ci skips most downloads. npm ci still runs and still deletes and recreates node_modules, so the cache speeds up downloads but does not skip installation.
For other tools, use actions/cache directly and choose the key yourself:
- uses: actions/cache@v4
with:
path: ~/.cache/pip
key: pip-${{ runner.os }}-${{ hashFiles('requirements.txt') }}
restore-keys: |
pip-${{ runner.os }}-If no exact match exists for key, the cache falls back to the newest entry matching a restore-keys prefix. That gives a partial hit that is usually better than nothing.
Common errors
- "Artifact not found" in the deploy job. The name in
download-artifactdiffers from the name inupload-artifact, or the build job failed before uploading. - Missing files after download.
pathin the download step is where files are placed. If you uploadeddistand download with nopath, the contents land in the working directory, not in adistfolder. - Stale cache hiding a problem. A key that never changes keeps restoring old content. Include a hash of the lockfile in the key.
- Secrets in artifacts. Anyone with read access to the repository can download artifacts. Never put
.envfiles or credentials in the upload path.
Exercise
Your deploy job runs aws s3 sync dist s3://example-bucket, but the log shows the dist folder is empty, and the build job succeeded. Name two likely causes in the upload step and one setting that would have caught the problem earlier.
Show answer
Likely causes: the path in upload-artifact points to the wrong folder (for example build instead of dist), or the build wrote its output elsewhere so the uploaded path held no files. Setting if-no-files-found: error on the upload step makes the build job fail immediately when nothing matches, instead of only warning.