我一直在尝试将声纳法与詹金斯集成。在我使用脚本化管道返回质量门状态之前,它一直运行良好。 我正在编写的代码是:
pipeline {
agent any
stages {
stage("Git checkout") {
steps {
git 'https://github.com/AmolMandloi/junit-java-example.git'
}
}
stage("Maven") {
steps {
bat "mvn clean package test"
}
}
stage("Sonar") {
steps {
withSonarQubeEnv('sonar'){
bat 'mvn sonar:sonar'
}
}
}
stage("Quality Gate"){
timeout(time: 1, unit: 'HOURS') {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
stage("Postsonar") {
steps{
bat 'echo "All done"'
}
}
}}
该错误处于“质量门”阶段,因为没有它,一切正常。enter image description here
这是错误:
由用户Amol Mandloi启动 以耐久性级别运行:MAX_SURVIVABILITY org.codehaus.groovy.control.MultipleCompilationErrorsException:启动失败: WorkflowScript:21:未知阶段部分“超时”。从0.5版开始,阶段中的步骤必须位于“步骤”块中。 @第21行,第9列。 阶段(“质量门”){ ^
WorkflowScript:21:对于阶段“ Quality Gate”(第21行,第9列),预期为“步骤”,“阶段”或“平行”之一。 阶段(“质量门”){ ^
2个错误
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:142)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
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:337)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:428)
完成:失败
答案 0 :(得分:0)
stage("Quality Gate"){ timeout(time: 1, unit: 'HOURS') { def qg = waitForQualityGate() if (qg.status != 'OK') { error "Pipeline aborted due to quality gate failure: ${qg.status}" } } }
在声明性管道中,timeout
在stage
的正下方无效。您可以将其用作选项或步骤。
无论如何,对于声明性管道中的脚本节,您需要执行script
步骤。
stage("Quality Gate"){
options {
timeout(time: 1, unit: 'HOURS')
}
steps {
script {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
当超时应用于阶段中的所有内容时,此语法更易读。
stage("Quality Gate"){
steps {
script {
timeout(time: 1, unit: 'HOURS') {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
}
当超时仅适用于阶段的某些部分时,此语法是必需的。在您的情况下,我会选择使用timeout
选项,以获得更具说明性和可读性的方法。