Sub topic 8
Generating tags and labels automatically
This chapter assumes you can read a GitHub Actions workflow and know the difference between a branch, a tag, and a commit SHA. It also assumes you have seen the build-and-push workflow from the previous chapters.
Why generate tags instead of typing them
A hard-coded tag such as latest tells you nothing about which commit produced the image. Typing a new tag by hand on every release invites mistakes. The docker/metadata-action step reads the Git event that triggered the workflow and computes tags and labels from it. The build step then consumes its outputs.
The metadata step
This step comes before the build step in the workflow:
- name: Generate tags and labels
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=shaThe id: meta line lets later steps read steps.meta.outputs.tags and steps.meta.outputs.labels. Each line under tags is a rule. A rule produces a tag only when its event matches.
| Event | Rule that matches | Resulting tag |
|---|---|---|
| Push to `main` | `type=ref,event=branch` | `main` |
| Pull request #12 | `type=ref,event=pr` | `pr-12` |
| Push of tag `v1.4.0` | `type=semver,pattern={{version}}` | `1.4.0` |
| Any event | `type=sha` | `sha-` plus the short commit SHA |
The sha rule matches every event. Every image therefore gets an immutable-looking tag that points back to one commit.
Labels
The action also builds OCI labels, which are metadata stored in the image config. By default it sets org.opencontainers.image.source, revision, created, version, and a few others. Registries such as GitHub Container Registry use the source label to link the package to its repository. You pass the labels to the build step like this:
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}The latest tag
By default the action adds latest only when a Git tag triggers the run. Pushes to main do not get it. If you want latest to follow main, add type=raw,value=latest,enable={{is_default_branch}}. Decide this on purpose, because anyone pulling latest gets whatever you moved it to.
Common errors
- Empty tags on a pull request. If no rule matches the event, the
tagsoutput is empty. Addtype=ref,event=pror rely ontype=sha. - No version tag on a tag push.
type=semverneeds a valid semantic version, so tagv1.4.0works andrelease-finaldoes not. Check that your trigger includestags: ["v*"]. - Wrong image name case. Registry names must be lowercase.
github.repositorycan contain capitals, but the metadata action lowercases the image name for you. Plain string concatenation in your own steps does not.
Exercise
A workflow uses the rules type=ref,event=branch, type=semver,pattern={{version}}, and type=sha. You push the Git tag v2.0.1. Which tags does the image receive, ignoring latest?
Show answer
It receives 2.0.1 from the semver rule and sha- plus the short commit SHA from the sha rule. The branch rule does not match, because a tag push is not a branch push.