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

Handle multiple prefixes with Bake #124

Open
felipecrs opened this issue Aug 24, 2021 · 6 comments
Open

Handle multiple prefixes with Bake #124

felipecrs opened this issue Aug 24, 2021 · 6 comments

Comments

@felipecrs
Copy link
Contributor

felipecrs commented Aug 24, 2021

Let's say I have a Dockerfile like so:

FROM debian:latest AS debian

[...]


# debian is the default
FROM debian


FROM ubuntu:latest AS ubuntu

[...]


FROM alpine:latest AS alpine

[...]

And a docker-bake.hcl like so:

group "default" {
  targets = ["debian"]
}

group "all" {
  targets = ["debian", "ubuntu", "alpine", "centos"]
}

target "base" {
  context = "."
}

target "debian" {
  inherits = ["base"]
  target = "debian"
}

target "ubuntu" {
  inherits = ["base"]
  target = "ubuntu"
}

target "alpine" {
  inherits = ["base"]
  target = "alpine"
}

Any suggestions on how could I integrate the Metadata Action with the Bake Action so I would have tags generated with prefixes like:

my-image:latest # debian
my-image:debian
my-image:ubuntu
my-image:alpine

And of course, tagging with semver like my-image:alpine-1.1.0.

@crazy-max
Copy link
Member

@felipecrs If I understand correctly you want to transpose the bake target name as a tag?

@felipecrs
Copy link
Contributor Author

If I understand correctly you want to transpose the bake target name as a tag?

@crazy-max this specific point is just a coincidence.

One solution would be to:

  1. metadata-action to allow setting multiple targets
  2. metadata-action to allow setting default configuration that would be applied for all the multiple targets

Then, metadata-action instead of outputting a single target, it would output multiple targets.

@fourbytes
Copy link

I'm trying to do something similar to this. I have a repo with multiple micro-services, each service is pushed to a registry (ghcr.io/<owner>/<project>/<service>:<tag>) and it doesn't seem possible to use this metadata data action to set the tags for each service at the moment.
I could do it with multiple steps however that would slow down the build as they'd have to run sequentially.

@sandhose
Copy link

sandhose commented Jan 21, 2022

I had this exact problem, and figured that the bake files are just JSON files that can be merged with jq. This is how I solved it, by running the action twice and merging the files:

https://github.com/matrix-org/matrix-authentication-service/blob/1df5cf42e32abaeedddc504afeb431d77154f78f/.github/workflows/check.yaml#L317-L345

     - name: Docker meta
       id: meta
       uses: docker/metadata-action@v3
       with:
         images: ...
         bake-target: docker-metadata-action
         tags: ...

     - name: Docker meta (debug variant)
       id: meta-debug
       uses: docker/metadata-action@v3
       with:
         images: ...
         bake-target: docker-metadata-action-debug
         tags: ...

     - name: Merge buildx bake files
       run: |
           jq -s '.[0] * .[1]' ${{ steps.meta.outputs.bake-file }} ${{ steps.meta-debug.outputs.bake-file }} > docker-bake.override.json

That way I have two different targets filled in my bake file: https://github.com/matrix-org/matrix-authentication-service/blob/1df5cf42e32abaeedddc504afeb431d77154f78f/docker-bake.hcl#L4-L6

@StefanSchoof
Copy link
Contributor

There is a buildx bug that is related: docker/buildx#756

@felipecrs
Copy link
Contributor Author

Here is what I'm currently doing, which does not require jq merging the JSONs.

# docker-bake.hcl

target "base" {
    context = "."
    target = "base"
}

target "github" {
    inherits = ["base"]
    target = "github"
}
# .github/workflows/ci.yaml

[...]
    steps:
      - name: Docker meta (base)
        id: docker-meta-base
        uses: docker/metadata-action@v4
        with:
          images: |
            ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}
          flavor: |
            latest=false
          tags: |
            type=schedule,pattern=nightly
            type=schedule,pattern={{date 'YYYYMMDD'}}
            type=ref,event=branch
            type=ref,event=pr
            type=sha,enable=${{ github.event_name == 'push' }}
            type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) || github.event_name == 'schedule' }}
            type=raw,value=base,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) || github.event_name == 'schedule' }}
          bake-target: base
      - name: Docker meta (github)
        id: docker-meta-github
        uses: docker/metadata-action@v4
        with:
          images: |
            ghcr.io/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}
          flavor: |
            latest=false
            prefix=github-
          tags: |
            type=schedule,pattern=nightly
            type=schedule,pattern={{date 'YYYYMMDD'}}
            type=ref,event=branch
            # Prefix needs to be repeated for these, refs:
            # https://github.com/docker/metadata-action/issues/270
            type=ref,event=pr,prefix=github-pr-
            type=sha,prefix=github-sha-,enable=${{ github.event_name == 'push' }}
          bake-target: github
      - name: Build and push
        uses: docker/bake-action@v4
        with:
          pull: true
          no-cache: true
          push: ${{ ! startsWith(github.ref, 'refs/pull/') }}
          files: |
            ./docker-bake.hcl
            ${{ steps.docker-meta-base.outputs.bake-file }}
            ${{ steps.docker-meta-github.outputs.bake-file }}

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

No branches or pull requests

5 participants