Sub topic 11
Generating tags in CI
Typing docker tag by hand works for a demo. In a team, a pipeline should compute the tags from the Git state, so the same commit always gets the same tags and nobody mistypes a version. This chapter assumes you can build and push images, you know Git tags and commits, and you know the tagging schemes from earlier chapters: semantic version tags, rolling major and minor tags, and commit SHA tags.
What CI needs to decide
A CI job knows two facts when it starts: which commit it is building and which Git ref triggered it. From those it can derive every tag.
| Trigger | Git ref | Tags worth producing |
|---|---|---|
| Push of tag `v1.4.2` | `refs/tags/v1.4.2` | `1.4.2`, `1.4`, `1`, `sha-<short>` |
| Push to `main` | `refs/heads/main` | `sha-<short>` only |
Release tags are created only when a release tag is pushed. Every other build gets only the SHA tag, so you can still trace it.
Using docker/metadata-action
In GitHub Actions, docker/metadata-action turns the Git ref into a list of tags. The docker/build-push-action step then builds the image once and applies every tag in that list. Because one build gets all the tags, every tag points at identical content, which is the same property you got by hand with docker tag.
Here is a complete workflow for a registry that needs a login.
name: release
on:
push:
tags:
- "v*"
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Check out source
uses: actions/checkout@v4
- name: Set up Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to registry
uses: docker/login-action@v3
with:
registry: registry.example.com
username: ${{ secrets.REGISTRY_USER }}
password: ${{ secrets.REGISTRY_TOKEN }}
- name: Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: registry.example.com/team/myapp
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}Each type= line is a rule:
type=semver,pattern={{version}}reads the Git tagv1.4.2and produces1.4.2. It strips the leadingv, because Docker tags and Git tags follow different conventions.type=semver,pattern={{major}}.{{minor}}produces the rolling tag1.4.type=shaproducessha-followed by the first seven characters of the commit SHA, for examplesha-abc1234.
The labels output adds OCI labels such as the source revision to the image. They give you a second way to trace a container back to its commit.
Behaviors that surprise people
The action has defaults that change what you get:
- latest is added automatically. By default, a stable semver tag push also produces
latest. Setflavor: latest=falseunderwith:if you do not want CI to movelatest. - Pre-releases are treated carefully. A Git tag like
v2.0.0-rc.1produces the full version tag. It does not produce the rolling2.0tag orlatest, so a release candidate cannot take over the stable tags. - Major tags skip 0.x versions.
{{major}}is not emitted for0.y.zversions, because in semver anything may change between 0.x releases. - The tag trigger matters. If the workflow only runs on
v*tags, thetype=sharule only ever runs for release builds. Add a second trigger onbranches: [main]if you want SHA-only builds from every merge.
Checking the result
After a run, read the pushed digest from the build step's output, or pull the image and inspect it. If the 1.4.2 and sha-abc1234 tags show the same digest, the pipeline applied them to one build. If they differ, something built twice.
Exercise
A pipeline runs on the Git tag v3.0.0-rc.1 with the three rules type=semver,pattern={{version}}, type=semver,pattern={{major}}.{{minor}}, and type=sha. Which tags does it push, and why is that a good outcome?
Show answer
It pushes 3.0.0-rc.1 and sha-<short commit>. The rolling 3.0 tag is skipped for a pre-release, and latest is not added. Anyone following 3.0 or latest keeps getting the stable release instead of an unfinished one.