Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a tag to a Docker image if there's a git tag using GitHub Action

I'm using GitHub Actions to build a docker image using the build-push-action. I'd like to add tags to the image before pushing it to the docker registry:

  • Each image should be tagged with latest
  • If the commit that triggered the build has a git tag attached, the image should be tagged with this tag as well.

I have something along the lines of:

- name: Build and push
  id: docker_build
  uses: docker/build-push-action@v2
  with:
    context: .
    push: true
    tags:
      - user/image:latest

It would be easy to always add more tags, but I want to add it only if there's a git tag. Is there a way to do that?

like image 866
l7r7 Avatar asked Dec 05 '25 20:12

l7r7


2 Answers

There's a docker/metadata-action that does this. Make sure to set up the push tags trigger along with whatever other triggers you might need. The action docs have a lot more details about what tags it applies for each event trigger type. See also https://docs.github.com/en/actions/publishing-packages/publishing-docker-images for even more info on the general topic of Docker image publishing.

name: Tag Docker Image with Git

on:
  push:
    tags: [ v* ]

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - name: Checkout
      uses: actions/checkout@v2

    - name: Log into the Container registry
      uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
      with:
        registry: ${{ env.REGISTRY }}
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}

    - name: Extract metadata for the Docker image
      id: meta
      uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
      with:
        images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

    - name: Build and push the Docker image
      uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
      with:
        context: .
        push: true
        tags: ${{ steps.meta.outputs.tags }}
        labels: ${{ steps.meta.outputs.labels }}
like image 179
Kevin Condon Avatar answered Dec 10 '25 09:12

Kevin Condon


I found a solution using a dedicated action. I create the tags in a separate step and add it afterwards. The relevant parts of the CI config look like this:

- name: Docker meta
  id: docker_meta
  uses: crazy-max/ghaction-docker-meta@v1
  with:
    images: l7r7/sample
    tag-custom: latest
    tag-semver: |
      {{raw}}
- name: Build and push
  id: docker_build
  uses: docker/build-push-action@v2
  with:
    context: .
    push: false
    tags: ${{ steps.docker_meta.outputs.tags }}
    labels: ${{ steps.docker_meta.outputs.labels }}

To be precise, it's not exactly what I was asking for, because it will add a tag containing the branch name as well. But that's ok for me. I can live with that.

A full working example can be found here.

like image 23
l7r7 Avatar answered Dec 10 '25 10:12

l7r7