无法使用 jq 捕获标签包含数字参数的 imageDigest,但为什么呢?
Unable to catch imageDigest where the tag contains a numeric argument using jq, but why?
正在尝试使用 imageTag 提取 ecr 图像摘要。
base ❯ aws ecr list-images --repository-name itaig
{
"imageIds": [
{
"imageDigest": "sha256:ec2ad330fadaf1ef05e30b8ba1ee5a92efb8b39c8d51bbcfb706b1b1a1981020",
"imageTag": "latest"
},
{
"imageDigest": "sha256:9377451ac4ea941e49d3176457b3d285d6418fec883a9a8fee87a491b79ac06a"
},
{
"imageDigest": "sha256:1fb63f185c11a3300d394f83f8152d391b5f37ee3b17b73702296e979fb42576"
},
{
"imageDigest": "sha256:4fc44daf69561482c9b00858b1a60e6590444703b75d49fed7d46c025520ddac"
},
{
"imageDigest": "sha256:ce109ba9df0e566a4bd9d0a8311596b61def59d51c1b42455170dcb41451f4b0",
"imageTag": "main-48"
},
{
"imageDigest": "sha256:928950d4cab36d434f99e65bb75e65e4a6218760b1a32a08ac7212b2b6b9b9b0",
"imageTag": "main-49"
},
{
"imageDigest": "sha256:30a7f57f1a8aea4b00a441fff139806662342f0b584651231ed99738bf9d00b8"
},
{
"imageDigest": "sha256:2319a7b3d5a030cc22efc06be780d6789cbf575822dd96bffbb550e3d20faefd"
}
]
}
~/src/personal/aws-s3-lambda main ⇣
base ❯ TRAVIS_BUILD_NUMBER=49
~/src/personal/aws-s3-lambda main ⇣
base ❯ TRAVIS_BRANCH=main
~/src/personal/aws-s3-lambda main ⇣
base ❯ aws ecr list-images --repository-name itaig | jq -r --arg travis_branch ${TRAVIS_BRANCH} --arg travis_build_number ${TRAVIS_BUILD_NUMBER} '.imageIds[] | select(.imageTag=="$travis_branch-$travis_build_number") .imageDigest'
如果我不使用 args,它就像一个魅力:
base ❯ aws ecr list-images --repository-name itaig | jq -r '.imageIds[] | select(.imageTag=="main-49") .imageDigest'
sha256:928950d4cab36d434f99e65bb75e65e4a6218760b1a32a08ac7212b2b6b9b9b0
但是jq没有抓到相关的imageDigest
我怀疑它与 travis_build_number 有关系,我想我需要使用“tonumber”,但我不确定将它放在我的查询中的什么位置。
知道怎么做吗?
与其将 2 个参数传递给 JQ,不如将它们组合到 bash 级别的 1 个参数中,如下所示:
jq -r --arg travis_search "$TRAVIS_BRANCH-$TRAVIS_BUILD_NUMBER" '.imageIds[] | select(.imageTag == $travis_search) | .imageDigest'
JqPlay Demo
我本地机器上的示例:
$
$ jq '.' test.json
{
"imageIds": [
{
"imageDigest": "sha256:ec2ad330fadaf1ef05e30b8ba1ee5a92efb8b39c8d51bbcfb706b1b1a1981020",
"imageTag": "latest"
},
{
"imageDigest": "sha256:9377451ac4ea941e49d3176457b3d285d6418fec883a9a8fee87a491b79ac06a"
},
{
"imageDigest": "sha256:1fb63f185c11a3300d394f83f8152d391b5f37ee3b17b73702296e979fb42576"
},
{
"imageDigest": "sha256:4fc44daf69561482c9b00858b1a60e6590444703b75d49fed7d46c025520ddac"
},
{
"imageDigest": "sha256:ce109ba9df0e566a4bd9d0a8311596b61def59d51c1b42455170dcb41451f4b0",
"imageTag": "main-48"
},
{
"imageDigest": "sha256:928950d4cab36d434f99e65bb75e65e4a6218760b1a32a08ac7212b2b6b9b9b0",
"imageTag": "main-49"
},
{
"imageDigest": "sha256:30a7f57f1a8aea4b00a441fff139806662342f0b584651231ed99738bf9d00b8"
},
{
"imageDigest": "sha256:2319a7b3d5a030cc22efc06be780d6789cbf575822dd96bffbb550e3d20faefd"
}
]
}
$
$ TRAVIS_BUILD_NUMBER=49
$ TRAVIS_BRANCH=main
$
$ jq -r --arg travis_search "$TRAVIS_BRANCH-$TRAVIS_BUILD_NUMBER" '.imageIds[] | select(.imageTag == $travis_search) | .imageDigest' test.json
sha256:928950d4cab36d434f99e65bb75e65e4a6218760b1a32a08ac7212b2b6b9b9b0
$
$
使用原题(jq -r --arg travis_branch ${TRAVIS_BRANCH} --arg travis_build_number ${TRAVIS_BUILD_NUMBER} ...
)中的调用形式,一个合适的jq程序可以是这样的:
.imageIds[]
| select(.imageTag == "\($travis_branch)-\($travis_build_number)")
| .imageDigest
正在尝试使用 imageTag 提取 ecr 图像摘要。
base ❯ aws ecr list-images --repository-name itaig
{
"imageIds": [
{
"imageDigest": "sha256:ec2ad330fadaf1ef05e30b8ba1ee5a92efb8b39c8d51bbcfb706b1b1a1981020",
"imageTag": "latest"
},
{
"imageDigest": "sha256:9377451ac4ea941e49d3176457b3d285d6418fec883a9a8fee87a491b79ac06a"
},
{
"imageDigest": "sha256:1fb63f185c11a3300d394f83f8152d391b5f37ee3b17b73702296e979fb42576"
},
{
"imageDigest": "sha256:4fc44daf69561482c9b00858b1a60e6590444703b75d49fed7d46c025520ddac"
},
{
"imageDigest": "sha256:ce109ba9df0e566a4bd9d0a8311596b61def59d51c1b42455170dcb41451f4b0",
"imageTag": "main-48"
},
{
"imageDigest": "sha256:928950d4cab36d434f99e65bb75e65e4a6218760b1a32a08ac7212b2b6b9b9b0",
"imageTag": "main-49"
},
{
"imageDigest": "sha256:30a7f57f1a8aea4b00a441fff139806662342f0b584651231ed99738bf9d00b8"
},
{
"imageDigest": "sha256:2319a7b3d5a030cc22efc06be780d6789cbf575822dd96bffbb550e3d20faefd"
}
]
}
~/src/personal/aws-s3-lambda main ⇣
base ❯ TRAVIS_BUILD_NUMBER=49
~/src/personal/aws-s3-lambda main ⇣
base ❯ TRAVIS_BRANCH=main
~/src/personal/aws-s3-lambda main ⇣
base ❯ aws ecr list-images --repository-name itaig | jq -r --arg travis_branch ${TRAVIS_BRANCH} --arg travis_build_number ${TRAVIS_BUILD_NUMBER} '.imageIds[] | select(.imageTag=="$travis_branch-$travis_build_number") .imageDigest'
如果我不使用 args,它就像一个魅力:
base ❯ aws ecr list-images --repository-name itaig | jq -r '.imageIds[] | select(.imageTag=="main-49") .imageDigest'
sha256:928950d4cab36d434f99e65bb75e65e4a6218760b1a32a08ac7212b2b6b9b9b0
但是jq没有抓到相关的imageDigest
我怀疑它与 travis_build_number 有关系,我想我需要使用“tonumber”,但我不确定将它放在我的查询中的什么位置。
知道怎么做吗?
与其将 2 个参数传递给 JQ,不如将它们组合到 bash 级别的 1 个参数中,如下所示:
jq -r --arg travis_search "$TRAVIS_BRANCH-$TRAVIS_BUILD_NUMBER" '.imageIds[] | select(.imageTag == $travis_search) | .imageDigest'
JqPlay Demo
我本地机器上的示例:
$
$ jq '.' test.json
{
"imageIds": [
{
"imageDigest": "sha256:ec2ad330fadaf1ef05e30b8ba1ee5a92efb8b39c8d51bbcfb706b1b1a1981020",
"imageTag": "latest"
},
{
"imageDigest": "sha256:9377451ac4ea941e49d3176457b3d285d6418fec883a9a8fee87a491b79ac06a"
},
{
"imageDigest": "sha256:1fb63f185c11a3300d394f83f8152d391b5f37ee3b17b73702296e979fb42576"
},
{
"imageDigest": "sha256:4fc44daf69561482c9b00858b1a60e6590444703b75d49fed7d46c025520ddac"
},
{
"imageDigest": "sha256:ce109ba9df0e566a4bd9d0a8311596b61def59d51c1b42455170dcb41451f4b0",
"imageTag": "main-48"
},
{
"imageDigest": "sha256:928950d4cab36d434f99e65bb75e65e4a6218760b1a32a08ac7212b2b6b9b9b0",
"imageTag": "main-49"
},
{
"imageDigest": "sha256:30a7f57f1a8aea4b00a441fff139806662342f0b584651231ed99738bf9d00b8"
},
{
"imageDigest": "sha256:2319a7b3d5a030cc22efc06be780d6789cbf575822dd96bffbb550e3d20faefd"
}
]
}
$
$ TRAVIS_BUILD_NUMBER=49
$ TRAVIS_BRANCH=main
$
$ jq -r --arg travis_search "$TRAVIS_BRANCH-$TRAVIS_BUILD_NUMBER" '.imageIds[] | select(.imageTag == $travis_search) | .imageDigest' test.json
sha256:928950d4cab36d434f99e65bb75e65e4a6218760b1a32a08ac7212b2b6b9b9b0
$
$
使用原题(jq -r --arg travis_branch ${TRAVIS_BRANCH} --arg travis_build_number ${TRAVIS_BUILD_NUMBER} ...
)中的调用形式,一个合适的jq程序可以是这样的:
.imageIds[]
| select(.imageTag == "\($travis_branch)-\($travis_build_number)")
| .imageDigest