Optimize Your CI/CD Pipeline
Get instant insights into your CI/CD performance and costs. Reduce build times by up to 45% and save on infrastructure costs.
name: 'Usage of metadata-action GitHub Action'
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: name/app
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
Metadata Action
GitHub Action to extract metadata (tags, labels) from Git reference and GitHub events for Docker
What is Metadata GitHub Action?
The Docker Metadata GitHub Action fetches metadata from Git references and GitHub events. It is very helpful if used in combination with Docker action Build Push, which is also helpful in tagging and labeling Docker images.
How to Use the Docker Metadata GitHub Action with Semantic Versioning
The Docker Metadata GitHub Action is designed to extract metadata from Git references and GitHub events. This action is particularly useful when used in conjunction with the Docker Build Push action to tag and label Docker images. This guide includes an example of using semantic versioning (semver) to tag Docker images.
name: ci
on:
push:
branches:
- 'master'
tags:
- 'v*'
pull_request:
branches:
- 'master'
jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Docker meta
id: meta
uses: docker/metadata-action@v5
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@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
How to Use the Docker Metadata GitHub Action with Bake Definitions
Additionally, it handles a bake definition file that can be used with the Docker Bake action.
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: Checkout
uses: actions/checkout@v4
-
name: Docker meta
id: meta
uses: docker/metadata-action@v5
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@v4
with:
files: |
./docker-bake.hcl
${{ steps.meta.outputs.bake-file }}
targets: build
How to Specify Context
Specifies where to get context data. Allowed options are: workflow
(default), git
.
with:
context: git
How to Specify Images
List of Docker images to use as base names for tags. List type is a newline-delimited string.
with:
images: |
name/app
name/another-app
How to Specify Tags
List of tags as key-value pair attributes. List type is a newline-delimited string.
with:
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
How to Specify Flavor
Flavor to apply. List type is a newline-delimited string.
with:
flavor: |
latest
debug
How to Specify Labels
List of custom labels. List type is a newline-delimited string.
with:
labels: |
org.opencontainers.image.title=MyCustomTitle
org.opencontainers.image.description=Another description
org.opencontainers.image.vendor=MyCompany
How to Specify Annotations
List of custom annotations. List type is a newline-delimited string.
with:
annotations: |
org.opencontainers.image.source=https://github.com/myrepo
org.opencontainers.image.documentation=https://docs.example.com
How to Specify Tag Separator
Separator to use for tags output (default \n).
with:
sep-tags: ','
How to Specify Label Separator
Separator to use for labels output (default \n).
with:
sep-labels: ';'
How to Specify Annotation Separator
Separator to use for annotations output (default \n).
with:
sep-annotations: ';'
How to Specify Bake Target
Bake target name (default docker-metadata-action).
with:
bake-target: my-custom-target