Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{{branch}} is not set on pull request events #191

Closed
evandam opened this issue Apr 20, 2022 · 10 comments
Closed

{{branch}} is not set on pull request events #191

evandam opened this issue Apr 20, 2022 · 10 comments

Comments

@evandam
Copy link

evandam commented Apr 20, 2022

Troubleshooting

Before submitting a bug report please read the Troubleshooting doc.

Behaviour

Using {{branch}} as outlined in https://github.com/docker/metadata-action#global-expressions does not work when pushing commits to a branch with a PR open.

Steps to reproduce this issue

  1. Open a PR for new branch
  2. Push commits
  3. Observe output of workflow

Expected behaviour

Docker tags should be outputted as follows assuming a branch foo and sha 12345

my/repo:12345-amd64
my/repo:foo-amd64
my/repo:foo-12345-amd64

Actual behaviour

{{branch}} is empty so the tags are as follows:

my/repo:12345-amd64
my/repo:-amd64
my/repo:-12345-amd64

Configuration

name: docker-metadata-test
on:
  push:
    branches: ["main"]
  pull_request:
    branches: ["*"]
jobs:
  name: docker-metadata-test
  runs-on: ubuntu-latest
  steps:
    - uses: actions/checkout@v3
    - name: Docker meta
       id: meta
       uses: docker/metadata-action@v3
       with:
         images: my/repo
         tags: |
           type=raw,value={{sha}}-amd64
           type=raw,value={{branch}}-amd64
           type=raw,value={{branch}}-{{sha}}-amd64

Logs

metadata step of logs with some parts redacted
2022-04-20T00:00:44.3789494Z ##[group]Run docker/metadata-action@v3
2022-04-20T00:00:44.3789795Z with:
2022-04-20T00:00:44.3790108Z   images: my/repo
2022-04-20T00:00:44.3790585Z   tags: type=raw,value={{sha}}-amd64
type=raw,value={{branch}}-amd64
type=raw,value={{branch}}-{{sha}}-amd64

