How can I run another task from a jenkins project (jenkinsfile) using GitHub Org Plugin?

How can I start building another job from Jenkinsfile ?

I assume this job is a different repository under the same github organization , which already has its own Jenkins file.

I also want to do this only if the branch name is a master, as it makes no sense to run downstream assemblies of any local branches.

Update:

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

However, when I execute, I get an error

No parameterized job defined with some-down -ream-job-name

I am sure that this work exists in jenkins and is in the same organization folder as the current one. This is another task that has its own Jenkinsfile .

Note that this question is specific to the GitHub Organization Plugin , which automatically creates and maintains tasks for each repository and branch from your GitHub organization.

+113
jenkins groovy jenkins-pipeline jenkins-workflow
Mar 30 '16 at 11:01
source share
5 answers

First of all, this is a waste of the executor slot to wrap the build step in node . Your upstream artist is simply sitting idle for no reason.

Secondly, from a multi-brand project, you can use the BRANCH_NAME environment variable to make the logic conditional for the current branch.

Third, the job parameter accepts the absolute or relative name of the job. If you give a name without any qualifications along the way, this will refer to another task in the same folder, which in the case of a multi-brand project will mean a different branch of the same repository.

So you wanted to write, probably

 if (env.BRANCH_NAME == 'master') { build '../other-repo/master' } 
+116
Mar 31 '16 at 11:23
source share

In addition to the above answers: I wanted to get started with a simple parameter passed to the second pipeline and found the answer at 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]] } 
+98
Sep 23 '16 at 8:40
source share

The build command in the pipeline should run other jobs in jenkins.

Example for github

Work must exist in Jenkins and can be parameterized. As for the branch, I think you can read it from git

+21
Mar 30 '16 at 11:23
source share

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

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

How to use build :

  • job : name of the child job to build. It may be another Pipeline job, but more often it is freestyle or another project.
    • Use a simple name if the job is in the same folder as the upstream job;
    • Instead, you can use relative paths such as ../sister-folder/downstream
    • Or you can use absolute paths like /top-level-folder/nested-folder/downstream

Start another job using branch as parameter

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

In this example, we use 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 } } } 

Start another job using build number as parameter

 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)) ] 

Run many jobs in parallel

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

More information about 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 } } } } 
+3
Jul 01 '19 at 19:16
source share

Use the job build plugin for this task to run other jobs from the jenkins file. You can add a variety of logic to your execution, such as parallel, node and agent parameters, and steps for running external jobs. For this, I gave some easily readable cookbook examples.

1. An example of starting an external job from a jenkins file with a conditional example:

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

2. An example of starting several jobs from a jenkins file with an example condition:

  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') ] } } 
+2
May 29 '19 at 8:15 am
source share



All Articles