Automated Docker Builds in GitHub Actions
Build and push images from GitHub Actions workflows on every PR and release.
In plain words
Treat every image as build provenance: source SHA, workflow identity, platform, tag/digest, SBOM and scan result should connect.
Think of it like…
The workflow is an assembly line with the Git commit as the work order and the emitted digest as the sealed serial number.
Where you’ve seen it
In practice, uses: docker/build-push-action@v6; with: context=., push=${{ github.event_name != "pull_request" }}, tags=ghcr.io/acme/api:${{ github.sha }} is the concrete move for this job: build and push images from GitHub Actions workflows on every PR and release. The release is not accepted until inspect the step summary digest and run a no-push PR from a fork, then verify the release branch has OIDC read/package write only confirms the result.
Words you’ll hear
- GitHub Actions
- The implementation control in
uses: docker/build-push-action@v6; with: context=., push=${{ github.event_name != "pull_request" }}, tags=ghcr.io/acme/api:${{ github.sha }}for build and push images from GitHub Actions workflows on every PR and release. - Build
- The result to observe with
inspect the step summary digest and run a no-push PR from a fork, then verify the release branch has OIDC read/package write only, rather than infer from a successful command alone. - Push
- The production boundary exposed when exposing registry write secrets to fork PRs, or tagging by branch only and losing the source SHA in image metadata.
Visual diagram
Theory & deep dive
GitHub Actions control path
The concrete mechanism is uses: docker/build-push-action@v6; with: context=., push=${{ github.event_name != "pull_request" }}, tags=ghcr.io/acme/api:${{ github.sha }}. The observable result is inspect the step summary digest and run a no-push PR from a fork, then verify the release branch has OIDC read/package write only; if those disagree, inspect the runtime evidence before changing unrelated settings.
Build and push images from GitHub Actions workflows on every PR and release. Keep GitHub Actions, Build and Push in the same deployment record so the intent and the observed state cannot drift apart.
uses: docker/build-push-action@v6; with: context=., push=${{ github.event_name != "pull_request" }}, tags=ghcr.io/acme/api:${{ github.sha }}
inspect the step summary digest and run a no-push PR from a fork, then verify the release branch has OIDC read/package write only, not with command success alone.Push failure boundary
A public fork pull_request can contain hostile Dockerfile code, so privileged credentials and --privileged runners are never passed to untrusted PRs. Mutable actions/tags and missing permissions: contents: read make the supply chain harder to audit. The narrow failure to design for is exposing registry write secrets to fork PRs, or tagging by branch only and losing the source SHA in image metadata.
Accept this compromise explicitly: build-push-action simplifies standard metadata/cache; a custom script gives flexibility but must reproduce provenance and least-privilege semantics. The controlled check-inspect the step summary digest and run a no-push PR from a fork, then verify the release branch has OIDC read/package write only-also identifies the point where a different design is safer.
Exercise this failure safely: exposing registry write secrets to fork PRs, or tagging by branch only and losing the source SHA in image metadata.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: A public fork pull_request can contain hostile Dockerfile code, so privileged credentials and --privileged runners are never passed to untrusted PRs. Mutable actions/tags and missing permissions: contents: read make the supply chain harder to audit.
- Intent
- GitHub Actions
When to use it & trade-offs
What it is
A Docker workflow uses a runner, checkout, QEMU/buildx setup, short-lived registry authentication and build-push-action to emit a reviewed tag plus digest. Pull requests build without push or use permissions-bound preview repositories; releases push from a protected workflow.
Why it matters: A public fork pull_request can contain hostile Dockerfile code, so privileged credentials and --privileged runners are never passed to untrusted PRs. Mutable actions/tags and missing permissions: contents: read make the supply chain harder to audit.
When to reach for it
- The fit is strongest for this job: build and push images from GitHub Actions workflows on every PR and release. Pair it with
inspect the step summary digest and run a no-push PR from a fork, then verify the release branch has OIDC read/package write onlyin the runbook. - Production GitHub Actions changes should make Build observable through
uses: docker/build-push-action@v6; with: context=., push=${{ github.event_name != "pull_request" }}, tags=ghcr.io/acme/api:${{ github.sha }}, rather than trusting the runtime default. - Keep a rollback example for the case where exposing registry write secrets to fork PRs, or tagging by branch only and losing the source SHA in image metadata.
When to avoid or be careful
- Do not accept command success while the system can still fail by exposing registry write secrets to fork PRs, or tagging by branch only and losing the source SHA in image metadata.
- Avoid erasing Push protection merely to dodge this cost: “build-push-action simplifies standard metadata/cache; a custom script gives flexibility but must reproduce provenance and least-privilege semantics”.
- Do not replace the evidence path-
inspect the step summary digest and run a no-push PR from a fork, then verify the release branch has OIDC read/package write only-with a screenshot or a mutable tag.
uses: docker/build-push-action@v6; with: context=., push=${{ github.event_name != "pull_request" }}, tags=ghcr.io/acme/api:${{ github.sha }}: practical choices
Strengths: A Docker workflow uses a runner, checkout, QEMU/buildx setup, short-lived registry authentication and build-push-action to emit a reviewed tag plus digest. Pull requests build without push or use permissions-bound preview repositories; releases push from a protected workflow.
Watch out: build-push-action simplifies standard metadata/cache; a custom script gives flexibility but must reproduce provenance and least-privilege semantics.
Strengths: It can begin with uses: docker/build-push-action@v6; with: context=., push=${{ github.event_name != "pull_request" }}, tags=ghcr.io/acme/api:${{ github.sha }} and fewer surrounding controls.
Watch out: It silently accepts exposing registry write secrets to fork PRs, or tagging by branch only and losing the source SHA in image metadata, which makes the result hard to reproduce after promotion.
Strengths: A public fork pull_request can contain hostile Dockerfile code, so privileged credentials and --privileged runners are never passed to untrusted PRs. Mutable actions/tags and missing permissions: contents: read make the supply chain harder to audit.
Watch out: The platform adds an ownership and debugging layer; retain inspect the step summary digest and run a no-push PR from a fork, then verify the release branch has OIDC read/package write only as the direct check.
Interview signals & numbers to know
When this topic comes up
- uses: docker/build-push-action@v6; with: context=., push=${{ github.event_name != "pull_request" }}, tags=ghcr.io/acme/api:${{ github.sha }}
- inspect the step summary digest and run a no-push PR from a fork, then verify the release branch has OIDC read/package write only
- Failure signature: exposing registry write secrets to fork PRs, or tagging by branch only and losing the source SHA in image metadata
- Trade-off: build-push-action simplifies standard metadata/cache; a custom script gives flexibility but must reproduce provenance and least-privilege semantics.
Benchmarks & rules of thumb
- 1 digest output
- primary release artifact ID
- 2 trust contexts
- untrusted PR and protected release differ
- contents: read
- example minimal source permission
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
uses: docker/build-push-action@v6; with: context=., push=${{ github.event_name != "pull_request" }}, tags=ghcr.io/acme/api:${{ github.sha }}as proof and skippinginspect the step summary digest and run a no-push PR from a fork, then verify the release branch has OIDC read/package write only. - Exposing registry write secrets to fork PRs, or tagging by branch only and losing the source SHA in image metadata
- Optimizing GitHub Actions without accepting that build-push-action simplifies standard metadata/cache; a custom script gives flexibility but must reproduce provenance and least-privilege semantics.
Likely interviewer follow-ups
- Why does
uses: docker/build-push-action@v6; with: context=., push=${{ github.event_name != "pull_request" }}, tags=ghcr.io/acme/api:${{ github.sha }}address GitHub Actions here rather than through application code? - How would you distinguish healthy Build from the failure “exposing registry write secrets to fork PRs, or tagging by branch only and losing the source SHA in image metadata” using
inspect the step summary digest and run a no-push PR from a fork, then verify the release branch has OIDC read/package write only? - When would this trade-off-“build-push-action simplifies standard metadata/cache; a custom script gives flexibility but must reproduce provenance and least-privilege semantics”-justify a different Push design?
uses: docker/build-push-action@v6; with: context=., push=${{ github.event_name != "pull_request" }}, tags=ghcr.io/acme/api:${{ github.sha }}-and one proof-inspect the step summary digest and run a no-push PR from a fork, then verify the release branch has OIDC read/package write only. Then name the boundary: exposing registry write secrets to fork PRs, or tagging by branch only and losing the source SHA in image metadata.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Automated Docker Builds in GitHub Actions with generative AI interview Q&A and hands-on practice.