TExcelTAD
Home › CI/CD Pipelines & Pipeline Security › A Build → Scan → Push → Deploy Workflow

A Build → Scan → Push → Deploy Workflow

This lesson assembles the section into the workflow most container services need: build the image once, scan it before anyone can pull it, push it, and deploy the pushed digest to staging and (after approval) production. The workflow is non-run; after it, the same sequence is rehearsed for real against a local registry, which is how you should test pipeline logic before wiring it into YAML.

The workflow

yaml
# .github/workflows/delivery.yml
name: delivery
on:
  push:
    branches: [main]
  pull_request:

permissions:
  contents: read

env:
  IMAGE: ghcr.io/${{ github.repository }}           # e.g. ghcr.io/acme/shop

jobs:
  test:
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v7
      - run: make lint test

  image:
    needs: test
    runs-on: ubuntu-24.04
    permissions:
      contents: read
      packages: write                               # push to GitHub Container Registry
    outputs:
      digest: ${{ steps.push.outputs.digest }}
    steps:
      - uses: actions/checkout@v7
      - uses: docker/setup-buildx-action@v4
      - uses: docker/metadata-action@v6
        id: meta
        with:
          images: ${{ env.IMAGE }}
          tags: |
            type=sha
            type=ref,event=branch

      # 1. build and load into the runner's Docker, not into the registry yet
      - uses: docker/build-push-action@v7
        with:
          context: .
          load: true
          tags: shop:candidate
          cache-from: type=gha
          cache-to: type=gha,mode=max

      # 2. scan the candidate; the gate fails the job before anything is pushed
      - uses: acme/pipelines/actions/setup-tools@v3   # pinned Trivy (see "Reusable Workflows")
      - name: Scan
        run: trivy image --severity HIGH,CRITICAL --ignore-unfixed --exit-code 1 shop:candidate
      - name: SBOM
        run: trivy image --format cyclonedx --output sbom.cdx.json shop:candidate

      # 3. push (main only); the layers come from the build cache, so this is fast
      - uses: docker/login-action@v4
        if: github.event_name == 'push'
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - uses: docker/build-push-action@v7
        id: push
        if: github.event_name == 'push'
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha
      - uses: actions/upload-artifact@v7
        if: always()
        with:
          name: sbom
          path: sbom.cdx.json

  staging:
    needs: image
    if: github.event_name == 'push'
    uses: acme/pipelines/.github/workflows/deploy.yml@v3
    with:
      environment: staging
      image: ghcr.io/${{ github.repository }}@${{ needs.image.outputs.digest }}
    secrets: inherit

  production:
    needs: [image, staging]
    uses: acme/pipelines/.github/workflows/deploy.yml@v3
    with:
      environment: production                       # reviewers approve here
      image: ghcr.io/${{ github.repository }}@${{ needs.image.outputs.digest }}
    secrets: inherit

The design decisions, in order:

  • Pull requests build and scan, but never push. Every PR gets the same gate as main; only main produces registry images.
  • Scan before push. An image that fails the gate never reaches the registry, so nothing can deploy it by accident. (Scanning after push is also common; then the deploy job must check the scan result.)
  • The digest is the contract. build-push-action outputs the pushed manifest's digest; both deployments receive image@sha256:…, and the shared deploy workflow refuses anything else.
  • The SBOM is kept as an artifact with if: always(), so it exists even when the gate fails. Attaching it to the image as a signed attestation is the next step (Attestations).
  • Separate permissions per job: only image can write packages, and only the deploy jobs see deployment secrets.

A local rehearsal

The same sequence, run for real with crane (to assemble and push images without Docker) and Trivy, against the registry on localhost:5000. Two candidates are built from the same application layer: one on an old Alpine base, one with nothing but the application files (like a distroless or scratch image). Only a candidate that passes the gate is pushed under its release tag and "deployed", by writing the digest into the deployment manifest:

bash · try it yourself
set -u
REG=localhost:5000
step() { printf '\n== %s\n' "$*"; }

