Skip to content

Instantly share code, notes, and snippets.

@paul-butcher
Created November 3, 2016 09:35
Show Gist options
  • Save paul-butcher/dc68adc1c05ca970158c18206756dab1 to your computer and use it in GitHub Desktop.
Save paul-butcher/dc68adc1c05ca970158c18206756dab1 to your computer and use it in GitHub Desktop.
Jenkins pipeline to check the status of a set of other jobs
import groovy.json.JsonSlurper
def getJobStatus(String jobName){
def rx = httpRequest "https://jenkins.example.com/job/${jobName}/lastBuild/api/json"
def rxJson = new JsonSlurper().parseText(rx.getContent())
return rxJson['result']
}
node() {
def any_success = false
stage('check'){
for(jobname in ['SomeJob', 'AnotherJob']){
jobStatus = getJobStatus(jobName)
echo jobStatus
if(jobStatus == "SUCCESS" || jobStatus == "UNSTABLE"){
any_success = true
break
}
}
}
stage('report') {
if(!any_success){
httpRequest 'https://jenkins.example.com/sounds/playSound?src=https://jenkins.example.com/userContent/sounds/dead.wav'
}
}
}
@mramanathan
Copy link

After changing 'jobName' in line 12, script fails to run complaining of 'httpRequest' in line 4.

Removed that too and now rx.getContent() seems to be invalid. Error is:

No signature of method: java.lang.String.getContent() is applicable for argument types: () values: []
Possible solutions: getBytes()

Want to know if you were able to get job status with this code snippet ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment