提问人:Itai Ganot 提问时间:6/22/2021 最后编辑:peakItai Ganot 更新时间:6/23/2021 访问量:137
无法捕获 imageDigest,其中标签包含使用 jq 的数字参数,但为什么?
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
但相关的 imageDigest 没有被 jq 捕获。
我怀疑它与travis_build_number有关,我想我需要使用“tonumber”,但我不确定将它放在我的查询中的什么位置。
知道怎么做吗?
答:
2赞
0stone0
6/22/2021
#1
与其将 2 个参数传递给 JQ,我们为什么不像这样将它们组合成 bash 级别的 1 个:
jq -r --arg travis_search "$TRAVIS_BRANCH-$TRAVIS_BUILD_NUMBER" '.imageIds[] | select(.imageTag == $travis_search) | .imageDigest'
JqPlay 演示
我的本地计算机上的示例:
$
$ 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
$
$
评论
0赞
Itai Ganot
6/22/2021
它仍然对我不起作用...... 也许这与争论有关?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' ~/src/personal/aws-s3-lambda main ⇣
0赞
Itai Ganot
6/22/2021
请检查这个 SO 问题:stackoverflow.com/questions/41772776/... ->这就是我试图实现的,但无济于事,因为两个论点之间的破折号
0赞
0stone0
6/22/2021
当我们可以在发送到 JQ 之前将它们组合在一起时,为什么要转换该参数?请看我的编辑和示例。
0赞
peak
6/23/2021
#2
使用原始问题 () 中的调用形式,适当的 jq 程序可能如下所示:jq -r --arg travis_branch ${TRAVIS_BRANCH} --arg travis_build_number ${TRAVIS_BUILD_NUMBER} ...
.imageIds[]
| select(.imageTag == "\($travis_branch)-\($travis_build_number)")
| .imageDigest
评论