1
votes

I wish to get the latest status of a separate Jenkins job Backup_Precheck in my current Pipeline script.

Below is my pipeline script.

import groovy.json.JsonSlurper

pipeline 
{
   agent any
   stages {

      stage('check Job Backup_Precheck status'){

         steps {
  
            script{
 
              if(checkStatus() == "RUNNING" ){
                  timeout(time: 60, unit: 'MINUTES') {
                       waitUntil {

                         def status = checkStatus()
                            return  (status == "SUCCESS" || status == "FAILURE" || status == "UNSTABLE" || status == "ABORTED")
                  }
            }
        }

        if( checkStatus() != "SUCCESS" ){
           error('Stopping Job Weekend_Backup becuase job Backup_Precheck is not successful.')
        } else {
      
            echo 'Triggering ansible backup automation'
                      
      } // script end
                     
     } //steps ends here
    
} // stage ends here
   

stage('Hello') {
        
    steps {  echo 'Hello World'}
    
   }
    
  } //step closes
    
}    
    
def checkStatus() {
        
def statusUrl = httpRequest "https://portal.myshop.com:9043/job/Backup_Precheck/lastBuild/api/json"
    
def statusJson = new JsonSlurper().parseText(statusUrl.getContent())
        
                    return statusJson['result']
    
    }

I get the below error in the jenkins console logs:

[Pipeline] { (Hello) Stage "Hello" skipped due to earlier failure(s) [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline java.lang.NoSuchMethodError: No such DSL method 'httpRequest' found among steps [ansiColor, ansiblePlaybook, ansibleTower, ansibleTowerProjectRevision, ansibleTowerProjectSync, ansibleVault, archive, bat, build, catchError, checkout, deleteDir, dir, dockerFingerprintFrom, dockerFingerprintRun, dockerNode, echo, emailext, emailextrecipients, envVarsForTool, error, fileExists, findBuildScans, getContext, git, input, isUnix, junit, library, libraryResource, load, lock, mail, milestone, node, parallel, powershell, properties, publishHTML, pwd, pwsh, readFile, readTrusted, resolveScm, retry, script, sh, sleep, stage, stash, step, svn, task, timeout, tm, tool, unarchive, unstable, unstash, validateDeclarativePipeline, waitUntil, warnError, withContext, withCredentials, withDockerConta

I understand that i may need me to install HTTP Request Plugin inorder to resolve the above.

However, can't I get the latest status of a job without having to depend on the HTTP Request Plugin? If so, kindly guide me.

1

1 Answers

2
votes

There is an easier way to do this. Since Jenkins is implemented in Java and the pipeline runs in Groovy (the same VM), you can access every class and function from the Jenkins code. In order to get the build result of the current build of a job, you could do something like this:

def job = jenkins.model.Jenkins.instance.getItemByFullName("<folder>/<job name>")
def result = job.getLastBuild().result

This approach is very powerful and gives you a lot of control over Jenkins at runtime of your pipelines.

To find out more about it, you can:

  • Look at the documentation
  • Open the script console, start with jenkins.model.Jenkins.instance and just [print all available methods][2]

[2]: https://bateru.com/news/2011/11/code-of-the-day-groovy-print-all-methods-of-an-object/. You can move on from there.