2
votes

So, I want to run my parallel stages inside a stage but I also want to write some shared code by each parallel stage which I have written in steps of parallel parent stage The problem I faced is that that the parallel stages are not being run

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

      }
   }
}
1

1 Answers

0
votes

For executing shared code you can define variables and functions outside of the 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}”
        }
      }
    }
  }
}

You can also write a shared library, which you can use in all or certain jobs.

I’ve deleted my first answer, since it was pure BS.