234

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.

5 Answers 5

192

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 http://web.archive.org/web/20160209062101/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]] } 
Sign up to request clarification or add additional context in comments.

8 Comments

The build job must be embedded in a step (starting with version 0.5)
Is the the BUILD_NUMBER of the Jenkins job invoked by the build command returned? How to access the BUILD_NUMBER in one of the following stages? Anyone knows where that command is documented?
It's build job: 'freestyle', parameters: [ string(name: 'param1', value:'test_param'), string(name:'dummy', value: "${index}")] nowadays, from jenkins.io/doc/pipeline/examples/#jobs-in-parallel
Interestingly enough build job: 'RunArtInTest', parameters: [[$class: 'StringParameterValue', name: 'systemname', value: systemname]] did not work for me but: build job: 'RunArtInTest', parameters: [string(name: 'systemname', value: "${VALUE}")] worked
|
174

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

7 Comments

Thanks! If by any change you also know on how to trigger this build without waiting for it to finish it would be double-awesome :)
Check Snippet Generator: build job: '../other-repo/master', wait: false
Is there any way to call the build step with a dynamic branch name? Something like build job: '../other-repo/$BRANCH_NAME' where $BRANCH_NAME is the Jenkins environment variable pertaining to the branch running the Multibranch project?
if ${BRANCH_NAME} is available as an environment variable, simple string substitution will do. Just be sure to switch to " from ' for quoting your string. e.g. build job: "../other-repo/${BRANCH_NAME}"
|
99

You can use the build job step from Jenkins Pipeline (Minimum Jenkins requirement: 2.130).

Here's the full API for the build step: https://jenkins.io/doc/pipeline/steps/pipeline-build-step/

How to use build:

  • job: Name of a downstream job to build. May be another Pipeline job, but more commonly a freestyle or other project.
    • Use a simple name if the job is in the same folder as this upstream Pipeline job;
    • You can instead use relative paths like ../sister-folder/downstream
    • Or you can use absolute paths like /top-level-folder/nested-folder/downstream

Trigger another job using a branch as a param

At my company many of our branches include "/". You must replace any instances of "/" with "%2F" (as it appears in the URL of the job).

In this example we're using relative paths

 stage('Trigger Branch Build') { steps { script { echo "Triggering job for branch ${env.BRANCH_NAME}" BRANCH_TO_TAG=env.BRANCH_NAME.replace("/","%2F") build job: "../my-relative-job/${BRANCH_TO_TAG}", wait: false } } } 

Trigger another job using build number as a param

build job: 'your-job-name', parameters: [ string(name: 'passed_build_number_param', value: String.valueOf(BUILD_NUMBER)), string(name: 'complex_param', value: 'prefix-' + String.valueOf(BUILD_NUMBER)) ] 

Trigger many jobs in parallel

Source: https://jenkins.io/blog/2017/01/19/converting-conditional-to-pipeline/

More info on Parallel here: https://jenkins.io/doc/book/pipeline/syntax/#parallel

 stage ('Trigger Builds In Parallel') { steps { // Freestyle build trigger calls a list of jobs // Pipeline build() step only calls one job // To run all three jobs in parallel, we use "parallel" step // https://jenkins.io/doc/pipeline/examples/#jobs-in-parallel parallel ( linux: { build job: 'full-build-linux', parameters: [string(name: 'GIT_BRANCH_NAME', value: env.BRANCH_NAME)] }, mac: { build job: 'full-build-mac', parameters: [string(name: 'GIT_BRANCH_NAME', value: env.BRANCH_NAME)] }, windows: { build job: 'full-build-windows', parameters: [string(name: 'GIT_BRANCH_NAME', value: env.BRANCH_NAME)] }, failFast: false) } } 

Or alternatively:

 stage('Build A and B') { failFast true parallel { stage('Build A') { steps { build job: "/project/A/${env.BRANCH}", wait: true } } stage('Build B') { steps { build job: "/project/B/${env.BRANCH}", wait: true } } } } 

1 Comment

This is fantastic, and the Snippet Generator will actually pull the job for you and provide all the params as fields for you. For me, i wanted to run a new build of the same branch with different params, so it's just build job: BRANCH_NAME, .... I enabled a rudimentary CD flow with this :)
30

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

6 Comments

I tried adding build job: 'jobname but I get this error No parameterized job named jobname found and I can assure you that there is a job with this name at the same level in the organization folder.
Yes there is a job, but that job is not parameterized. I am trying to understand how to parameterize a job created by GitHub organization plugin though
The job name format is: build job: "${ORGANISATION_NAME}/${REPO_NAME}/master"
@SahilAhuja That's totally arbitrary and based on your Jenkins layout, and if you want to use an absolute path you need to start with a /. Relative paths are fine; sorin's problem was probably that the job calling build was a multibranch job, which means a path like jobname would try to build the branch jobname of the same job; it needs to be ../jobname/branchname instead
This works good but, what happen if the branch specified doesn't exists?
|
12

Use build job plugin for that task in order to trigger other jobs from jenkins file. You can add variety of logic to your execution such as parallel ,node and agents options and steps for triggering external jobs. I gave some easy-to-read cookbook example for that.

1.example for triggering external job from jenkins file with conditional example:

if (env.BRANCH_NAME == 'master') { build job:'exactJobName' , parameters:[ string(name: 'keyNameOfParam1',value: 'valueOfParam1') booleanParam(name: 'keyNameOfParam2',value:'valueOfParam2') ] } 

2.example triggering multiple jobs from jenkins file with conditionals example:

 def jobs =[ 'job1Title'{ if (env.BRANCH_NAME == 'master') { build job:'exactJobName' , parameters:[ string(name: 'keyNameOfParam1',value: 'valueNameOfParam1') booleanParam(name: 'keyNameOfParam2',value:'valueNameOfParam2') ] } }, 'job2Title'{ if (env.GIT_COMMIT == 'someCommitHashToPerformAdditionalTest') { build job:'exactJobName' , parameters:[ string(name: 'keyNameOfParam3',value: 'valueOfParam3') booleanParam(name: 'keyNameOfParam4',value:'valueNameOfParam4') booleanParam(name: 'keyNameOfParam5',value:'valueNameOfParam5') ] } } 

4 Comments

But how to receive and use the parameters in 2nd job
@Gentle. You can access passed paramater as params.systemname
is there a possibility for the triggered job to return a return code that indicates whether it was successfully finished or whether it has failed? If yes, please give an example
In this example, how can I start “ def jobs”?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.