docker / build-push-action

GitHub Action to build and push Docker images with Buildx
https://github.com/marketplace/actions/build-and-push-docker-images
Apache License 2.0
4.41k stars 561 forks source link

Add "latest" tag when condition met #1215

Closed marcwittke closed 3 months ago

marcwittke commented 3 months ago

Description

Requirement:

when the build is running on main, tag the resulting image as :latest additionally

Current solution:

I duplicated the step with two sets of tags, using github's conditions to choose one of them:

- name: Build and push (branch)
  if: github.ref != 'refs/heads/main'        # <==========================
  uses: docker/build-push-action@v5.3.0
  with:
    context: app
    push: true
    tags: |
      ghcr.io/repo/app:${{ inputs.tag }}

- name: Build and push (main)
  if: github.ref == 'refs/heads/main'
  uses: docker/build-push-action@v5.3.0
  with:
    context: app
    push: true
    tags: |
      ghcr.io/repo/app:${{ inputs.tag }}
      ghcr.io/repo/app:latest                  # <==========================

This adds a lot of duplication, since I left out build args and caching for simplicity, but in the real world there's a list of stuff being duplicated.

My wish:

- name: Build and push
  uses: docker/build-push-action@v5.3.0
  with:
    context: app
    push: true
    latest: {{ github.ref == 'refs/heads/main' }}        # <==========================
    tags: |
      ghcr.io/repo/app:${{ inputs.tag }}

I know the latest tag is nothing special but it's common convention nowadays. It enables me to have a docker-compose manifest that just uses the latest tag.

crazy-max commented 3 months ago

You can use the metadata-action for this, see https://github.com/docker/metadata-action?tab=readme-ov-file#latest-tag