詹金斯管道与并行

时间:2017-04-04 15:29:16

标签: jenkins jenkins-pipeline

这是我正在尝试执行的Jenkins管道。我正在关注this tutorial

pipeline {
    agent any
    stages {
        stage('one') {
            parallel "first" : {               
                    echo "hello"                
            },
            "second": {                
                    echo "world"            
            }
        }
        stage('two') {
            parallel "first" : {               
                    echo "hello"                
            },
            "second": {                
                    echo "world"            
            }
        }
    }
}

但是这项工作失败了以下信息。

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

WorkflowScript: 12: Unknown stage section "parallel". Starting with version 0.5, steps in a stage must be in a steps block. @ line 12, column 9.
           stage('two') {
           ^

WorkflowScript: 4: Nothing to execute within stage "one" @ line 4, column 9.
           stage('one') {
           ^

WorkflowScript: 12: Nothing to execute within stage "two" @ line 12, column 9.
           stage('two') {
           ^

4 errors

有人可以帮我解释为什么会失败。

3 个答案:

答案 0 :(得分:22)

您需要在舞台声明后添加一个步骤块。

pipeline {
    agent any
    stages {
        stage('one') {
            steps {
                parallel("first": {
                    echo "hello"
                },
                        "second": {
                            echo "world"
                        }
                )
            }
        }
        stage('two') {
            steps {
                parallel("first": {
                    echo "hello"
                },
                        "second": {
                            echo "world"
                        }
                )
            }
        }
    }
}

答案 1 :(得分:0)

您需要将Jenkins上的Declarative Pipeline插件升级到Version 1.2 (Sept 21, 2017)或更高版本

答案 2 :(得分:0)

在声明式管道中,如果您想在步骤内部添加阶段,也可以使用此嵌套。

如果在这种情况下步骤相同,您可以将 step 声明为 function

def steps  = ['first', 'second'] 
def generateSteps(stepLabel) {
    return {
        step("${stepLabel}") {
                echo "Running on ${stepLabel}"
        }
    }
}
def parallelStepMap = steps.collectEntries {
    ["${it}" : generateSteps(it)]
}
pipeline {
    agent any
    stages {
        stage('non-parallel stage') {
            steps {
                echo 'This stage will be executed first.'
            }
        }
 
        stage('parallel stage') {
            steps {
                script {
                    parallel parallelStepMap
                }
            }
        }       
    }
}

一般需要在不同的节点中执行不同的阶段。 在这种情况下,上述函数可以修改为在不同的代理 nodes 上执行,如下所示。

def agents  = ['master', 'agent1', 'agent2']
 
 
def generateStage(nodeLabel) {
    return {
        stage("Runs on ${nodeLabel}") {
            node(nodeLabel) {
                echo "Running on ${nodeLabel}"
            }
        }
    }
}
def parallelStagesMap = agents.collectEntries {
    ["${it}" : generateStage(it)]
}
pipeline {
    agent none
    stages {
        stage('non-parallel stage') {
            steps {
                echo 'This stage will be executed first.'
            }
        }
 
        stage('parallel stage') {
            steps {
                script {
                    parallel parallelStagesMap
                }
            }
        }       
    }
}

在管道启动期间在函数和代理nodelabel中定义none,阶段将在不同的节点中执行。