Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 151CI/CD · Containers

How to build and push an image in CI

Images built on a laptop are not reproducible and not trusted. The standard is: CI builds the image from the Git commit, scans it, pushes it with a clear tag (commit SHA and version), and records the digest for deployment. This how-to does it in GitHub Actions with GHCR (GitHub's registry) and the official Docker actions — no stored registry password, just the job's token.

  • 5 Lessons
  • 3 Commands

$ run as a normal user# run as root (or with sudo)

151.1

The quick answer

.github/workflows/image.yml
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 }}"

151.2

Step by step

  1. Login without a stored password — GITHUB_TOKEN with packages: write can push to GHCR. For other registries use a robot account in an environment secret (or OIDC for ECR/ACR).
  2. Good tags from metadata-action:
TagWhen
sha-3f9c2e1Every build — traceable to the commit.
1.4.2When you push the Git tag v1.4.2.
latestAvoid for deployments (it moves).
  1. Scan before you trust it — build, scan, then push (or block the deploy on findings):
.github/workflows/image.yml (scan step)
      - 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 }}
  1. Deploy by digest — the digest output (sha256:…) is what your deploy repo / Helm values should use; tags can change, digests can not.
  2. 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.
  3. Pull requests: build (and scan) but do not push — push: ${{ github.event_name != 'pull_request' }}.

151.3

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-3f9c2e1

shows the same digest as the job output.

151.4

Common problems

ProblemCauseFix
denied: permission_denied: write_packageMissing packages: write, or the package belongs to another repo.Add the permission; link the package to this repo in its settings.
Image name rejectedUppercase letters in github.repository.Use a lower-case name (images: ghcr.io/org/shop).
Every build is slowNo layer cache.cache-from/to: type=gha; order the Dockerfile well.
Fork PRs fail to pushNo secrets/token write rights for forks.Do not push on PRs.
Deploy got another image than testedDeployed by tag that moved.Deploy by digest.
failed to solve: … not foundWrong build context or Dockerfile path.context: and file: inputs.

151.5

Go further

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.

All commands in this chapter