Sub topic 3
Building and pushing the image
This chapter covers the build stage of the pipeline: turning your source code into a container image and pushing it to a registry, so the cluster can pull it later. It assumes you can read a GitHub Actions workflow file, know what a Dockerfile and a registry are, and have a Dockerfile at the root of your repository.
What the build job does
A push-based pipeline starts with a job that produces an artifact the cluster can run. For Kubernetes, that artifact is a container image stored in a registry. The deploy job comes later and only refers to the image by name and tag.
The build job needs to do three things:
- Check out the repository so the Dockerfile and source are on the runner.
- Log in to the registry with credentials that can push.
- Build the image and push it with a tag.
The build job
This example pushes to GitHub Container Registry (GHCR). It is the first job of the workflow built in this tutorial.
name: deploy
on:
push:
branches: [main]
permissions:
contents: read
packages: write
env:
IMAGE: ghcr.io/${{ github.repository }}
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ env.IMAGE }}:${{ github.sha }}How each part works
Permissions. The GITHUB_TOKEN is created for each workflow run. Its rights are set by the permissions block. packages: write lets it push to GHCR, and contents: read lets it check out the code. Anything you do not list is denied, so the token stays narrow.
Login. docker/login-action stores registry credentials on the runner for later steps. Using GITHUB_TOKEN means you do not create or store a separate registry password. If you push to another registry, such as Docker Hub or Amazon ECR, change registry, username, and password, and keep the password in a repository secret.
Build and push. docker/build-push-action runs the build with the context directory and uploads the result because push: true. With push: false, the image is built but not uploaded. That setting is useful for pull request checks.
Image name. github.repository has the form owner/repo. GHCR requires lowercase names. If your owner or repository name has capital letters, the push fails, so lowercase the name first.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `denied: permission_denied` on push | The `packages: write` permission is missing, or the package is linked to another repository | Add the permission and check the package's access settings |
| `invalid reference format` | Uppercase letters in the image name | Lowercase the owner and repository name |
| `unauthorized` during login | Wrong `registry` value or an expired secret | Check the registry host and rotate the secret |
| Cluster cannot pull the image later | The package is private and the cluster has no pull credentials | Make the package public, or add an `imagePullSecret` |
Why the build and deploy stages are separate jobs
Keeping the build in its own job lets the deploy job use needs: build. If the build fails, the deploy never starts. It also lets you attach protections, such as a required approval, to the deploy job only. The next chapter explains how the tag on this image identifies exactly which commit it came from.
Exercise
You change the workflow so the build step uses push: false. The job succeeds. Why will a later deploy of that image fail, and which single setting restores the push?
Show answer
With push: false the image is built only on the runner and is discarded when the job ends. It never reaches the registry, so the cluster has nothing to pull. Setting push: true on docker/build-push-action restores the upload.
Exercise
Your repository is named Acme/Shop-API. The push fails with invalid reference format. What is the cause?
Show answer
The image name ghcr.io/Acme/Shop-API contains uppercase letters, and registry repository names must be lowercase. Lowercase the owner and repository name, for example by computing the name in a shell step with tr '[:upper:]' '[:lower:]'.