Jenkins pipeline code auto trigger with multiple repositories through GitHub Organization Folder Plugin
Asked Answered
C

1

16

This question is related to the Jenkins job auto trigger with multiple repositories.

Defined 3 repo's to checkout in Jenkinsfile.

 node('slave'){
 git clone github.com/owner/abc.git -b ${env.BRANCH_NAME}
 git clone github.com/owner/def.git -b ${env.BRANCH_NAME}
 git clone github.com/owner/ghi.git -b ${env.BRANCH_NAME}
 }

Configured Jenkins job using Github organization plugin.

In this case my Jenkinsfile is in abc repo and the Jenkins auto trigger is working fine for the abc repo. its not working for other repo's.

Is there anyway to define auto trigger for 2 or more repo's?

Is there any plugin which can auto trigger job for 2 or more repositories?

Do I need to define "checkout scm" different way in Jenkinsfile?

Conradconrade answered 8/8, 2016 at 4:35 Comment(0)
P
10

Yes, you can do that with the Pipeline script from SCM option in a pipeline job by specifying multiple repositories (click on Add Repository button), assuming you can watch the same branch for your 3 repositories, which seems to be your case.

enter image description here

With this configuration (and of course the Poll SCM option activated), a build will be triggered every time a change is made to one of your three repositories.

A few more hints about this solution:

  1. You need a Jenkinsfile in each repository
  2. If you commited on more than one project between two SCM polls the result will be unpredictable (either one of the two projects you just committed in could finally get built) so you should not depend on which project gets built.
  3. To solve previous point and also avoid code duplication, you should probably just load a generic script from each of your Jenkinsfile, such as:

Jenkinsfile in abc/def/ghi:

node {
    // --- Load the generic pipeline ---
    checkout scm: [$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'http://github/owner/pipeline-repo.git']]]
    load 'common-pipeline.groovy'
}()

common-pipeline.groovy script:

{ ->
    node() {
       git clone github.com/owner/abc.git
       git clone github.com/owner/def.git
       git clone github.com/owner/ghi.git            

       // Whatever you do with your 3 repos...
    }
}
Papain answered 10/8, 2016 at 14:10 Comment(5)
Looks like not feasible in my case. Don't want to maintain multiple Jenkinsfiles. Pipeline job not allowing to fetch current branch name with ${env.BRANCH_NAME}. This way its just creating single job & I am looking for 3 jobs Dev, Stage, Prod as per branch name. Just keeping below code in common-pipeline.groovy giving error. <pre> node("slave"){ sh "git clone github/test.git" sh "git clone github/hello.git" sh "ls -la" } </pre> java.lang.NullPointerException: Cannot invoke method call() on null object at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:77)Conradconrade
Jenkins job auto trigger with multiple repositories : how is that not a single job ?? Otherwise, no you can't use ${env.BRANCH_NAME}, the documentation specifies that it's only for multibranches projects. However you can still get it with a small sh script such as sh "echo 'gitBranch=\$(git branch | cut -d ' ' -f 2)' >> properties.txt". For your last problem, please ask another question because your problem is unrelated to your original problem and lacks context.Papain
Thanks for your inputs but its not going to work in my case and I can't move to the pipeline job. Everything is working fine with "GitHub Organization Folder Plugin" except SCM polling for multiple repo's.Conradconrade
What about the case that I need to build different branch for each repo?Lionel
Note that the "Add repository" button is still unintuitively placed in the middle of the form (above "Branches to build") rather than at the very bottom (so still well hidden), but it has been moved to the right-hand side (from the depicted left-hand side).Mcgriff

© 2022 - 2024 — McMap. All rights reserved.