首页 > 解决方案 > 在声明性詹金斯的阶段内并行运行

问题描述

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

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

      }
   }
}

标签: jenkinsgroovyjenkins-pipelinejenkins-declarative-pipeline

解决方案


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

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

您还可以编写一个共享库,您可以在所有或某些工作中使用它。

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


推荐阅读