【问题标题】:Jenkins pipeline script error: End of Pipeline java.lang.NoSuchMethodError: No such DSL method 'httpRequest' foundJenkins 管道脚本错误:管道结束 java.lang.NoSuchMethodError:找不到这样的 DSL 方法“httpRequest”
【发布时间】:2020-12-31 15:16:40
【问题描述】:

我希望在我当前的流水线脚本中获得单独的 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" 由于早期的失败而被跳过 [管道] } [管道] // 阶段 [管道] } [管道] // 节点 [管道] 管道结束 java.lang.NoSuchMethodError: 没有这样的 DSL 在步骤 [ansiColor, ansiblePlaybook, ansibleTower, ansibleTowerProjectRevision, ansibleTowerProjectSync, ansibleVault,存档,蝙蝠,构建,catchError,结帐,deleteDir, 目录,dockerFingerprintFrom,dockerFingerprintRun,dockerNode,回声, emailext, emailextrecipients, envVarsForTool, 错误, fileExists, findBuildScans、getContext、git、输入、isUnix、junit、库、 libraryResource,加载,锁定,邮件,里程碑,节点,并行, powershell, 属性, publishHTML, pwd, pwsh, readFile, readTrusted, resolveScm, 重试, 脚本, sh, sleep, stage, stash, step, svn, task, 超时,tm,工具,取消归档,不稳定,unstash, validateDeclarativePipeline、waitUntil、warnError、withContext、 withCredentials, withDockerConta

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

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

【问题讨论】:

    标签: http jenkins jenkins-pipeline status nosuchmethoderror


    【解决方案1】:

    有一种更简单的方法可以做到这一点。由于 Jenkins 是用 Java 实现的,并且管道在 Groovy(同一个 VM)中运行,因此您可以从 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/。你可以从那里继续前进。

    【讨论】:

    • 我试过`jenkins.model.Jenkins.instance.getItemByFullName("job/Backup_Precheck")。但后来我得到 java.lang.NullPointerException: Cannot invoke method getLastBiild() on mil object.
    • 请注意,在我的管道作业中,我不想检查当前作业状态,而是检查不同的单独作业的状态,即Backup_Precheck
    • 如果您的工作不在文件夹中,您只需执行 getItemByFullName(""),这适用于完全独立的工作
    • 我取得了进展,但仍然无法正常工作。最初我得到了org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method hudson.model.Run getResult,这是通过允许权限解决的。但是,我现在收到以下错误:[Pipeline] End of Pipeline groovy.lang.MissingPropertyException: No such property: statusJson for class: groovy.lang.Binding。请提出建议。
    • 那是因为你不再定义 statusJson 但你仍在使用它在一些品脱
    猜你喜欢
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 2021-07-23
    相关资源
    最近更新 更多