jenkinscurljqjenkins-cli

How to get rid of null output while using jq command to grep the json output?


Am using below curl command to get the revision id from last successful jenkins build, from the json output am using jq to grep the revision id.

here is my json output from curl command

    "actions": [
      {
        "_class": "hudson.model.CauseAction",
        "causes": [
          {
            "_class": "hudson.triggers.SCMTrigger$SCMTriggerCause",
            "shortDescription": "Started by an SCM change"
          }
        ]
      },
      {},
      {
        "_class": "jenkins.metrics.impl.TimeInQueueAction",
        "blockedDurationMillis": 0,
        "blockedTimeMillis": 0,
        "buildableDurationMillis": 0,
        "buildableTimeMillis": 0,
        "buildingDurationMillis": 499235,
        "executingTimeMillis": 499235,
        "executorUtilization": 1,
        "subTaskCount": 0,
        "waitingDurationMillis": 6006,
        "waitingTimeMillis": 6006
      },
      {},
      {},
      {
        "_class": "hudson.plugins.git.util.BuildData",
        "buildsByBranchName": {
          "refs/remotes/origin/<branch name>": {
            "_class": "hudson.plugins.git.util.Build",
            "buildNumber": 6,
            "buildResult": null,
            "marked": {
              "SHA1": "4bvdcaeacjd652d9abb85",
              "branch": [
                {
                  "SHA1": "4bvdcaeacjd652d9abb85",
                  "name": "refs/remotes/origin/<branch name>"
                }
              ]
            },
            "revision": {
              "SHA1": "4bvdcaeacjd652d9abb85",
              "branch": [
                {
                  "SHA1": "4bvdcaeacjd652d9abb85",
                  "name": "refs/remotes/origin/<branch name>"
                }
              ]
            }
          }
        },
        "lastBuiltRevision": {
          "SHA1": "4bvdcaeacjd652d9abb85",
          "branch": [
            {
              "SHA1": "4bvdcaeacjd652d9abb85",
              "name": "refs/remotes/origin/<branch name>"
            }
          ]
        },
        "remoteUrls": [
          "<remote git url>"
        ],
        "scmName": ""
      },
      {},
      {
        "_class": "hudson.maven.reporters.MavenAggregatedArtifactRecord"
      },
      {},
      {
        "_class": "hudson.plugins.jira.JiraBuildAction"
      },
      {},
      {
        "_class": "org.jenkinsci.plugins.buildenvironment.actions.BuildEnvironmentBuildAction"
      },
      {
        "_class": "hudson.plugins.disk_usage.BuildDiskUsageAction"
      },
      {},
      {
        "_class": "org.jenkinsci.plugins.displayurlapi.actions.RunDisplayAction"
      }
    ]

here is the command am using to get the details from jenkins using curl and greping the revision id from curl output(raw curl output mentioned above).

curl --user <user name>:<user token> -s "<jenkins url>/jenkins/job/<job name>/<build number>/api/json | jq -r '.actions[].lastBuiltRevision.branch.SHA1'

below is the output am getting

null
null
null
null
null
4bvdcaeacjd652d9abb85
null
null
null
null
null
null
null
null
null

The curl command is taking 5 seconds to get the json output from jenkins, how do i get rid of this null ouput.

Because i want to use the revision id value on another steps.


Solution

  • Given the input, one way to achieve the desired output would be:

    .actions[]
    | select(.lastBuiltRevision)
    | .lastBuiltRevision.branch[].SHA1
    

    Another would be:

    .actions[] | try .lastBuiltRevision.branch[] | .SHA1
    

    There are numerous other possibilities. Which you choose will perhaps depend on your robustness requirements.