在声明性詹金斯中,在一个步骤的内部并行运行

问题描述 投票:1回答:1

所以,我想在一个阶段内运行并行阶段,但我也想在并行父阶段的步骤中编写的每个并行阶段都编写一些共享代码我面临的问题是并行阶段没有运行

stages {
   stage('partent stage 1'){
      something here
   }
   stage('parent stage 2') {
      steps {
         // common code for parallel stages

         parallel {
            stage ('1'){
               // some shell command
            }
            stage('2') {
               // some shell command
            }
         }

      }
   }
}
jenkins groovy jenkins-pipeline jenkins-declarative-pipeline
1个回答
0
投票

对于执行共享代码,您可以在声明性管道之外定义变量和函数:

def foo = true

def checkFoo {
  return foo
}

pipeline {
  stage('parallel stage') {
    parallel {
      stage('stage 1') {
        steps {
          script {
            def baz = checkFoo()
          }
          sh “echo ${baz}”
        }
      }
      stage('stage 2') {
        steps {
          script {
            def baz = checkFoo()
          }
          sh “echo ${baz}”
        }
      }
    }
  }
}

You can also write a shared library,您可以在所有或某些作业中使用。

我已经删除了我的第一个答案,因为它是纯BS。

© www.soinside.com 2019 - 2024. All rights reserved.