step "build two candidates from one application layer"
mkdir -p rootfs/app
printf '#!/bin/sh\necho shop 1.4.0\n' > rootfs/app/server && chmod 755 rootfs/app/server
tar --sort=name --mtime=@"$SOURCE_DATE_EPOCH" --owner=0 --group=0 --numeric-owner -C rootfs -cf layer.tar .
crane push /opt/samples/images/alpine-3.18.0.tar $REG/base/alpine:3.18.0 > /dev/null 2>&1
crane append -b $REG/base/alpine:3.18.0 -f layer.tar -t $REG/ci/shop:alpine 2> /dev/null > /dev/null
crane append -f layer.tar -t $REG/ci/shop:static 2> /dev/null > /dev/null
echo "candidates in the CI-only repository: $(crane ls $REG/ci/shop | sort | tr '\n' ' ')"

gate() {   # gate CANDIDATE: HIGH/CRITICAL with a fix available fail the build
  trivy image -q --insecure "$REG/ci/shop:$1" --severity HIGH,CRITICAL --ignore-unfixed \
    --exit-code 1 -f json -o "scan-$1.json"
  local rc=$?
  echo "scan $1: exit $rc, $(jq '[.Results[]?.Vulnerabilities[]?] | length' "scan-$1.json") findings"
  return $rc
}

release() {   # release CANDIDATE: gate, then promote by digest and deploy
  step "pipeline for the $1 candidate"
  gate "$1" || { echo "gate failed: nothing pushed, nothing deployed"; return 1; }
  crane copy "$REG/ci/shop:$1" "$REG/shop:1.4.0" 2> /dev/null
  local digest; digest=$(crane digest "$REG/shop:1.4.0")
  printf 'image: %s/shop@%s\n' "$REG" "$digest" > deploy-staging.yaml
  echo "pushed $REG/shop:1.4.0 (${digest:0:19}...)"
  echo "deploy-staging.yaml -> $(cut -c1-45 deploy-staging.yaml)..."
}

release alpine
jq -r '[.Results[].Vulnerabilities[] | "\(.Severity) \(.PkgName) \(.InstalledVersion) -> \(.FixedVersion)"] | unique | .[]' scan-alpine.json
release static
echo
echo "release tags in the registry: $(crane ls $REG/shop | tr '\n' ' ')"

== build two candidates from one application layer
candidates in the CI-only repository: alpine static

== pipeline for the alpine candidate
scan alpine: exit 1, 9 findings
gate failed: nothing pushed, nothing deployed
CRITICAL busybox 1.36.0-r9 -> 1.36.1-r1
CRITICAL busybox-binsh 1.36.0-r9 -> 1.36.1-r1
CRITICAL ssl_client 1.36.0-r9 -> 1.36.1-r1
HIGH libcrypto3 3.1.0-r4 -> 3.1.4-r0
HIGH libcrypto3 3.1.0-r4 -> 3.1.7-r0
HIGH libssl3 3.1.0-r4 -> 3.1.4-r0
HIGH libssl3 3.1.0-r4 -> 3.1.7-r0
HIGH musl 1.2.4-r0 -> 1.2.4-r3
HIGH musl-utils 1.2.4-r0 -> 1.2.4-r3

== pipeline for the static candidate
scan static: exit 0, 0 findings
pushed localhost:5000/shop:1.4.0 (sha256:4d9e9b0ad0be...)
deploy-staging.yaml -> image: localhost:5000/shop@sha256:4d9e9b0ad0b...

release tags in the registry: 1.4.0

The run shows the whole policy working: the Alpine 3.18 candidate failed the gate on fixable vulnerabilities in the base image, so nothing was promoted or deployed; the minimal candidate passed, was copied from the CI repository to the release repository, and the deployment manifest received its digest. In a real pipeline the fix for the first candidate is a newer base image, which is exactly what Dependabot or Renovate pull requests for FROM lines provide (Dependabot, Renovate & Scorecard).

Each piece of the rehearsal has its own lesson: the gate flags in Container Scanning as a CI Gate, SBOMs in SBOMs in the Pipeline, and tags vs digests in Tags vs Digests.

Test yourself

Why does the workflow load the image locally, scan it, and only then push?

  1. Scanners can't read registries
  2. An image that fails the gate never reaches the registry, so nothing can deploy it
  3. Pushing first is slower
  4. GHCR requires it

What do the staging and production jobs receive to deploy?

  1. The latest tag
  2. The image reference pinned by the digest pushed in the image job
  3. The Dockerfile
  4. The SBOM

In the rehearsal, why did the alpine candidate fail even with --ignore-unfixed?

  1. The layer was corrupt
  2. Its base image has HIGH/CRITICAL vulnerabilities for which fixed package versions exist
  3. Trivy was offline
  4. The tag was wrong