SJunejo
SJunejo

Reputation: 1336

Find out status of last build in shell script

I have a job with Shell script which runs every after 30 mins, download and accept changes from source control. Now I want to proceed in my shell script if and only if;

I have looked at the Jenkins wiki but from the obvious environment variable it is not possible to find out 'if my last run' was Stable or Unstable without writing code within my build either via using Jenkins XML API or some python script...Is there any easy way to find this information?

Upvotes: 3

Views: 6683

Answers (2)

Deepti
Deepti

Reputation: 361

You can make curl call directly to http://${JOB_URL}/lastBuild/api/json

No need to get prev build number.

Upvotes: 4

Slav
Slav

Reputation: 27485

You have to use a URL API, unless you want to complicate it even further with jenkins-cli

/api/json end-point is the easiest to use in shell/bash as it provides all information in a single line. We can then grep it for the data we need, strip out the leading identifier, and get the result.

You have access to the current build number as BUILD_NUMBER and can calculate the previous one. You also have access to convenient JOB_URL.

#!/bin/bash

# Calculate previous build number
prevBuild=$(($BUILD_NUMBER - 1))

# Get previous run status, returns like: result":"UNSTABLE
prevStatus=`curl -silent http://${JOB_URL}/${prevBuild}/api/json | grep -iEo 'result":"\w*'`

# Strip out leading identifier, i.e: result":"
prevStatus=${prevStatus/result\"\:\"/}

if [[ "$prevStatus" == "UNSTABLE" ]]; then
    do_whatever
fi

Upvotes: 9

Related Questions