$ run as a normal user# run as root (or with sudo)
The quick answer
name: image
on:
push:
branches: [main]
tags: ["v*"]
permissions:
contents: read
packages: write
jobs:
build:
runs-on: ubuntu-24.04
timeout-minutes: 30
steps:
- uses: actions/checkout@v5
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=sha
type=semver,pattern={{version}}
- id: build
uses: docker/build-push-action@v6
with:
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- run: echo "digest=${{ steps.build.outputs.digest }}"Step by step
- Login without a stored password —
GITHUB_TOKENwithpackages: writecan push to GHCR. For other registries use a robot account in an environment secret (or OIDC for ECR/ACR). - Good tags from
metadata-action:
| Tag | When |
|---|---|
sha-3f9c2e1 | Every build — traceable to the commit. |
1.4.2 | When you push the Git tag v1.4.2. |
latest | Avoid for deployments (it moves). |
- Scan before you trust it — build, scan, then push (or block the deploy on findings):
- uses: docker/build-push-action@v6
with:
load: true
tags: scan-me:${{ github.sha }}
- run: trivy image --exit-code 1 --severity CRITICAL --ignore-unfixed scan-me:${{ github.sha }}- Deploy by digest — the
digestoutput (sha256:…) is what your deploy repo / Helm values should use; tags can change, digests can not. - Provenance and SBOM — build-push-action can attach build provenance and an SBOM (
provenance: true,sbom: true); sign with Cosign keyless for verification in the cluster. - Pull requests: build (and scan) but do not push —
push: ${{ github.event_name != 'pull_request' }}.
Check that it worked
The run is green, the package page shows the image with sha-… (and the version on tags), and:
docker buildx imagetools inspect ghcr.io/ORG/shop:sha-3f9c2e1shows the same digest as the job output.
Common problems
| Problem | Cause | Fix |
|---|---|---|
denied: permission_denied: write_package | Missing packages: write, or the package belongs to another repo. | Add the permission; link the package to this repo in its settings. |
| Image name rejected | Uppercase letters in github.repository. | Use a lower-case name (images: ghcr.io/org/shop). |
| Every build is slow | No layer cache. | cache-from/to: type=gha; order the Dockerfile well. |
| Fork PRs fail to push | No secrets/token write rights for forks. | Do not push on PRs. |
| Deploy got another image than tested | Deployed by tag that moved. | Deploy by digest. |
failed to solve: … not found | Wrong build context or Dockerfile path. | context: and file: inputs. |
Go further
- Command pages: docker, gh, trivy.
- Next how-tos: Cache dependencies in CI, Scan a container image, Sign and verify an image with Cosign, Build images for ARM and x86, Promote a release from staging to production.
- Learn it: GitHub Actions — Build an image, Provenance.
- Practise: Lab: build a CI pipeline — test, scan, build an image and gate the merge.
1Why deploy by digest instead of tag?
A tag can be moved to another image; a digest always means exactly the image that was built and tested.
2Which permission lets a workflow push to GHCR with GITHUB_TOKEN?
packages: write.