Jenkins管道脚本错误WorkflowScript:预期的步骤

时间:2019-10-22 09:49:25

标签: jenkins syntax-error runtime-error jenkins-pipeline

我从一个更大的Jenkins管道项目中创建了一个测试用例,它工作正常。

pipeline { 
            agent none
        stages {     
            stage ("Check Parameters")
                          { 
                             steps {
                                     echo "In pipeline"
                                     script {
                                           echo "Start condition check"
                                                }
                                     build job: 'printuser'
                                     //def slaveJob = build job: 'printuser'
                                     //println slaveJob.rawBuild.log  
                                        }
                            }
                    }
            }

Howevr,为了将printuser作业的输出输出到管道中,我注释了一个构建作业:'printuser'并取消注释slaveJob代码,如下所示:

                                 //build job: 'printuser'
                                 def slaveJob = build job: 'printuser'
                                 println slaveJob.rawBuild.log  

现在它失败,并显示以下错误:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 13: Expected a step @ line 13, column 15.
                                            def slaveJob = build job: 'printuser'
                 ^

1 error

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
    at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
    at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:133)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:126)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:561)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:522)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:320)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE

请问这里是什么问题?

1 个答案:

答案 0 :(得分:1)

如果要使用groovy,则必须将其放在script部分的内部 ,而不是外部:

script {
    echo "Start condition check"
    build job: 'printuser'
    def slaveJob = build job: 'printuser'
    println slaveJob.rawBuild.log  
}

PS:不确定代码是否可以正常工作,但至少应将其放在内部script。在script闭包之外,jenkins将只接受一组预定义的步骤