Jenkins pipeline script error: End of Pipeline java.lang.NoSuchMethodError: No such DSL method 'httpRequest' found

Jenkins pipeline script error: End of Pipeline java.lang.NoSuchMethodError: No such DSL method 'httpRequest' found

我希望在我当前的流水线脚本中获取单独 Jenkins 作业的最新状态 Backup_Precheck

下面是我的管道脚本。

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']
    
    }

我在 jenkins 控制台日志中收到以下错误:

[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

我知道我可能需要安装 HTTP 请求插件才能解决上述问题。

但是,我不能不依赖 HTTP Request Plugin 获取工作的最新状态吗?如果是这样,请指导我。

有一种更简单的方法可以做到这一点。由于 Jenkins 在 Java 中实现并且管道在 Groovy(同一 VM)中运行,因此您可以访问每个 class 并从 Jenkins 代码运行。为了获得当前构建作业的构建结果,您可以这样做:

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

这种方法非常强大,可以让您在管道运行时对 Jenkins 进行大量控制。

要了解更多信息,您可以:

  • 看看documentation
  • 打开脚本控制台,从jenkins.model.Jenkins.instance开始,然后[打印所有可用方法][2]

[2]:https://bateru.com/news/2011/11/code-of-the-day-groovy-print-all-methods-of-an-object/。您可以从那里继续前进。