How can I trigger another job from a jenkins pipel

2019-01-12 18:09发布

How can I trigger build of another job from inside the Jenkinsfile?

I assume that this job is another repository under the same github organization, one that already has its own Jenkins file.

I also want to do this only if the branch name is master, as it doesn't make sense to trigger downstream builds of any local branches.

Update:

stage 'test-downstream'
node {
     def job = build job: 'some-downtream-job-name'
}

Still, when executed I get an error

No parameterized job named some-downtream-job-name found

I am sure that this job exists in jenkins and is under the same organization folder as the current one. It is another job that has its own Jenkinsfile.

Please note that this question is specific to the GitHub Organization Plugin which auto-creates and maintains jobs for each repository and branch from your GitHub Organization.

3条回答
贼婆χ
2楼-- · 2019-01-12 18:49

First of all, it is a waste of an executor slot to wrap the build step in node. Your upstream executor will just be sitting idle for no reason.

Second, from a multibranch project, you can use the environment variable BRANCH_NAME to make logic conditional on the current branch.

Third, the job parameter takes an absolute or relative job name. If you give a name without any path qualification, that would refer to another job in the same folder, which in the case of a multibranch project would mean another branch of the same repository.

Thus what you meant to write is probably

if (env.BRANCH_NAME == 'master') {
    build '../other-repo/master'
}
查看更多
迷人小祖宗
3楼-- · 2019-01-12 18:51

In addition to the above mentioned answers: I wanted to start a job with a simple parameter passed to a second pipeline and found the answer on https://dzone.com/refcardz/continuous-delivery-with-jenkins-workflow.

So i used:

stage ('Starting ART job') {
    build job: 'RunArtInTest', parameters: [[$class: 'StringParameterValue', name: 'systemname', value: systemname]]
}
查看更多
老娘就宠你
4楼-- · 2019-01-12 19:02

The command build in pipeline is there to trigger other jobs in jenkins.

Example on github

The job must exist in Jenkins and can be parametrized. As for the branch, I guess you can read it from git

查看更多
登录 后发表回答