Skip to content
name: build Docker image on release
on:
release:
types: [published]
jobs:
build:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
-
# The release event is a superset of the push tag event,
# so we expect two tags to be generated by docker/metadata-action
# based on the behaviour for the type=semver,pattern=... tag type:
# 1. a semver tag vX.Y.Z (from the release Git tag; this is what will be used as the image version in the generated label)
# 2. latest (as type=semver,pattern=... is part of the latest tag handling)
# References:
# - https://github.com/docker/metadata-action?tab=readme-ov-file#typesemver
# - https://github.com/docker/metadata-action?tab=readme-ov-file#latest-tag
# - https://github.com/docker/metadata-action/issues/103
# - https://github.com/docker/metadata-action/issues/168
name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
# TODO: Repos with this wf must have DOCKERHUB_REPO set in the repository variables
images: ${{ secrets.DOCKERHUB_USERNAME }}/${{ vars.DOCKERHUB_REPO }}
# NOTE: This input is probably not necessary as the action should generate the appropriate tags
# based on the default type=ref,event=tag, but we define an explicit pattern here for clarity
tags: |
type=semver,pattern={{raw}}
-
name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
-
name: Build and push
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
# Select a subset of labels to apply from https://github.com/opencontainers/image-spec/blob/main/annotations.md#pre-defined-annotation-keys,
# following https://github.com/docker/metadata-action/issues/303
labels: |
org.opencontainers.image.created=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.source'] }}
org.opencontainers.image.revision=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}
org.opencontainers.image.version=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}