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

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

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:

      - name: Build all Dockerfiles images listed in docker-compose.yml
        uses: docker/bake-action@v2.3.0
        id: monorepo-build
        env:
          ....
        with:
          push: true
          files: docker-bake.hcl
          set: |
            *.args.environment=${{ steps.vars.outputs.env }}
            *.args.site=${{ inputs.SITE }}
            *.args.GH_PACKAGE_READ=${{ secrets.GH_TOKEN }}            

Then immediately after I run:

      - name: Test getting the metadata output from Bake step 
        run: |
          touch metadata.json
          echo "${{ steps.monorepo-build.outputs.metadata }}" > metadata.json
          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:

{
	mykey: {
		containerimage.buildinfo: {
			frontend: dockerfile.v0,
			attrs: {
				...
			sources: [{
				...
			}]
		},
		containerimage.descriptor: {
			mediaType: ********
			digest: **********
			size: 856
		},
		containerimage.digest: *****************
		image.name: ****************
	}
}

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:

      - name: Build all Dockerfiles images listed in docker-compose.yml
        uses: docker/bake-action@v2.3.0
        id: monorepo-build
        env:
          ....
        with:
          push: true
          files: docker-bake.hcl
          set: |
            *.args.environment=${{ steps.vars.outputs.env }}
            *.args.site=${{ inputs.SITE }}
            *.args.GH_PACKAGE_READ=${{ secrets.GH_TOKEN }}

Then immediately after I run:

      - name: Test getting the metadata output from Bake step 
        run: |
          touch metadata.json
          echo "${{ steps.monorepo-build.outputs.metadata }}" > metadata.json
          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:

{
	mykey: {
		containerimage.buildinfo: {
			frontend: dockerfile.v0,
			attrs: {
				...
			sources: [{
				...
			}]
		},
		containerimage.descriptor: {
			mediaType: ********
			digest: **********
			size: 856
		},
		containerimage.digest: *****************
		image.name: ****************
	}
}

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 中的双引号 (""),如下所示:

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

将导致类似以下的结果:

echo "{
    "mykey": {
        "containerimage.buildinfo": {
            "frontend": "dockerfile.v0,"
            "attrs": {

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

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

要么使用 cat

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

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

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

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

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

would lead to something like:

echo "{
    "mykey": {
        "containerimage.buildinfo": {
            "frontend": "dockerfile.v0,"
            "attrs": {

So the fix is to either use single quotes:

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

or use cat:

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

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

  - name: Test getting the metadata output from Bake step 
    run: |
      jq '.myKey' <<< $METADATA > some-output.json
    env:
      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:

确定