Sub topic 5
Structuring a CI workflow for image builds
This chapter assumes you can write a Dockerfile, know what a Git branch and tag are, and can read YAML. Here you will lay out the workflow file that builds your image. Registry login, tagging and caching are added in later chapters.
What a workflow is made of
A GitHub Actions workflow is a YAML file in .github/workflows/. It has three main parts:
- Triggers decide which events start a run, such as a push or a pull request.
- Jobs are groups of steps. Each job runs on its own fresh machine.
- Runners are the machines that execute jobs.
ubuntu-latestis a GitHub-hosted Linux runner with Docker already installed.
Steps inside a job run in order and share the runner's file system. Separate jobs do not share files unless you pass them explicitly.
A first build-only workflow
This workflow builds the image on every pull request and on pushes to main or to version tags. It never pushes, so it needs no registry credentials yet.
name: build-image
on:
push:
branches: [main]
tags: ["v*"]
pull_request:
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v6
with:
context: .
push: falseThe checkout step is required. A fresh runner has no copy of your repository, so without it the build context is empty.
Choosing triggers
Build on pull requests so a broken Dockerfile is caught before it merges. Publish only from main and from tags, because those represent code you have accepted. The pattern v* matches tags such as v1.2.0.
Pull requests from forks get a read-only token and no access to your secrets. A build that needs to push will fail there, so the publish steps must be skipped for pull requests. Later chapters do this with the condition github.event_name != 'pull_request'.
Setting permissions
The permissions block limits what the built-in GITHUB_TOKEN can do. Start with contents: read. Add packages: write only when the workflow pushes to GitHub Container Registry. Granting only what the job needs limits the damage if a step is compromised.
Common errors
- Empty or missing context. The build fails with "no such file". The cause is usually a missing
actions/checkoutstep. - Tag pushes do nothing. The
tagsfilter has to match exactly. A tag1.0.0does not matchv*. - Publishing from pull requests. Pushing without a condition fails on forks and could publish unreviewed code from your own branches.
- Unpinned actions.
@v4follows a moving major version. This is usually acceptable. Pin to a commit SHA if your security policy requires it.
Exercise
A teammate pushes the tag release-3 and expects the workflow above to build the image, but no run starts. Why, and what is the smallest change that fixes it?
Show answer
The tag filter is v*, and release-3 does not start with v, so the push event matches no trigger. Either rename the tag to something like v3.0.0, or change the filter to include the new pattern, for example tags: ["v*", "release-*"].