How to call a Jenkins pipe A in another Jenkins pipe B

I have two Jenkins pipelines, say pipeline-A and pipeline-B. I want to call pipe-A to pipe-B. How can i do this?

(pipe-A is a subset of pipe-B. Pipeline-A is responsible for performing some routine operations that can be reused in pipe-B)

I installed Jenkins 2.41 on my machine.

+48
source share
5 answers

The following solution works for me:

pipeline { agent { node { label 'master' customWorkspace "${env.JobPath}" } } stages { stage('Start') { steps { sh 'ls' } } stage ('Invoke_pipeline') { steps { build job: 'pipeline1', parameters: [ string(name: 'param1', value: "value1") ] } } stage('End') { steps { sh 'ls' } } } } 
+32
source

It's a little unclear if you want to reference the script or the work of another script, so I answer both:

Pipeline script The "load" step will execute another pipeline script. If you have both scripts in the same directory, you can load it as follows:

 def pipelineA = load "pipeline_A.groovy" pipelineA.someMethod() 

Another script (pipe_a.groovy):

 def someMethod() { //do something } return this 

Pipeline work

If you are talking about doing another job on the pipeline, the "build job" step can do the following:

 build job: '<Project name>', propagate: true, wait: true 

distribute: distribute errors

wait: Wait for completion

If you have work options, you can add them as follows:

 build job: '<Project name>', parameters: [[$class: 'StringParameterValue', name: 'param1', value: 'test_param']] 
+45
source

To add to what @ matias-snellingen said. If you have multiple functions, return this should be under the function that will be called in the main pipeline script. For example, in:

 def someMethod() { helperMethod1() helperMethod2() } return this def helperMethod1(){ //do stuff } def helperMethod2(){ //do stuff } 

someMethod() - the one that will be called in the script of the main pipeline

+1
source

Another option is to create a package, download it, and execute from the package.

 package name.of.package import groovy.json.* def myFunc(var1) { return result } 

How to consume it

 @Library('name_of_repo') import name.of.package.* utils = new name_of_pipeline() // here you can invoke utils.myFunc(var) 

Hope help

0
source

As @Matias Snellingen and @ CΓ©line Aussourd mentioned, if you are running a multi-industry task, you must specify the branch to build as follows:

 stage ('Invoke_pipeline') { steps { build job: 'pipeline1/master', parameters: [ string(name: 'param1', value: "value1") ] } } 

In my case, this solved the problem.

0
source

Source: https://habr.com/ru/post/1272198/


All Articles