如何在声明性Jenkinsfile中定义并行阶段?

时间:2017-10-10 14:40:20

标签: jenkins jenkins-pipeline jenkins-declarative-pipeline

我正在使用Jenkins 2.73.1,我正在尝试使用声明性Jenkins文件定义并行阶段,如下所示:

pipeline {
    agent any

    tools {
        jdk 'JDK8'
    }

    options {
        disableConcurrentBuilds()
        timestamps() // write timestamps in front of each console line
    }

    triggers {
        // Every minute
        pollSCM '*/1 * * * *'
    }

    stages {
        stage('Rüsten') {
            steps {
                bat "echo Hello"
            }
        }

        stage('Kochen (parallel)') {

            parallel {
                stage('Salzen') {
                    steps {
                        bat "echo Hello"
                    }        
                }
                stage('Rühren') {
                    steps {
                        bat "echo Hello"
                    }        
                }
                stage('Abschmecken') {
                    steps {
                        bat "echo Hello"
                    }        
                }
            }
        }

        stage('Essen') {
            steps {
                bat "echo Hello"
            }
        }
    }
}

不幸的是,这会导致以下编译错误。

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 25: Unknown stage section "parallel". Starting with version 0.5, steps in a stage must be in a steps block. @ line 25, column 9.
           stage('Kochen (parallel)') {
           ^

WorkflowScript: 25: Nothing to execute within stage "Kochen (parallel)" @ line 25, column 9.
           stage('Kochen (parallel)') {
           ^

2 errors

    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:129)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:123)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:517)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:480)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:269)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:419)
Finished: FAILURE

然后我在Stackoverflow上找到了以下question并尝试应用他们的解决方案,但没有任何方法可以帮助我。

有人可以告诉我是否可以并行运行各个阶段或步骤以及如何使用Jenkins 2.73.1(以及更高版本)实现它?

1 个答案:

答案 0 :(得分:2)

该语法是全新的,仅在管道模型定义插件v 1.2+中可用。

如果您想在该版本之前执行此操作,则语法不是很好。

这是一个较旧语法的示例:https://gist.github.com/abayer/925c68132b67254147efd8b86255fd76