如何使用jq从Jenkins构建URL上的curl中提取userId?

时间:2017-12-22 13:35:08

标签: json curl jenkins jq

我有来自<vector>, <string>, <chrono>, <stdio>, <iomanip>, <iostream>, <fstream>, <strsafe>, <math.h>, <thread>, <mutex>, <ctime> Jenkins $ BUILD_URL / api / json的以下输出:

curl

我正在尝试提取启动构建作业的“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[]'
{
  "_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 ~ -  $

我认为这与某些“.actions []”块中没有“.causes []”的事实有关,如果是这样的话,是否有可能提取“userId”?

3 个答案:

答案 0 :(得分:3)

使用jq,您可以使用post-fix“?”来防止不存在运算符,例如在你的情况下:

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

答案 1 :(得分:2)

Jenkins拥有自己的api语法,允许您过滤api输出。您可以使用xml版本并指定xpath。在您的情况下,以下网址应该为您提供userId。

curl -g https://ip.us-west-2.compute.amazonaws.com/job/Company-Analytics-Prod-Deploy_New/24/api/xml?xpath=//*/action[1]/cause/userId

答案 2 :(得分:0)

如何挖掘和修剪输出:

$ 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[]' | grep userId | cut -d ":" -f2 | tr -d '"," '