0

I am trying to parse CircleCI job json to see if ALL of the jobs are in successful state or not. If they are, then do something else keep waiting.

I was using this shell command in circleci job to see if any job failed to exit earlier

  is_failed=$(echo $json | jq '.items | map(select(.name | contains("my-custom-job") | not)) | any(.status == "failed")')

Now I want to check if ALL the jobs has success status, if so then do something else. How can i do that in shell script?

Here is link to CirclCI api (apparently the status doesn't have enum, it could be null etc).

Update: Sample Json

{
  "next_page_token": null,
  "items": [
    {
      "started_at": "2021-04-01T14:50:43Z",
      "name": "...",
      "type": "build",
      "status": "running"
    },
    {
      "started_at": null,
      "name": "...",
      "type": "build",
      "status": "blocked"
    },
    {
      "started_at": null,
      "name": "...",
      "type": "build",
      "status": "blocked"
    },
    {
      "started_at": "2021-04-01T14:50:43Z",
      "name": "auto-cancel",
      "type": "build",
      "status": "running"
    }
  ]
}
Em Ae
  • 8,167
  • 27
  • 95
  • 162
  • Please provide sample JSON data. BTW, `echo $json` is itself buggy -- **always** quote expansions, as in `echo "$json"`; see [I just assigned a variable but `echo $variable` shows something else](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Charles Duffy Apr 05 '21 at 16:12
  • Also, consider using `jq -e` to make exit status reflect result -- that way you can say `if jq ...` and branch on the result directly instead of needing to store the output to a variable and test the variable. – Charles Duffy Apr 05 '21 at 16:14
  • I have added `json`. I am not 100% sure how bash works, I just copy pasted this code. – Em Ae Apr 05 '21 at 16:17
  • updated the json. – Em Ae Apr 05 '21 at 17:46

2 Answers2

1

As implied in a comment, all makes it trivial with jq. Plus jq's -e option to set the exit status so it can easily be used with shell if:

if jq -e '.items | all(.status == "success")' <<<"$json" >/dev/null; then
    echo "All tests passed."
else
    echo "There are issues."
fi
Shawn
  • 47,241
  • 3
  • 26
  • 60
0

I want to check if ALL the jobs has success status

jq --arg wanted "success" '[.items[] | select(.status != $wanted)] | length == 0' file.json
glenn jackman
  • 238,783
  • 38
  • 220
  • 352