本文介绍了错误:并行步骤只能作为唯一的顶级步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I use this script it works as expected:

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

但是如果包含在管道块中,它会失败并出现错误使用了无效的步骤‘阶段’ - 在此上下文中不允许 - 阶段步骤不能在管道中的步骤块中使用"

But if enclosed in a pipeline block it fails with an Error "Invalid step 'stage' used - not allowed in this context - The stage step cannot be used in step blocks in Pipeline"

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" }
            )
        }
    }
}

知道为什么吗?

推荐答案

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

As you can see nested stages cannot contain further parallel stages themselves in declarative pipelines.

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

I also think it's a bad idea to use parallel in parallel in combination with scripted pipelines because it cannot be displayed correctly in blue ocean.

例如:

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()))
                     } 
                  }
               )
            } 
         },
      )
   }
}

被解释为:

但通常有很好的解决方法.我建议在并行阶段触发一个新的 build.

But as so often there are good workarounds. I would recommend to trigger a new build in a parallel stage.

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'
                     }
                  }
               } 
            }
         }
      }
   }
}

这篇关于错误:并行步骤只能作为唯一的顶级步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 10:40