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

Question: list vs YAML array for inputs #88

Closed
tpolekhin opened this issue May 21, 2021 · 6 comments · Fixed by #89
Closed

Question: list vs YAML array for inputs #88

tpolekhin opened this issue May 21, 2021 · 6 comments · Fixed by #89

Comments

@tpolekhin
Copy link

Hello!
I was wondering why use multi-line string list instead of YAML array for inputs?
Maybe it's a technical limitation and it's okay if so I just wanted to check.

I'm trying to pass build args to the docker build with meta information and having difficulties because of this list type.
https://github.com/docker/metadata-action provides an easy way to inject meta labels for the container,
exporting a list of OCI labels that you can then use for labels: input. I guess they used a list type for their output to match this action input type.

I'm trying to reuse some of them as build args to also inject this information into the app binary, but it doesn't work because steps.meta.outputs.labels is not an array. I was looking around for a conversion function or tool that would help me to get this values out of labels, but couldn't find anything.

      - name: Prepare image metadata
        uses: docker/metadata-action@v3
        id: meta
        with:
          images: repo/app

      - name: Build and push docker image
        uses: docker/build-push-action@v2
        with:
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            BUILDTIME=${{ steps.meta.outputs.labels['org.opencontainers.image.created'] }}
            VERSION=${{ steps.meta.outputs.labels['org.opencontainers.image.version'] }}
            REVISION=${{ steps.meta.outputs.labels['org.opencontainers.image.revision'] }}

I think it would be more convenient to work with JSON/YAML Arrays, just because there's a lot more supported tooling and operations than for just a list of strings.

Curious to get your opinion on this

@tpolekhin tpolekhin changed the title Question: list vs YAML array for imputes Question: list vs YAML array for inputs May 21, 2021
@crazy-max
Copy link
Member

@tpolekhin

I was wondering why use multi-line string list instead of YAML array for inputs?
Maybe it's a technical limitation and it's okay if so I just wanted to check.

This is a limitation with the GitHub Actions toolkit itself yes (actions/toolkit#184).

I guess they used a list type for their output to match this action input type.

The separator used by the metadata action for tags or labels output is lf (\n).

I'm trying to reuse some of them as build args to also inject this information into the app binary, but it doesn't work because steps.meta.outputs.labels is not an array.

We could output labels, tags and other info using a JSON representation with a new output for this purpose:

{
  "tags": [
    "name/app:1.2.3",
    "name/app:1.2",
    "name/app:sha-90dd603",
    "name/app:latest"
  ],
  "labels": {
    "org.opencontainers.image.title": "Hello-World",
    "org.opencontainers.image.description": "This your first repo!",
    "org.opencontainers.image.url": "https://github.com/octocat/Hello-World",
    "org.opencontainers.image.source": "https://github.com/octocat/Hello-World",
    "org.opencontainers.image.version": "1.2.3",
    "org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
    "org.opencontainers.image.revision": "90dd6032fac8bda1b6c4436a2e65de27961ed071",
    "org.opencontainers.image.licenses": "MIT"
  }
}

This way you could use the fromJSON function expression to handle that:

      - name: Prepare image metadata
        uses: docker/metadata-action@v3
        id: meta
        with:
          images: repo/app

      - name: Build and push docker image
        uses: docker/build-push-action@v2
        with:
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            BUILDTIME=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}
            VERSION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}
            REVISION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}

@tpolekhin
Copy link
Author

Thank you for a quick response on that!
Currently I use workaround, pretty similar to what you proposed actually :)

      - name: Extract meta image labels
        id: extract
        run: echo ::set-output name=labels::$(jq -r -c '.target."docker-metadata-action".labels' ${{ steps.meta.outputs.bake-file }})

      - name: Build and push operator image
        uses: docker/build-push-action@v2
        with:
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            BUILDTIME=${{ fromJSON(steps.extract.outputs.labels)['org.opencontainers.image.created'] }}
            VERSION=${{ fromJSON(steps.extract.outputs.labels)['org.opencontainers.image.version'] }}
            REVISION=${{ fromJSON(steps.extract.outputs.labels)['org.opencontainers.image.revision'] }}

I would definitely appreciate if you can add json format output so I can throw away this intermediate step

@crazy-max
Copy link
Member

@tpolekhin Ok thanks for your feedback. I move this issue to the metadata repo.

@crazy-max crazy-max transferred this issue from docker/build-push-action May 21, 2021
@crazy-max
Copy link
Member

@tpolekhin You can try with docker/metadata-action@master.

@tpolekhin
Copy link
Author

tpolekhin commented May 23, 2021

@crazy-max Works perfectly! Thanks!
https://github.com/tpolekhin/actions-playground/runs/2650265161

Are you planning to release a new version?
I'm a little concerned to use @master in the real pipeline

@crazy-max
Copy link
Member

@tpolekhin Yes in few minutes

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

Successfully merging a pull request may close this issue.

2 participants