Sub topic 1
Recap: workflows, jobs, and needs
This chapter refreshes the workflow concepts that environments build on. Environments attach to jobs, so you need a clear model of how jobs run and depend on each other.
Prerequisites
This chapter assumes you can do the following:
- Use GitHub repositories, branches, pull requests, and repository settings.
- Read and write basic GitHub Actions workflow YAML.
- Explain why secrets must not be committed to a repository.
Workflows, jobs, and steps
A workflow is a YAML file in .github/workflows/. It starts when an event happens, such as a push or a manual dispatch. A workflow contains one or more jobs. A job contains an ordered list of steps.
- A job runs on its own fresh runner, which is a virtual machine that GitHub provides or that you host yourself.
- Steps in the same job share that runner's filesystem. Steps in different jobs do not.
- Jobs run in parallel by default.
The job is the unit that environments attach to. A later chapter adds one key, environment, to a job. Everything else in this chapter stays the same.
Ordering jobs with needs
The needs keyword makes a job wait for other jobs. A job with needs starts only after every listed job finishes successfully. If a required job fails or is skipped, the dependent job is skipped as well.
name: Pipeline
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "building"
test:
needs: build
runs-on: ubuntu-latest
steps:
- run: echo "testing"
deploy:
needs: [build, test]
runs-on: ubuntu-latest
steps:
- run: echo "deploying"Here build runs first, then test, then deploy. Listing build in deploy is optional because test already depends on it, but it makes the dependency explicit.
Passing data between jobs
Because each job gets its own runner, a job cannot read files that another job created. You have two main options:
- Upload files as artifacts in one job and download them in a later job.
- Declare job outputs, which pass small string values to jobs that list the producer in
needs.
Secrets are also scoped. A repository secret is available to any job in the repository's workflows. Environment secrets, covered later, are available only to jobs that target that environment.
Common errors
- Missing dependency. Without
needs, a deploy job can start at the same time as the build job and deploy stale or missing output. - Typo in a job name.
needs: biuldmakes the workflow invalid, and it will not run. - Assuming shared files. A file written in
builddoes not exist indeployunless you pass it as an artifact. - Skipped dependents. If an earlier job is skipped, jobs that need it are skipped too, unless you override this with an
ifcondition.
Exercise
In a workflow, the deploy job has needs: test, and the test job fails. What happens to deploy, and why might deploy still be unable to read a file that build created?
Show answer
deploy is skipped because a job it needs did not succeed. Even when it runs, each job has its own runner, so files from build are not available unless they are passed as artifacts.