Rahul
Rahul

Reputation: 849

How to make a Jenkins pipeline triggered by a pull request to build all the repositories in a Github project

I want to set up a pipeline that is triggered automatically by pull requests for a GitHub project and then builds all the repositories in it. I found this article and followed the instructions as it was similar to what I required, but I'm currently stuck in getting the pipeline to trigger and build multiple repositories in the same GitHub project every time a PR is made for even one of the repositories.

I've attached this diagram to bring more clarity into my issue.

So the goal is when a pull request is made on the Branch 3 of Repository 1 the pipeline is triggered which builds that branch and all the other repositories in a specified order, i.e. Repository 2, Repository 3, etc. of the Working Project.

Your help would be much appreciated and I think a solution for this would benefit the CI DevOps community very much. Thanks!

Upvotes: 0

Views: 1019

Answers (1)

Matt Hayward
Matt Hayward

Reputation: 11

Give the following a try - can't promise the below will be exact but should get you in the right direction.

The first thing you want to do is have a consistent Jenkinsfile across each of the repositories, now you could do this by a number of different manners but one way to accomplish this would be to use external groovy pipelines so that the logic can be kept consistent across the repos. An example of this is located here.. Copying the Jenkinsfile across each of the repositories would also work, however a single source of truth is generally a better approach.

node{
    deleteDir()

    git env.flowScm
    def flow = load 'pipeline.groovy'
    stash includes: '**', name: 'flowFiles'

    stage 'Checkout'
    checkout scm // short hand for checking out the "from scm repository"

    flow.runFlow()
}

Where the pipeline.groovy file would contain the actual pipeline would look like this:

def runFlow() {
    // your pipeline code

} 

// Has to exit with 'return this;' in order to be used as library
return this;

Once you've got each of your triggers using the same pipeline logic you can take advantage of the dir command to clone and work with repositories that are not the one that triggered the build. An example of this is located here.

node('ATLAS && Linux') {
    dir('CalibrationResults') {
        git url: 'https://github.com/AtlasBID/CalibrationResults.git'
    }
    dir('Combination') {
        git url: 'https://github.com/AtlasBID/Combination.git'
    }
    dir('CombinationBuilder') {
        git url: 'https://github.com/AtlasBID/CombinationBuilder.git'
    }

    sh('ls')
    sh('. CombinationBuilder/build.sh')
}

Putting the two steps together should achieve what you are after in this instance.

Upvotes: 1

Related Questions