How to invoke a jenkins pipeline A in another jenkins pipeline B

a 夏天 提交于 2019-12-17 17:36:41

问题


I have two Jenkins pipelines, let's say pipeline-A and pipeline-B. I want to invoke pipeline-A in pipeline-B. How can I do this?

(pipeline-A is a subset of pipeline-B. Pipeline-A is responsible for doing some routine stuff which can be reused in pipeline-B)

I have installed Jenkins 2.41 on my machine.


回答1:


A little unclear if you want to invoke another pipeline script or job, so I answer both:

Pipeline script The "load" step will execute the other pipeline script. If you have both scripts in the same directory, you can load it like this:

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

Other script (pipeline_a.groovy):

def someMethod() {
    //do something
}

return this

Pipeline job

If you are talking about executing another pipeline job, the "build job" step can accomplish this:

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

propagate: Propagate errors

wait: Wait for completion

If you have paramters on the job, you can add them like this:

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



回答2:


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



回答3:


To add to what @matias-snellingen said. If you have multiple functions, the 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
}

The someMethod() is the one that will be called in the main pipeline script




回答4:


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

package name.of.package
import groovy.json.*

def myFunc(var1) {
return result
}

Than consume it

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

hope it helps




回答5:


As mentioned by @Matias Snellingen and @Céline Aussourd, in the case of launching a multibranch job you have to specify the branch to build like this :

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

In my case it solved the problem.



来源:https://stackoverflow.com/questions/43337070/how-to-invoke-a-jenkins-pipeline-a-in-another-jenkins-pipeline-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!