About
GitHub Action to extract metadata from Git reference and GitHub events. This action
is particularly useful if used with Docker Build Push
action to tag and label Docker images.

Usage
Basic
name: ci
on:
workflow_dispatch:
push:
branches:
- 'master'
tags:
- 'v*'
pull_request:
branches:
- 'master'
jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Docker meta
id: meta
uses: docker/metadata-action@v6
with:
images: name/app
-
name: Login to DockerHub
if: github.event_name != 'pull_request'
uses: docker/login-action@v4
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
uses: docker/build-push-action@v7
with:
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
| Event |
Ref |
Docker Tags |
pull_request |
refs/pull/2/merge |
pr-2 |
push |
refs/heads/master |
master |
push |
refs/heads/releases/v1 |
releases-v1 |
push tag |
refs/tags/v1.2.3 |
v1.2.3, latest |
push tag |
refs/tags/v2.0.8-beta.67 |
v2.0.8-beta.67, latest |
workflow_dispatch |
refs/heads/master |
master |
Semver
name: ci
on:
push:
branches:
- 'master'
tags:
- 'v*'
pull_request:
branches:
- 'master'
jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Docker meta
id: meta
uses: docker/metadata-action@v6
with:
images: |
name/app
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
-
name: Login to DockerHub
if: github.event_name != 'pull_request'
uses: docker/login-action@v4
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
uses: docker/build-push-action@v7
with:
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
| Event |
Ref |
Docker Tags |
pull_request |
refs/pull/2/merge |
pr-2 |
push |
refs/heads/master |
master |
push |
refs/heads/releases/v1 |
releases-v1 |
push tag |
refs/tags/v1.2.3 |
1.2.3, 1.2, latest |
push tag |
refs/tags/v2.0.8-beta.67 |
2.0.8-beta.67 |
Bake definition
This action also handles a bake definition file that can be used with the
Docker Bake action. You just have to
declare an empty target named docker-metadata-action and inherit from it.
// docker-bake.hcl
target "docker-metadata-action" {}
target "build" {
inherits = ["docker-metadata-action"]
context = "./"
dockerfile = "Dockerfile"
platforms = [
"linux/amd64",
"linux/arm/v6",
"linux/arm/v7",
"linux/arm64",
"linux/386"
]
}
name: ci
on:
push:
branches:
- 'master'
tags:
- 'v*'
jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Docker meta
id: meta
uses: docker/metadata-action@v6
with:
images: |
name/app
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha
-
name: Build
uses: docker/bake-action@v7
with:
files: |
./docker-bake.hcl
cwd://${{ steps.meta.outputs.bake-file }}
targets: build
Content of ${{ steps.meta.outputs.bake-file }} file, combining tags and
labels, will look like this with refs/tags/v1.2.3 ref:
{
"target": {
"docker-metadata-action": {
"tags": [
"name/app:1.2.3",
"name/app:1.2",
"name/app:sha-90dd603",
"name/app:latest"
],
"labels": {
"org.opencontainers.image.title": "Hello-World",
"org.opencontainers.image.description": "This your first repo!",
"org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.source": "https://github.com/octocat/Hello-World",
"org.opencontainers.image.version": "1.2.3",
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision": "860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.licenses": "MIT"
},
"args": {
"DOCKER_META_IMAGES": "name/app",
"DOCKER_META_VERSION": "1.2.3"
}
}
}
}
You can also use the bake-file-tags and bake-file-labels outputs if you
just want to use tags and/or labels respectively. The following example is
similar to the previous one:
-
name: Build
uses: docker/bake-action@v7
with:
files: |
./docker-bake.hcl
cwd://${{ steps.meta.outputs.bake-file-tags }}
cwd://${{ steps.meta.outputs.bake-file-labels }}
targets: build
Customizing
inputs
The following inputs can be used as step.with keys:
[!NOTE]
List type is a newline-delimited string
labels: |
org.opencontainers.image.title=MyCustomTitle
org.opencontainers.image.description=Another description
org.opencontainers.image.vendor=MyCompany
| Name |
Type |
Description |
context |
String |
Where to get context data. Allowed options are: workflow (default), git, git:<path>. |
images |
List |
List of Docker images to use as base name for tags |
tags |
List |
List of tags as key-value pair attributes |
flavor |
List |
Flavor to apply |
labels |
List |
List of custom labels |
annotations |
List |
List of custom annotations |
sep-tags |
String |
Separator to use for tags output (default \n) |
sep-labels |
String |
Separator to use for labels output (default \n) |
sep-annotations |
String |
Separator to use for annotations output (default \n) |
bake-target |
String |
Bake target name (default docker-metadata-action) |
outputs
The following outputs are available:
| Name |
Type |
Description |
version |
String |
Docker image version |
tags |
String |
Docker tags |
tag-names |
String |
Docker tag names without image base name |
labels |
String |
Docker labels |
annotations |
String |
Annotations |
json |
String |
JSON output of tags and labels |
bake-file-tags |
File |
Bake file definition path with tags |
bake-file-labels |
File |
Bake file definition path with labels |
bake-file-annotations |
File |
Bake file definition path with annotations |
Alternatively, each output is also exported as an environment variable when DOCKER_METADATA_SET_OUTPUT_ENV is true:
DOCKER_METADATA_OUTPUT_VERSION
DOCKER_METADATA_OUTPUT_TAGS
DOCKER_METADATA_OUTPUT_LABELS
DOCKER_METADATA_OUTPUT_ANNOTATIONS
DOCKER_METADATA_OUTPUT_JSON
DOCKER_METADATA_OUTPUT_BAKE_FILE_TAGS
DOCKER_METADATA_OUTPUT_BAKE_FILE_LABELS
DOCKER_METADATA_OUTPUT_BAKE_FILE_ANNOTATIONS
So it can be used with our Docker Build Push action:
- uses: docker/build-push-action@v7
with:
build-args: |
DOCKER_METADATA_OUTPUT_JSON
environment variables
| Name |
Type |
Description |
DOCKER_METADATA_PR_HEAD_SHA |
Bool |
If true, set associated head SHA instead of commit SHA that triggered the workflow on pull request event |
DOCKER_METADATA_SHORT_SHA_LENGTH |
Number |
Specifies the length of the short commit SHA to ensure uniqueness. Default is 7, but can be increased for larger repositories. |
DOCKER_METADATA_ANNOTATIONS_LEVELS |
String |
Comma separated list of annotations levels to set for annotations output separated (default manifest) |
DOCKER_METADATA_SET_OUTPUT_ENV |
Bool |
If true, sets each output as an environment variable (default true) |
context input
context defines where to get context metadata:
# default
context: workflow
# or
context: git
Use context: git:<path> when the repository is checked out to a custom path.
The path can be absolute or relative to the current working directory
(normally $GITHUB_WORKSPACE):
- uses: actions/checkout@v7
with:
path: source
- uses: docker/metadata-action@v6
with:
context: git:source
images: name/app
tags: type=sha
The selected checkout supplies the Git ref, SHA, and commit date. Other repository
metadata still comes from the workflow repository.
images input
images defines a list of Docker images to use as base name for tags: