解析 Docker Bake GitHub Action 中的元数据 JSON。

huangapple go评论101阅读模式
英文:

Parse metadata json from docker bake github action

问题

以下是您要翻译的部分:

"I need to grab the outputs from the metadata output on the docker/bake action: https://github.com/docker/bake-action#outputs

Here's the bake action:

  1. - name: Build all Dockerfiles images listed in docker-compose.yml
  2. uses: docker/bake-action@v2.3.0
  3. id: monorepo-build
  4. env:
  5. ....
  6. with:
  7. push: true
  8. files: docker-bake.hcl
  9. set: |
  10. *.args.environment=${{ steps.vars.outputs.env }}
  11. *.args.site=${{ inputs.SITE }}
  12. *.args.GH_PACKAGE_READ=${{ secrets.GH_TOKEN }}

Then immediately after I run:

  1. - name: Test getting the metadata output from Bake step
  2. run: |
  3. touch metadata.json
  4. echo "${{ steps.monorepo-build.outputs.metadata }}" > metadata.json
  5. cat metadata.json | jq '.mykey'

So as you see I've tried to echo the metadata into a json file first and then parse that using jq to get the top level key mykey. I've also tried to parse directly by echoing "${{ steps.monorepo-build.outputs.metadata }}" | jq '.mykey'

I'm getting the error parse error: Invalid numeric literal at line 2, column 8. Here's the JSON output from the file:

  1. {
  2. mykey: {
  3. containerimage.buildinfo: {
  4. frontend: dockerfile.v0,
  5. attrs: {
  6. ...
  7. sources: [{
  8. ...
  9. }]
  10. },
  11. containerimage.descriptor: {
  12. mediaType: ********
  13. digest: **********
  14. size: 856
  15. },
  16. containerimage.digest: *****************
  17. image.name: ****************
  18. }
  19. }

I've noticed it doesn't have double quotes around the key/value pairs. When I add double quotes on in my local copy and run jq it seems fine.

How can I sort this?"

英文:

I need to grab the outputs from the metadata output on the docker/bake action: https://github.com/docker/bake-action#outputs

Here's the bake action:

  1. - name: Build all Dockerfiles images listed in docker-compose.yml
  2. uses: docker/bake-action@v2.3.0
  3. id: monorepo-build
  4. env:
  5. ....
  6. with:
  7. push: true
  8. files: docker-bake.hcl
  9. set: |
  10. *.args.environment=${{ steps.vars.outputs.env }}
  11. *.args.site=${{ inputs.SITE }}
  12. *.args.GH_PACKAGE_READ=${{ secrets.GH_TOKEN }}

Then immediately after I run:

  1. - name: Test getting the metadata output from Bake step
  2. run: |
  3. touch metadata.json
  4. echo "${{ steps.monorepo-build.outputs.metadata }}" > metadata.json
  5. cat metadata.json | jq '.mykey'

So as you see I've tried to echo the metadata into a json file first and then parse that using jq to get the top level key mykey. I've also tried to parse directly by echoing "${{ steps.monorepo-build.outputs.metadata }}" | jq '.mykey'

I'm getting the error parse error: Invalid numeric literal at line 2, column 8. Here's the JSON output from the file:

  1. {
  2. mykey: {
  3. containerimage.buildinfo: {
  4. frontend: dockerfile.v0,
  5. attrs: {
  6. ...
  7. sources: [{
  8. ...
  9. }]
  10. },
  11. containerimage.descriptor: {
  12. mediaType: ********
  13. digest: **********
  14. size: 856
  15. },
  16. containerimage.digest: *****************
  17. image.name: ****************
  18. }
  19. }

I've noticed it doesn't have double quotes around the key/value pairs. When I add double quotes on in my local copy and run jq it seems fine.

How can I sort this?

答案1

得分: 2

代码部分不翻译,以下是翻译好的部分:

问题出在您的 echo 中的双引号 (""),如下所示:

  1. echo "${{ steps.monorepo-build.outputs.metadata }}"

将导致类似以下的结果:

  1. echo "{
  2. "mykey": {
  3. "containerimage.buildinfo": {
  4. "frontend": "dockerfile.v0,"
  5. "attrs": {

所以修复方法要么使用单引号:

  1. - name: 测试从 Bake 步骤获取元数据输出
  2. run: |
  3. touch metadata.json
  4. echo '${{ steps.monorepo-build.outputs.metadata }}' > metadata.json
  5. cat metadata.json | jq '.mykey'

要么使用 cat

  1. - name: 测试从 Bake 步骤获取元数据输出
  2. run: |
  3. cat << EOF > metadata.json
  4. ${{ steps.monorepo-build.outputs.metadata }}
  5. EOF
  6. cat metadata.json | jq '.mykey'

或者,根据评论中 Azeem 提到的可能有不同的方法:

  1. - name: 测试从 Bake 步骤获取元数据输出
  2. run: |
  3. jq '.myKey' <<< $METADATA > some-output.json
  4. env:
  5. METADATA: ${{ steps.monorepo-build.outputs.metadata }}
英文:

The issue is with the double quotes ("") in your echo, this:

  1. echo "${{ steps.monorepo-build.outputs.metadata }}"

would lead to something like:

  1. echo "{
  2. "mykey": {
  3. "containerimage.buildinfo": {
  4. "frontend": "dockerfile.v0,"
  5. "attrs": {

So the fix is to either use single quotes:

  1. - name: Test getting the metadata output from Bake step
  2. run: |
  3. touch metadata.json
  4. echo '${{ steps.monorepo-build.outputs.metadata }}' > metadata.json
  5. cat metadata.json | jq '.mykey'

or use cat:

  1. - name: Test getting the metadata output from Bake step
  2. run: |
  3. cat << EOF > metadata.json
  4. ${{ steps.monorepo-build.outputs.metadata }}
  5. EOF
  6. cat metadata.json | jq '.mykey'

Or maybe a different approach as Azeem mentioned in the comments:

  1. - name: Test getting the metadata output from Bake step
  2. run: |
  3. jq '.myKey' <<< $METADATA > some-output.json
  4. env:
  5. METADATA: ${{ steps.monorepo-build.outputs.metadata }}

huangapple
  • 本文由 发表于 2023年3月9日 21:06:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685041.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定