如何在jq中通过最后构建分支名称获取对应构建编号
单次jq调用获取最后构建分支的构建编号
给定JSON:
{ "buildsByBranchName": { "origin/master": { "buildNumber": 28694 }, "master": { "buildNumber": 28563 }, "refs/remotes/origin/master": { "buildNumber": 4094 } }, "lastBuiltRevision": { "branch": [ { "name": "origin/master" } ] } }
可以通过变量绑定的方式在单次jq调用里完成需求,具体命令如下:
jq '.lastBuiltRevision.branch[0].name as $branch | .buildsByBranchName[$branch].buildNumber' input.json
命令解释
- 先用
.lastBuiltRevision.branch[0].name as $branch提取最后构建的分支名称,将其赋值给变量$branch - 再通过管道符
|连接后续操作,直接用变量$branch作为键,从.buildsByBranchName中取出对应的buildNumber
执行该命令后,输出结果为:
28694
内容的提问来源于stack exchange,提问作者Philippe
相关产品推荐
相关产品推荐

