Sub topic 16
Putting it together: release and promotion
This chapter assumes you have read the earlier chapters on tagging, registry login, scanning and attestations. It combines them into one release flow. The key rule is to build an image once and then move that same image through environments.
The release flow
Each event in the repository triggers a different stage of the pipeline:
| Event | What the pipeline does | Tags pushed |
|---|---|---|
| Pull request | Build, test and scan. Do not push. | none |
| Push to `main` | Build, test, scan, then push. | `main`, `sha-<short>` |
| Tag `v1.4.0` | Same as a push to `main`, with version tags added. | `1.4.0`, `sha-<short>` |
| Manual promotion | Add a tag to an existing image. | for example `1.4.0` or `production` |
Gate the push
Publishing must depend on the checks. In a single job, step order does this: put the test and scan steps before the push step, and a failure stops the job. In a larger pipeline, split them into jobs and use needs:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: docker build --target build -t app-test .
- run: docker run --rm app-test npm test
publish:
needs: test
if: github.event_name != 'pull_request'
runs-on: ubuntu-latest
steps:
- run: echo "build, scan and push steps go here"If test fails, publish is skipped. Keep the scan step before the push so a vulnerable image never reaches the registry under a release tag.
Promote instead of rebuilding
Rebuilding for each environment can produce a different image. Base images move, and package mirrors change. Instead, promote the image you already tested. Promotion adds a new tag to an existing manifest, so the digest stays identical.
name: promote-image
on:
workflow_dispatch:
inputs:
source_tag:
description: Existing tag, for example sha-1a2b3c4
required: true
target_tag:
description: New tag, for example 1.4.0
required: true
permissions:
contents: read
packages: write
jobs:
promote:
runs-on: ubuntu-latest
env:
IMAGE: ghcr.io/${{ github.repository }}
SOURCE_TAG: ${{ inputs.source_tag }}
TARGET_TAG: ${{ inputs.target_tag }}
steps:
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Add tag to existing image
run: |
docker buildx imagetools create \
--tag "$IMAGE:$TARGET_TAG" \
"$IMAGE:$SOURCE_TAG"The inputs pass through environment variables instead of being written into the script. This stops a crafted input from injecting shell commands.
imagetools create copies the manifest in the registry. It does not pull or rebuild layers, and it keeps multi-platform manifests intact. Verify the result with docker buildx imagetools inspect. The digest should match the source tag.
Common errors
- Deploying a mutable tag.
latestormaincan change under you. Deploy by digest, such asimage@sha256:..., or by an immutable version tag. - Pushing before the scan. Order the steps so the scan runs first.
- Missing
packages: write. The promotion job fails with a permission error if the workflow lacks it. - Uppercase in the repository name. Registry image names must be lowercase. If your organization name has capitals, lowercase it before use.
Exercise
Why does the promotion workflow use docker buildx imagetools create instead of running docker build again with a new tag?
Show answer
Promotion should release the exact image that was already tested and scanned. imagetools create adds a tag to the existing manifest, so the digest is unchanged. A new build could pull different base image or package versions and produce a different image.