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

Test image with multiple tags from metadata-action #506

Closed
sim642 opened this issue Nov 21, 2021 · 6 comments
Closed

Test image with multiple tags from metadata-action #506

sim642 opened this issue Nov 21, 2021 · 6 comments

Comments

@sim642
Copy link

sim642 commented Nov 21, 2021

There's an example on how to load and test an image with a fixed tag: https://github.com/docker/build-push-action/blob/master/docs/advanced/test-before-push.md. But I'm using metadata-action to potentially build and push images with multiple tags, thus just trying to run ${{ steps.meta.outputs.tags }} won't work.

Previously I didn't use gha cache and then ${{ steps.build.outputs.digest }} seemed to work for docker run. But since adding caching (and thus docker/setup-buildx-action), this doesn't work anymore:

 docker: Error response from daemon: No such image: sha256:6507963a29515198ef9813243ad16cd47e864a05594682b66e0de32fbc9196e2.

I guess it's somehow related to #461, but I don't really care what digest is what and where it's shown. I just want to run the loaded image.

I also tried ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.build.outputs.digest }}, but that doesn't work for running the locally loaded image either:

Unable to find image 'ghcr.io/goblint/analyzer@sha256:53a5a659327deceb4ea5f1a9e89d6648cd8069d2b5bc59d7b75d1199987fd768' locally

How is this supposed to be done?

@crazy-max
Copy link
Member

@sim642

How is this supposed to be done?

Pretty much the same as the example from https://github.com/docker/build-push-action/blob/master/docs/advanced/test-before-push.md:

name: ci

on:
  push:
    branches:
      - 'master'

env:
  TEST_TAG: myapp:test

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout
        uses: actions/checkout@v2
      -
        name: Docker meta
        id: meta
        uses: docker/metadata-action@v3
        with:
          images: name/app
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      -
        name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Build and export to Docker
        uses: docker/build-push-action@v2
        with:
          context: .
          load: true
          tags: ${{ env.TEST_TAG }}
      -
        name: Test
        run: |
          docker run --rm ${{ env.TEST_TAG }}
      -
        name: Build and push
        uses: docker/build-push-action@v2
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

@sim642
Copy link
Author

sim642 commented Nov 21, 2021

The example completely misses my entire point of multiple tags given by the metadata-action. The example just hardcodes a single tag. When using metadata-action to dynamically determine tags, some builds may get multiple and there might not be a single tag that's always present. Even the latest tag wouldn't be applied to all builds (e.g. edge or PRs).

The value of ${{ steps.meta.outputs.tags }} is separated by newlines (or commas if reconfigured), but neither is suitable to be passed as an image name argument to docker run, if there happen to be multiple tags built simultaneously.

@crazy-max
Copy link
Member

crazy-max commented Nov 21, 2021

@sim642 Don't really get your point of using the metadata action in the step that exports the image to docker to be able to test it but you can use the ${{ steps.meta.outputs.version }} output:

      -
        name: Test
        run: |
          docker run --rm name/app:${{ steps.meta.outputs.version }}

Or the JSON output from the metadata action:

      -
        name: Test
        run: |
          docker run --rm ${{ fromJSON(steps.meta.outputs.json).tags[0] }}

Or the metadata output of this action:

      -
        name: Test
        run: |
          docker run --rm ${{ fromJSON(steps.build.outputs.metadata)['containerimage.config.digest'] }}

@sim642
Copy link
Author

sim642 commented Nov 21, 2021

Thanks! This is what I was looking for. Maybe it would be useful for others to have this mentioned in the test-before-push example document as well.

@Sleepful
Copy link

As someone that isn't super-familiar with Github Actions, I had no idea how to select from the JSON output that is mentioned in the README, this bit from your example is super clarifying:

${{ fromJSON(steps.build.outputs.metadata)['containerimage.config.digest'] }}

Please consider adding this as an example to the README, thanks!

@thecristen
Copy link

I had no idea which data and which keys were available in the outputs.metadata, as there's no description of what's in this object anywhere in the docs. Just saying... this thread was helpful to me as well.

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

4 participants