2022-04-20T00:00:44.3791171Z   github-token: ***
2022-04-20T00:00:44.3791392Z ##[endgroup]
2022-04-20T00:00:44.8175981Z ##[group]Context info
2022-04-20T00:00:44.8232603Z eventName: pull_request
2022-04-20T00:00:44.8232917Z sha: 137b7b03cf19010d9f14a6315a7d8d7741b99b4a
2022-04-20T00:00:44.8233160Z ref: refs/pull/80/merge
2022-04-20T00:00:44.8233385Z workflow: docker-metadata-test
2022-04-20T00:00:44.8233598Z action: meta
2022-04-20T00:00:44.8233787Z actor: evandam
2022-04-20T00:00:44.8233989Z runNumber: 124
2022-04-20T00:00:44.8234191Z runId: 2192825535
2022-04-20T00:00:44.8234662Z ##[endgroup]
2022-04-20T00:00:44.8235061Z ##[group]Processing tags input
2022-04-20T00:00:44.8235683Z type=raw,value={{sha}}-amd64,enable=true,priority=200
2022-04-20T00:00:44.8236093Z type=raw,value={{branch}}-amd64,enable=true,priority=200
2022-04-20T00:00:44.8236831Z type=raw,value={{branch}}-{{sha}}-amd64,enable=true,priority=200
2022-04-20T00:00:44.8237290Z ##[endgroup]
2022-04-20T00:00:44.8237665Z ##[group]Processing flavor input
2022-04-20T00:00:44.8237895Z latest=auto
2022-04-20T00:00:44.8238091Z prefix=
2022-04-20T00:00:44.8238279Z prefixLatest=false
2022-04-20T00:00:44.8238484Z suffix=
2022-04-20T00:00:44.8238678Z suffixLatest=false
2022-04-20T00:00:44.8239104Z ##[endgroup]
2022-04-20T00:00:44.8326167Z ##[group]Docker image version
2022-04-20T00:00:44.8326749Z 137b7b0-amd64
2022-04-20T00:00:44.8327230Z ##[endgroup]
2022-04-20T00:00:44.8348648Z ##[group]Docker tags
2022-04-20T00:00:44.8349280Z my/repo:137b7b0-amd64
2022-04-20T00:00:44.8349838Z my/repo:-amd64
2022-04-20T00:00:44.8350355Z my/repo:-137b7b0-amd64
2022-04-20T00:00:44.8351069Z ##[endgroup]
2022-04-20T00:00:44.8353443Z ##[group]Docker labels
2022-04-20T00:00:44.8353828Z org.opencontainers.image.title=my_repo
2022-04-20T00:00:44.8354284Z org.opencontainers.image.description=description
2022-04-20T00:00:44.8354824Z org.opencontainers.image.url=https://github.com/my/repo
2022-04-20T00:00:44.8355332Z org.opencontainers.image.source=https://github.com/my/repo
2022-04-20T00:00:44.8355840Z org.opencontainers.image.version=137b7b0-amd64
2022-04-20T00:00:44.8356377Z org.opencontainers.image.created=2022-04-20T00:00:44.822Z
2022-04-20T00:00:44.8356850Z org.opencontainers.image.revision=137b7b03cf19010d9f14a6315a7d8d7741b99b4a
2022-04-20T00:00:44.8357230Z org.opencontainers.image.licenses=
2022-04-20T00:00:44.8357770Z ##[endgroup]
2022-04-20T00:00:44.8358909Z ##[group]JSON output
2022-04-20T00:00:44.8359141Z {
2022-04-20T00:00:44.8359395Z   "tags": [
2022-04-20T00:00:44.8359898Z     "my/repo:137b7b0-amd64",
2022-04-20T00:00:44.8360652Z     "my/repo:-amd64",
2022-04-20T00:00:44.8361195Z     "my/repo:-137b7b0-amd64"
2022-04-20T00:00:44.8361544Z   ],
2022-04-20T00:00:44.8361757Z   "labels": {
2022-04-20T00:00:44.8363964Z     "org.opencontainers.image.title": "my_repo",
2022-04-20T00:00:44.8364504Z     "org.opencontainers.image.description": "description",
2022-04-20T00:00:44.8364981Z     "org.opencontainers.image.url": "https://github.com/my/repo",
2022-04-20T00:00:44.8365506Z     "org.opencontainers.image.source": "https://github.com/my/repo",
2022-04-20T00:00:44.8366099Z     "org.opencontainers.image.version": "137b7b0-amd64",
2022-04-20T00:00:44.8366684Z     "org.opencontainers.image.created": "2022-04-20T00:00:44.822Z",
2022-04-20T00:00:44.8367285Z     "org.opencontainers.image.revision": "137b7b03cf19010d9f14a6315a7d8d7741b99b4a",
2022-04-20T00:00:44.8367724Z     "org.opencontainers.image.licenses": ""
2022-04-20T00:00:44.8368040Z   }
2022-04-20T00:00:44.8368250Z }
2022-04-20T00:00:44.8368809Z ##[endgroup]
2022-04-20T00:00:44.8370169Z ##[group]Bake definition file
2022-04-20T00:00:44.8370447Z {
2022-04-20T00:00:44.8371202Z   "target": {
2022-04-20T00:00:44.8371777Z     "docker-metadata-action": {
2022-04-20T00:00:44.8372031Z       "tags": [
2022-04-20T00:00:44.8372504Z         "my/repo:137b7b0-amd64",
2022-04-20T00:00:44.8373187Z         "my/repo:-amd64",
2022-04-20T00:00:44.8373693Z         "my/repo:-137b7b0-amd64"
2022-04-20T00:00:44.8374050Z       ],
2022-04-20T00:00:44.8374327Z       "labels": {
2022-04-20T00:00:44.8374689Z         "org.opencontainers.image.title": "my_repo",
2022-04-20T00:00:44.8375124Z         "org.opencontainers.image.description": "description",
2022-04-20T00:00:44.8375657Z         "org.opencontainers.image.url": "https://github.com/my/repo",
2022-04-20T00:00:44.8376194Z         "org.opencontainers.image.source": "https://github.com/my/repo",
2022-04-20T00:00:44.8376702Z         "org.opencontainers.image.version": "137b7b0-amd64",
2022-04-20T00:00:44.8377253Z         "org.opencontainers.image.created": "2022-04-20T00:00:44.822Z",
2022-04-20T00:00:44.8377729Z         "org.opencontainers.image.revision": "137b7b03cf19010d9f14a6315a7d8d7741b99b4a",
2022-04-20T00:00:44.8378187Z         "org.opencontainers.image.licenses": ""
2022-04-20T00:00:44.8378466Z       },
2022-04-20T00:00:44.8378768Z       "args": {
2022-04-20T00:00:44.8379288Z         "DOCKER_META_IMAGES": "my/repo",
2022-04-20T00:00:44.8379716Z         "DOCKER_META_VERSION": "137b7b0-amd64"
2022-04-20T00:00:44.8380053Z       }
2022-04-20T00:00:44.8380309Z     }
2022-04-20T00:00:44.8380513Z   }
2022-04-20T00:00:44.8380792Z }
2022-04-20T00:00:44.8381452Z ##[endgroup]

