Test and Build a Docker Image with GitHub Actions
You open a pull request, and a reviewer asks two things: do the tests pass, and does the image still build? Nobody should have to check by hand. In this lab you build a Docker image with GitHub Actions the safe way: a test job runs first, a build job packs the app only if the tests pass, and a pull request never publishes anything.
Job order: build only after the tests pass
As lesson 12 showed, each job runs on its own fresh runner, and a workflow's jobs start together unless you link them. needs: links them: a job with needs: test waits until the job called test has finished successfully.
If the needed job fails or is skipped, GitHub skips every job that needs it, unless that job sets a condition such as if: always(). That is the tasting rule: no image is built from code that failed its tests.
This lab's workflow runs on a push to main and on a pull request, the proposed change from lesson 1. Nobody has reviewed a pull request's code yet, so its run may build and check the image but must not publish it. The graph shows both lanes.
In this lab nothing is published at all, on either lane; pushing from main to a registry comes in lesson 15.
Lab step 1: copy the workflow and run the tests
The folder course-labs/ci-cd-and-devops-fundamentals/02-docker-build holds a tiny Python app: app/greeting.py prints the line in app/message.txt, and three tests in tests/test_greeting.py check it with Python's built-in unittest. Beside them sit a Dockerfile and the workflow, docker-build.yml. In your fork from lesson 12, remove hello.yml if it is still there, then copy the workflow in from the top of your clone:
mkdir -p .github/workflows
cp ci-cd-and-devops-fundamentals/02-docker-build/docker-build.yml .github/workflows/
git add .github/workflows/docker-build.yml
git commit -m "add test and build workflow"
git push
The push starts a run. Its test job checks out your code, installs Python with actions/setup-python and runs the tests; working-directory: sets the folder a run: step starts in:
test:
runs-on: ubuntu-latest
steps:
- name: Check out the code
uses: actions/checkout@v7
- name: Set up Python
uses: actions/setup-python@v7
with:
python-version: "3.12"
- name: Run the tests
working-directory: ci-cd-and-devops-fundamentals/02-docker-build
run: python -m unittest discover -s tests -v
Keep the quotes: YAML reads an unquoted 3.10 as the number 3.1. In the Actions tab, open Test and build and the newest run. You should see test and build joined by a line, with build starting only after test gets its green tick. The Run the tests log lists three tests ending in ... ok, then Ran 3 tests and OK. Either Start directory is not importable: 'tests' or No module named 'app' means a wrong working-directory: line.
Now watch the gate hold. Add a second line to app/message.txt, run git commit -am "break the one-line rule" and push. The test job fails with FAILED (failures=1), and build is skipped, so that commit gets no image. Undo it with git revert --no-edit HEAD and push again.
Lab step 2: build the image, but don't publish it
docker/setup-buildx-action sets up Buildx, Docker's build tool, with a BuildKit builder (Docker's build engine) running in a container on the runner, a kind of builder that can export its cache. docker/build-push-action then builds the image, and pushes it only if told to:
build:
needs: test
runs-on: ubuntu-latest
steps:
- name: Check out the code
uses: actions/checkout@v7
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Build the image
uses: docker/build-push-action@v7
with:
context: ci-cd-and-devops-fundamentals/02-docker-build
tags: sample-app:${{ github.sha }}
load: true
push: false
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Smoke test the image
env:
IMAGE: sample-app:${{ github.sha }}
run: |
echo "Running $IMAGE"
docker run --rm "$IMAGE"
context: is the build context, the folder whose files the build can copy, which also holds the Dockerfile. push: false keeps the image off every registry, and load: true loads it into the runner's Docker for the next step.
That step is a smoke test, lesson 8's quick check that the app basically works: here, that the image starts and prints its greeting. env: gives it a variable, and | starts a block of commands. Its log should show Running sample-app: with a 40-character SHA, then Hello from the sample app.
A pull request must never push, because a pushed image can be pulled and deployed like a reviewed one. A job that does publish usually writes push: ${{ github.event_name != 'pull_request' }}: true for a push to main, false for a pull request. The flowchart follows one run.
Because the publish decision comes last, every pull request still proves that its image builds and starts.
Lab step 3: tag with the commit SHA and cache the layers
tags: sample-app:${{ github.sha }} tags the image with the full commit SHA from lesson 6, so every image traces back to its commit; git rev-parse HEAD prints the same SHA on your laptop. On a pull request, though, GitHub tests a temporary merge of your branch into main, and github.sha is that merge commit, not your branch's last commit.
The build cache reuses a step's layer while that step and every step before it are unchanged, but each run starts on an empty runner. cache-to: type=gha saves the layers in the GitHub Actions cache, and cache-from: type=gha loads them next time. mode=max also saves layers that never reach the final image, such as those of earlier build stages.
The lab's Dockerfile copies app/, the part you edit most, last. Change the text in app/message.txt, keeping one line, then run git commit -am "change the greeting" and push. The new run's Build the image log shows (trimmed; the # numbers vary):
#7 [2/4] WORKDIR /opt/sample-app
#7 CACHED
#8 [3/4] COPY tests/ tests/
#8 CACHED
#9 [4/4] COPY app/ app/
#9 DONE 0.0s
Steps 2 and 3 came from the cache; only step 4 ran, because its files changed, and the smoke test prints your new greeting.
Reproduce the CI build on your laptop
A CI failure is easier to fix on your laptop, and the build job does nothing your laptop cannot: it runs the same Dockerfile on the same folder, as the try-it shows. To repeat the test job as well, run its command inside the image, which carries the tests:
docker run --rm sample-app:local python -m unittest discover -s tests -v
Words after the image name replace its CMD, so you should see the same three ... ok lines and OK, without installing Python. Real services usually keep tests out of the final image with a multi-stage build.
Alike is not identical: the two Pythons can differ in the patch number after 3.12, an Apple silicon Mac builds for arm64 while ubuntu-latest builds for x86-64, and each image records its build time, so the digests differ. So promote the CI-built image itself, as lesson 6 said.
Check yourself
A teammate deletes needs: test to make the pipeline faster. What can now go wrong?
Both jobs start together, so an image is built, and in a publishing pipeline pushed, even when the tests fail. Only needs: makes a red test job skip the build.
You edited only app/message.txt, yet the second run's log shows no CACHED step. What are two likely causes?
A cache line is missing, or the cache was emptied, for example after 7 days unused. Each runner starts empty, so nothing is reused unless the GitHub Actions cache supplies it.
Why build the image on a pull request at all, if it is never pushed?
So a broken Dockerfile or a failing smoke test shows up during review, before the merge. Building checks the package; only pushing would publish unreviewed code.