Sub topic 2
Workflows, jobs, and triggers in GitHub Actions
This chapter assumes you know basic Git and GitHub usage, can read YAML, and understand Docker images and Kubernetes Deployments. Here you learn the three building blocks of every GitHub Actions pipeline: workflows, jobs, and triggers. Later chapters use them to build and deploy an image.
The working model
A workflow is a YAML file in .github/workflows/ in your repository. GitHub reads each file there and runs it when its trigger fires.
A workflow contains one or more jobs. Each job runs on its own fresh virtual machine, called a runner. A job contains an ordered list of steps. A step either runs a shell command (run) or calls a reusable action (uses).
Triggers
The on key says what starts the workflow. The deploy pipeline in this tutorial starts on every push to main:
name: deploy
on:
push:
branches: [main]Other common triggers are pull_request, workflow_dispatch (a manual run from the GitHub UI), and schedule. For deployments, limiting push to a branch is important. Without a branch filter, a push to any branch would try to deploy.
Jobs and dependencies
Jobs run in parallel by default. Because the machines are separate, a job cannot see files or variables created by another job. Use needs to make one job wait for another:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Building commit $GITHUB_SHA"
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- run: echo "Deploying after build succeeded"If build fails, deploy is skipped. This gives you a safe default: nothing reaches the cluster unless the image was built and pushed.
Since the jobs do not share a file system, the two jobs pass information through external systems. In our pipeline, the image registry carries the image, and the commit SHA, which both jobs can read, identifies it.
Permissions and context
The permissions key limits what the automatic GITHUB_TOKEN can do. Set it explicitly instead of relying on the repository default:
permissions:
contents: read
packages: writeExpressions in ${{ ... }} read from contexts. Common ones are github.sha (the commit being built), github.repository, secrets.NAME, and env.NAME. Inside a run script, the same data is also available as environment variables such as $GITHUB_SHA.
Common errors
- Wrong file location. A workflow outside
.github/workflows/, or without a.ymlor.yamlextension, never runs. - Assuming shared state between jobs. A file written in
builddoes not exist indeploy. Re-runactions/checkoutor pass data through the registry. - Missing
needs. Without it,buildanddeploystart at the same time, and deploy may reference an image that does not exist yet. - Unquoted YAML values. Characters like
:or${{ }}at the start of a value can break parsing. Quote strings when in doubt. - Missing checkout. A runner starts empty. If a job needs your source code or manifests, add
actions/checkout.
Exercises
Exercise
Your deploy job fails because the manifest file k8s/deployment.yaml cannot be found, although the build job checked out the repository. What is the cause, and how do you fix it?
Show answer
Each job runs on a separate runner, so the checkout in build does not carry over. Add - uses: actions/checkout@v4 as a step in the deploy job.
Exercise
Which key makes deploy wait for build, and what happens to deploy if build fails?
Show answer
needs: build creates the dependency. If build fails, deploy is skipped and does not run.