How to implement build after another project is built through a Jenkins file

3.3k Views Asked by At

For a project A I want to trigger it's build when B is built successfully.I can achieve this through the Jenkins console by selecting the option from Build trigger.

Now I wanna achieve this by writing corresponding steps in Jenkins file of project A.

What steps can be used I tried triggers{ upstream('B',hudson.model.Result.SUCCESS) }

2

There are 2 best solutions below

2
Dbercules On

From https://www.jenkins.io/doc/book/pipeline/syntax/#triggers :

The upstream trigger "accepts a comma-separated string of jobs and a threshold. When any job in the string finishes with the minimum threshold, the Pipeline will be re-triggered. For example: triggers { upstream(upstreamProjects: 'job1,job2', threshold: hudson.model.Result.SUCCESS) }"

To trigger a build of Project A after Project B has been built successfully, Project A's JenkinsFile might look like:

properties(
  [pipelineTriggers(
    [upstream(
        upstreamProjects: 'project_b_job_path',
        threshold: hudson.model.Result.SUCCESS         
    )]
  )]
)

where project_b_job_path is equal to the absolute or relative path of the job, for example:

  • projects/project_b/master
  • ../project_b/master
  • test_branch
1
Aditya Nair On

Just a suggestion, you can also make the change for this only in project B. Add a step in the post actions of project B to build project A when B is successful:

PROJECT B:

pipeline{
    agent{
        label "node"
    }
    stages{
        stage("A"){
            steps{
                echo "========executing A========"
            }
        }
    }
    post{
        success{
            build propagate: false, job: 'A'
        }
    }
}

post {} will be execute when project B is built and the code inside success {} will only be executed when project B is successful. build propagate: false, job: 'A' will call project A. propogate: false ensures that project B does not wait for project A's completion and simply invokes it.