Sub topic 8
Passing Build Output from One Job to the Next
This chapter assumes you can read a GitHub Actions workflow file, know the difference between a job and a step, and know that every job runs on its own runner with a fresh file system. Here you will use an artifact to hand files from one job to another.
Why jobs cannot share files directly
Each job gets a new virtual machine. Files that a build job writes to disk are gone when that job ends, and a later deploy job starts with an empty workspace. Two things are needed to pass files along:
- The producing job saves the files with
actions/upload-artifact. - The consuming job fetches them with
actions/download-artifact.
Jobs also run in parallel by default. The consuming job must wait for the producing job to finish, or the artifact may not exist yet. The needs keyword creates that ordering.
The build and deploy pattern
The workflow below builds a project in one job and inspects the result in another.
name: build-and-deploy
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm run build
- uses: actions/upload-artifact@v4
with:
name: dist
path: dist/
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
name: dist
path: dist/
- run: ls -R dist/Follow the data through the workflow:
- The
buildjob checks out the code, installs dependencies, and builds. The upload step stores the contents ofdist/under the artifact namedist. needs: buildholds thedeployjob untilbuildsucceeds.- The
deployjob does not check out the code. It only downloads the artifact into adist/directory and lists the files.
How download-artifact places files
The name input must match the name used at upload. The path input is the directory where files are written, and the action creates it if needed. Download into a directory you choose. The runner's workspace is otherwise empty, so a clear target path makes later steps easier to write.
The upload step stores paths relative to the common root of the files it matched. Uploading dist/ stores the files inside it, not the dist folder itself. That is why the download step sets path: dist/ again. If you left out path, the files would land directly in the workspace root.
If you omit name, the action downloads every artifact from the current run. Each one goes into its own subdirectory named after the artifact. For a single known artifact, setting name gives a simpler layout.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Unable to find any artifacts for the associated workflow` | The names differ, or the download job ran before the upload finished | Match the `name` values and add `needs` |
| The download job starts but the directory is empty or missing | The upload `path` matched no files and the default only warned | Set `if-no-files-found: error` on the upload |
| Files appear in an unexpected folder | `path` was omitted, or the download used a different layout than you assumed | Set `path` explicitly and check with `ls -R` |
| Deploy job runs even though build failed | `needs` is missing, or an `if: always()` condition overrides the default | Add `needs: build` and review any `if` conditions |
> Do not rely on job order in the file. Only needs creates a dependency.
Checking the transfer
Add a listing step right after the download while you build a workflow. The ls -R dist/ line in the example does this. It shows exactly what the consuming job received, which is faster than guessing from a failure several steps later.
Exercise
In the build-and-deploy workflow, what two things go wrong if you remove needs: build from the deploy job?
Show answer
The deploy job no longer waits for build, so both jobs start at the same time. The artifact named dist usually does not exist yet when the download step runs, so the download fails. Even if it timed out right, the deploy job could not rely on getting the finished output.
Exercise
The deploy job needs the built files, but it never runs actions/checkout. Why does that work?
Show answer
The files it needs come from the artifact, not from the repository. The download step places them on the new runner, so checking out the source is not required unless the job also needs the source code.