metadata-action icon indicating copy to clipboard operation
metadata-action copied to clipboard

Flavor is ignored in output, despite being parsed properly

Open fardog opened this issue 2 years ago • 7 comments

Behaviour

Even though I've specified a flavor which contains a prefix, and it is parsed properly, it doesn't result in a prefix on the docker tags which are produced. This prefix is being used in a matrix to allow two different packages to be built into images. Apologies if I'm missing something obvious (it feels like I must be?).

I'm using the following flavor:

          flavor: |
            latest=auto
            prefix=${{ matrix.package }}-,onlatest=true
            suffix=

I've tried this with a few different combinations, including both with tags specified and without, as well as messing with the onlatest, but none of this appears to change the behavior.

Thanks tons!

Steps to reproduce this issue

  1. Configure the workflow with a flavors defined, which specifies a prefix
  2. Run the action
  3. Observe incorrect output

Expected behaviour

Docker tags should be generated with a prefix; in the case of the above, it should be ghcr.io/fardog/primebot:twitter-pr-12.

Actual behaviour

Docker tags ignore the prefix; e.g. ghcr.io/fardog/primebot:pr-12

Configuration

  • Repository URL (if public): https://github.com/fardog/primebot/pull/12
  • Build URL (if public): https://github.com/fardog/primebot/actions/runs/4332624631/jobs/7565241206

The repository URL contains a few different attempts within that PR, none of the configs I attempted were successful however.

name: Docker Image

on:
  push:
    branches: 
      - master
    tags:
  pull_request:

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

jobs:
  build-and-push-image:
    strategy:
      matrix:
        package: ["twitter", "mastodon"]
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      - uses: actions/checkout@v3

      - uses: docker/login-action@v2
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - id: meta
        uses: docker/metadata-action@v4
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
          flavor: |
            latest=auto
            prefix=${{ matrix.package }}-,onlatest=true
            suffix=
          tags: |
            type=schedule
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern={{major}}
            type=ref,event=branch
            type=ref,event=pr
            type=sha

      - name: Build and push Docker image
        uses: docker/build-push-action@v4
        with:
          context: .
          file: Dockerfile-${{ matrix.package }}
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

Logs

logs_32.zip

Attached; also here is a screenshot of relevant output.

image

fardog avatar Mar 04 '23 20:03 fardog

Yes indeed. type=sha already has a default prefix sha- and type=ref,event=pr too with pr- prefix. Therefore a global prefix can't be applied atm.

I think we could allow a global prefix being set if value is empty, like:

      - id: meta
        uses: docker/metadata-action@v4
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
          flavor: |
            latest=auto
            prefix=${{ matrix.package }}-,onlatest=true
            suffix=
          tags: |
            type=ref,event=pr,prefix=
            type=sha,prefix=

WDYT?

As a workaround you can set the same prefix for type=sha and type=ref,event=pr:

      - id: meta
        uses: docker/metadata-action@v4
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
          flavor: |
            latest=auto
            prefix=${{ matrix.package }}-,onlatest=true
            suffix=
          tags: |
            type=ref,event=pr,prefix=${{ matrix.package }}-
            type=sha,prefix=${{ matrix.package }}-

crazy-max avatar Mar 20 '23 13:03 crazy-max

oh i see; yes, it was unexpected that sha- and pr- were already prefixes, so while i was expecting my prefix value to be written to all output, it was getting overwritten by this implicit default.

i think your workaround is probably a better solution than requiring the empty prefix, at least as it stands today. i think what i'd prefer is for the prefix to only be user-provided, and ref and sha to be instead part of the tag, so the prefix works as expected across all tags; i think the implicit prefix of the ref and sha creates unexpected behavior.

as this is my first use of this action though, i'm probably not best qualified to decide what's expected or unexpected, just my opinion. your workaround has helped me understand the problem and solved my issue; I super appreciate it. i'll leave this ticket open in case it's something you want to address, or feel free to close if not.

fardog avatar Mar 26 '23 01:03 fardog

@fardog I think I will do another implementation using a mode attr to prefix/suffix flavors like:

      - id: meta
        uses: docker/metadata-action@v4
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
          flavor: |
            latest=auto
            prefix=${{ matrix.package }}-,onlatest=true,mode=replace
            suffix=
          tags: |
            type=ref,event=pr
            type=sha

mode could be auto, merge, replace (defaults to auto):

  • auto: current behavior, set prefix/suffix if not already set in a tags entry
  • merge: if prefix/suffix is already set, then append (suffix) / prepend (prefix)
  • replace: if prefix/suffix is already set, then replace it with global one

WDYT?

crazy-max avatar Oct 25 '23 13:10 crazy-max

@crazy-max this seems like a very reasonable way to let someone pick the behavior that they want; looks good to me!

fardog avatar Oct 27 '23 13:10 fardog

I was expecting the flavor prefix to show up before the tag prefix:

      - uses: docker/metadata-action@v5
        id: meta
        with:
          flavor: |
            prefix=${{ matrix.distro }}-${{ matrix.edition }}-${{ matrix.arch }}-,onlatest=true
          images: |
            name=ghcr.io/${{ github.repository }},enable=${{ github.event_name == 'pull_request' && 'true' || 'false' }}
            name=${{ github.repository }},enable=${{ github.event_name != 'pull_request' && 'true' || 'false' }}
          tags: |
            type=ref,event=pr
            type=ref,event=tag,enable=${{  github.event.release.prerelease && 'true' || 'false' }},suffix=-pre
            type=ref,event=tag,enable=${{ !github.event.release.prerelease && 'true' || 'false' }}

Would yield something like:

for a pr: ubuntu-focal-arm64-pr-6 for a pre-release: ubuntu-focal-arm64-v3.0.0-pre for a release: ubuntu-focal-arm64-v3.0.0 and ubuntu-focal-arm64-latest

jeking3 avatar May 27 '24 14:05 jeking3