错误:并行步骤只能用作唯一的顶级步骤

时间:2016-10-11 07:48:10

标签: jenkins jenkins-pipeline

当我使用这个脚本时,它按预期工作:

stage("B") {
    parallel (
        'C' : { 
            stage ("BC") {
                parallel (
                    'E' : {
                        stage("Many") {
                            println "E1"
                            println "E2"
                        }
                    },
                    'F' : { println "F" }
                )
            }
        },
        'D' : { println "D" }
    )
}

但如果包含在管道块中,它将失败并显示错误“无效步骤'阶段' - 在此上下文中不允许 - 阶段步骤不能在管道中的步骤块中使用”

pipeline {
    agent label:"debian"
    stages {
        stage("B") {
            parallel (
                'C' : { 
                    stage ("BC") {
                        parallel (
                            'E' : {
                                stage("Many") {
                                    println "E1"
                                    println "E2"
                                }
                            },
                            'F' : { println "F" }
                        )
                    }
                },
                'D' : { println "D" }
            )
        }
    }
}

知道为什么吗?

3 个答案:

答案 0 :(得分:1)

如您所见,嵌套阶段在声明式管道中本身不能包含更多并行阶段。

我还认为将并行并行与脚本化管道结合使用是一个坏主意,因为它无法在蓝色海洋中正确显示。

例如:

import java.text.SimpleDateFormat

node() {
   stage('Build') {
      parallel (
         "win7-vs2012" : { 
            stage("checkout (win7-vs2012)") { 
               println 'checkout Win'
               sleep(20)
               println(new SimpleDateFormat("HH:mm:ss").format(new Date()))
            };
            stage("build (win7-vs2012)") { 
               println 'Build Win'
               sleep(45)
               println(new SimpleDateFormat("HH:mm:ss").format(new Date()))
            }; 
            stage("test (win7-vs2012)") { 
               parallel (
                  "test1 (win7-vs2012)" : {
                     stage("test1 (win7-vs2012)") { 
                        println 'TestRunner 1'
                        sleep(1)
                        println(new SimpleDateFormat("HH:mm:ss").format(new Date()))
                     } 
                  },
                  "test2 (win7-vs2012)" : {
                     stage("test2 (win7-vs2012)") { 
                        println 'TestRunner 2'
                        sleep(1)
                        println(new SimpleDateFormat("HH:mm:ss").format(new Date()))
                     } 
                  },
                  "test3 (win7-vs2012)" : {
                     stage("test3 (win7-vs2012)") { 
                        println 'TestRunner 3'
                        sleep(1)
                        println(new SimpleDateFormat("HH:mm:ss").format(new Date()))
                     } 
                  }
               )
            } 
         },
         "win10-vs2015" : { 
            stage("checkout (win10-vs2015)") { 
               println 'checkout Win'
               sleep(15)
               println(new SimpleDateFormat("HH:mm:ss").format(new Date()))
            };
            stage("build (win10-vs2015)") { 
               println 'Build Win'
               sleep(40)
               println(new SimpleDateFormat("HH:mm:ss").format(new Date()))
            }; 
            stage("test (win10-vs2015)") { 
               parallel (
                  "test1 (win10-vs2015)" : {
                     stage("test1 (win10-vs2015)") { 
                        println 'TestRunner 1'
                        sleep(1)
                        println(new SimpleDateFormat("HH:mm:ss").format(new Date()))
                     } 
                  },
                  "test2 (win10-vs2015)" : {
                     stage("test2 (win10-vs2015)") { 
                        println 'TestRunner 2'
                        sleep(1)
                        println(new SimpleDateFormat("HH:mm:ss").format(new Date()))
                     } 
                  },
                  "test3 (win10-vs2015)" : {
                     stage("test3 (win10-vs2015)") { 
                        println 'TestRunner 3'
                        sleep(1)
                        println(new SimpleDateFormat("HH:mm:ss").format(new Date()))
                     } 
                  }
               )
            } 
         },
         "linux-gcc5" : { 
            stage("checkout (linux-gcc5)") { 
               println 'checkout Win'
               sleep(10)
               println(new SimpleDateFormat("HH:mm:ss").format(new Date()))
            };
            stage("build (linux-gcc5)") { 
               println 'Build Win'
               sleep(20)
               println(new SimpleDateFormat("HH:mm:ss").format(new Date()))
            }; 
            stage("test (linux-gcc5)") { 
               parallel (
                  "test1 (linux-gcc5)" : {
                     stage("test1 (linux-gcc5)") { 
                        println 'TestRunner 1'
                        sleep(2)
                        println(new SimpleDateFormat("HH:mm:ss").format(new Date()))
                     } 
                  },
                  "test2 (linux-gcc5)" : {
                     stage("test2 (linux-gcc5)") { 
                        println 'TestRunner 2'
                        sleep(2)
                        println(new SimpleDateFormat("HH:mm:ss").format(new Date()))
                     } 
                  },
                  "test3 (linux-gcc5)" : {
                     stage("test3 (linux-gcc5)") { 
                        println 'TestRunner 3'
                        sleep(2)
                        println(new SimpleDateFormat("HH:mm:ss").format(new Date()))
                     } 
                  }
               )
            } 
         },
      )
   }
}

被解释为:

Totally confusing and no visibility of execution order

但是通常有很好的解决方法。我建议同时触发一个新的build

pipeline {
   agent none
   stages {
      stage("Example") {
         failFast true
         parallel {
            stage("win7-vs2012") {
               agent {
                  label "win7-vs2012"
               }
               stages {
                  stage("checkout (win7-vs2012)") {
                     steps {
                        echo "win7-vs2012 checkout"
                     }
                  } 

                  stage("build (win7-vs2012)") {
                     steps {
                        echo "win7-vs2012 build"
                     }
                  }

                  stage("test (win7-vs2012)") {
                     steps {
                        build 'test-win7-vs2012'
                     }
                  }
               } 
            }

            stage("win10-vs2015") {
               agent {
                  label "win10-vs2015"
               }
               stages {
                  stage("checkout (win10-vs2015)") {
                     steps {
                        echo "win10-vs2015 checkout"
                     }
                  } 

                  stage("build (win10-vs2015)") {
                     steps {
                        echo "win10-vs2015 build"
                     }
                  }

                  stage("test (win10-vs2015)") {
                     steps {
                        build 'test-win10-vs2015'
                     }
                  }
               } 
            }

            stage("linux-gcc5") {
               agent {
                  label "linux-gcc5"
               }
               stages {
                  stage("checkout (linux-gcc5)") {
                     steps {
                        echo "linux-gcc5 checkout"
                     }
                  } 

                  stage("build (linux-gcc5)") {
                     steps {
                        echo "linux-gcc5 build"
                     }
                  }

                  stage("test (linux-gcc5)") {
                     steps {
                        build 'test-linux-gcc5'
                     }
                  }
               } 
            }
         }
      }
   }
}

答案 1 :(得分:0)

不知道这是否仍然是您的问题;但是搜索引擎仍然会针对相关查询提出这个问题...

据我了解的docs,您不能嵌套parallel指令:

  

嵌套的stages本身不能再包含parallel个阶段,...

答案 2 :(得分:-1)

declarative pipeline syntax中需要steps部分。