docker / metadata-action

GitHub Action to extract metadata (tags, labels) from Git reference and GitHub events for Docker
https://github.com/marketplace/actions/docker-metadata-action
Apache License 2.0
892 stars 115 forks source link

Support for negating is_default_branch #247

Open ben-foxmoore opened 1 year ago

ben-foxmoore commented 1 year ago

From what I can tell, it's currently not possible to negate the value calculated by is_default_branch. This makes it harder to disable a tag for all branches other than the default branch.

This could be supported by either adding a specific is_not_default_branch or with a not (...) helper (though given that is_default_branch is the only Global expression that this would work with, it seems rather pointless). An alternative, but probably larger change could be to support a disable attribute that reverses the logic used by enable.

If I've overlooked a way to do this that already exists, please let me know! Happy to work on a PR to support this if some guidance on preferred solution can be given.

Example workflow

The example workflow below has two tags - one which uses is_default_branch and adds the this-is-main- prefix, and a second which adds the this-is-not- prefix. Currently the only way to do this is to fall back to standard Github Workflow expressions.

name: test-docker-tags

on:
  push:
    branches:
      - '**'

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      -
        name: Docker meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            name/app
          tags: |
            type=ref,event=branch,enable={{is_default_branch}},prefix=this-is-main-
            type=ref,event=branch,enable=${{ github.ref != format('refs/heads/{0}', 'master') }},prefix=this-is-not-
aalvarezaph commented 1 year ago

This would be a nice addition IMO

crazy-max commented 1 year ago

I think this should work:

type=ref,event=branch,enable={{is_default_branch}},prefix=this-is-main-
type=ref,event=branch,enable={{#if is_default_branch}}false{{else}}true{{/if}},prefix=this-is-not-
flokoe commented 3 months ago

Hi there,

Unfortunately, the solution with the Handlebars conditional does not work.

It appears that the # sign in the template is interpreted as a comment and therefore leaves a broken Handlebars template. I get the following error message (the reason is the missing closing handlebar delimiter in the enable attribute):

Processing tags input
  type=ref,event=pr,enable={{,prefix=pr-,priority=600
Processing flavor input
Error: Parse error on line 1:
{{
--^
Expecting 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'EOF'

The following workaround via GitHub expressions works:

type=ref,event=pr,enable=${{ github.ref != 'refs/heads/main' && github.ref != 'refs/heads/master' }}