Download the log file of your build and attach it to this issue.

@crazy-max crazy-max transferred this issue from docker/build-push-action Apr 20, 2022
@scalp42
Copy link

scalp42 commented Apr 20, 2022

I can reproduce the issue, we ended up just tagging by hand but it feels like we're missing something 👀

@crazy-max
Copy link
Member

I have updated the documentation in #192 to have a better understanding to what each global expression stands for: https://github.com/crazy-max/docker-metadata-action/tree/default-branch#global-expressions

Here {{branch}} returns the branch name that triggered the workflow run so it will be empty for a pull_request event as it's not a branch reference. What you need in this case is to use {{base_ref}}.

To make this works for push and pull_request events, I think you can just add both for the same tag entry as they can't be both set anyway:

    - name: Docker meta
       id: meta
       uses: docker/metadata-action@v3
       with:
         images: my/repo
         tags: |
           type=raw,value={{sha}}-amd64
           type=raw,value={{branch}}{{base_ref}}-amd64
           type=raw,value={{branch}}{{base_ref}}-{{sha}}-amd64

@evandam
Copy link
Author

evandam commented Apr 22, 2022

Hey @crazy-max I think this isn't quite what we're looking for though.

If we have a PR on a branch named feature-xyz, I want to create a Docker tag named feature-xyz. base_ref refers to the target branch though, which would intuitively create tags like master from your PR.

I want to avoid tagging by hand though to take advantage of sanitizing the branch name to a valid image tag.

@crazy-max
Copy link
Member

In this case you need the head ref:

    - name: Docker meta
       id: meta
       uses: docker/metadata-action@v3
       with:
         images: my/repo
         tags: |
           type=raw,value={{sha}}-amd64
           type=raw,value={{branch}}-amd64
           type=raw,value={{branch}}-{{sha}}-amd64
           type=raw,value={{github.event.pull_request.head.ref}}-amd64,enable=${{ github.event_name == 'pull_request' }}
           type=raw,value={{github.event.pull_request.head.ref}}-{{sha}}-amd64,enable=${{ github.event_name == 'pull_request' }}

@evandam
Copy link
Author

evandam commented Apr 25, 2022

I see, so would you still recommend the approach you mentioned earlier to support PRs and pushes to branches?

tags: |
  type=raw,value={{branch}}{{github.event.pull_request.head.ref}}

Also just want to confirm will this still sanitize the branch name, replacing / with _ for example?

Thanks!

@crazy-max
Copy link
Member

I see, so would you still recommend the approach you mentioned earlier to support PRs and pushes to branches?

tags: |
  type=raw,value={{branch}}{{github.event.pull_request.head.ref}}

Yes I think a single entry should work. Let me know.

Also just want to confirm will this still sanitize the branch name, replacing / with _ for example?

Unfortunately no, we would need another global expression like {{head_ref}}. At this point I'm worrying about adding too much logic and still think it should be covered by GitHub in their payload and not here. See #184 (comment)

But I think this one is still valid and we could add this global exp.

@evandam
Copy link
Author

evandam commented Apr 25, 2022

Gotcha thanks @crazy-max. If it's not sanitizing then I think it isn't a route we can go with and will have to do some custom work around it similar to @scalp42 most likely. It would be great to see this as a global ref that we could easily grab in our pipelines since it seems like a common enough pattern.

@felipecrs
Copy link
Contributor

felipecrs commented Apr 25, 2022

@evandam I know this isn't a solution for your problem, but if I were you, I would probably prefer to tag PRs with the pr number instead of the branch name. Like myimage:pr-123.

I find this more descriptive than the branch name itself, as often we just give it any names. Oftenly we even reuse the same branch name as they get deleted after PR merge anyway.

@crazy-max
Copy link
Member

I agree with @felipecrs, PR number is more reliable.

@crazy-max
Copy link
Member

Closing this one as pr number is a more reliable pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants