gpt4 book ai didi

json - 如何使用 jq 从 Jenkins 构建 url 的 curl 中提取 userId?

转载 作者:行者123 更新时间:2023-12-01 09:33:30 24 4
gpt4 key购买 nike

我从 curling Jenkins $BUILD_URL/api/json 得到以下输出:

itai@Itais-MBP ~ -  $ curl -s --insecure -u itaig:password  https://ip.us-west-2.compute.amazonaws.com/job/Company-Analytics-Prod-Deploy_New/24/api/json |
jq -r '.actions[]'
{
"_class": "hudson.model.CauseAction",
"causes": [
{
"_class": "hudson.model.Cause$UserIdCause",
"shortDescription": "Started by user Itai Ganot",
"userId": "itaig",
"userName": "Itai Ganot"
}
]
}
{
"_class": "hudson.model.CauseAction",
"causes": [
{
"_class": "com.sonyericsson.rebuild.RebuildCause",
"shortDescription": "Rebuilds build #23",
"upstreamBuild": 23,
"upstreamProject": "Company-Analytics-Prod-Deploy_New",
"upstreamUrl": "view/Prod-US%20Deploy/job/Company-Analytics-Prod-Deploy_New/"
}
]
}
{
"_class": "hudson.model.ParametersAction",
"parameters": [
{
"_class": "eu.markov.jenkins.plugin.mvnmeta.MavenMetadataParameterValue",
"name": "Version"
},
{
"_class": "hudson.model.StringParameterValue",
"name": "region",
"value": "us-east-1"
},
{
"_class": "hudson.model.StringParameterValue",
"name": "StackName",
"value": "Company-Prd-21"
},
{
"_class": "hudson.model.StringParameterValue",
"name": "autostart",
"value": "No"
}
]
}
{}
{}
{
"_class": "hudson.plugins.git.util.BuildData",
"buildsByBranchName": {
"refs/remotes/origin/master": {
"_class": "hudson.plugins.git.util.Build",
"buildNumber": 24,
"buildResult": null,
"marked": {
"SHA1": "0e71efeac11c9909838453f4f103457ec3b04d27",
"branch": [
{
"SHA1": "0e71efeac11c9909838453f4f103457ec3b04d27",
"name": "refs/remotes/origin/master"
}
]
},
"revision": {
"SHA1": "0e71efeac11c9909838453f4f103457ec3b04d27",
"branch": [
{
"SHA1": "0e71efeac11c9909838453f4f103457ec3b04d27",
"name": "refs/remotes/origin/master"
}
]
}
}
},
"lastBuiltRevision": {
"SHA1": "0e71efeac11c9909838453f4f103457ec3b04d27",
"branch": [
{
"SHA1": "0e71efeac11c9909838453f4f103457ec3b04d27",
"name": "refs/remotes/origin/master"
}
]
},
"remoteUrls": [
"git@git.assembla.com:company.ci.git"
],
"scmName": ""
}
{
"_class": "hudson.plugins.git.GitTagAction"
}
{}
{}
{}
{}
{}
itai@Itais-MBP ~ - $

我正在尝试提取开始构建作业的“userId”,因为当前可用的 Jenkins 插件由于某种原因无法正常工作。

这是我尝试过的,但它给了我一个错误:

itai@Itais-MBP ~ -  $ curl -s --insecure -u itaig:password  https://ip.us-west-2.compute.amazonaws.com/job/Company-Analytics-Prod-Deploy_New/24/api/json |
jq -r '.actions[].causes[] | .userId'
itaig
null
jq: error (at <stdin>:0): Cannot iterate over null (null)
itai@Itais-MBP ~ - $

我认为这与某些“.actions[]” block 中没有“.causes[]”有关,如果是这种情况,是否可以提取“userId”?

最佳答案

使用 jq,您可以使用后缀“?”来防止不存在运营商,例如在你的情况下:

.actions[].causes[]?.userId? | select(.)

关于json - 如何使用 jq 从 Jenkins 构建 url 的 curl 中提取 userId?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47942448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com