简体   繁体   中英

Jenkins project that checks pull requests from multiple GitHub repositories

I'm setting a Jenkins project to test many repositories in a GitHub organization. My intent is to have a single Jenkins project that is able to check for PRs in a set of GitHub repos in my GitHub organization. Then I use this project to trigger another Jenkins project that checkout/build/test code on my GitHub repos.

So far I have been able to setup a Jenkins project that can check PR on a single GitHub repo, but I have not figured out if there is a way to check for PRs on multiple GitHub repos belonging to the same GitHub organization through a single Jenkins project. Is there a way to achieve this?

Jenkins Bitbucket and Jenkins GitHub plugins unfortunately don't offer the feature of watching for hooks from multiple git repositories.

Possible solutions to your problem include to:

  • Work with branches: I don't know which kind of code are you trying to organize into repositories, but by my experience, some people often try to organize code belonging to the same project into different repositories rather then into different branches of the same git repository. Maybe this could be an approach that solves your problem?

  • Have one pipeline that clones the other git repositories: When using Jenkins declarative pipelines, you could use the checkout() and the git() functions. Look at the example below:

// Single pipeline example
pipeline {
    agent any

    stages {
        stage("I'm just printing a message in here") {
            steps {
                script {
                    print('Yep, just printing some happy message')
                }
            }
        }

        stage("Cloning repository A") {
            steps {
                script {
                    checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'my-repo-a']], userRemoteConfigs: [[credentialsId: 'MY-GIT-CREDENTIALS', url: 'https://github.com/my-user/my-repo-a.git']]])
                }
            }
        }
    }
}
  • Have two pipelines. A main (let's call it PIPELINE-A) which will be called by the hook, and a secondary (let's call it PIPELINE-B), which will clone the other repositories and do some fun stuff:
// PIPELINE-A
pipeline {
    agent any

    stages {
        stage('Calling PIPELINE-B') {
            steps {
                script {
                    build 'pipeline-b'
                }
            }
        }
    }
}
// PIPELINE-B
pipeline {
    agent any

    stages {
        stage('Cloning Git repository') {
            steps {
                script {
                    checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'my-repo-a']], userRemoteConfigs: [[credentialsId: 'MY-GIT-CREDENTIAL', url: 'https://github.com/my-user/my-repo-a.git']]])

                    checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'my-repo-b']], userRemoteConfigs: [[credentialsId: 'MY-GIT-CREDENTIAL', url: 'https://github.com/my-user/my-repo-b.git']]])
                }
            }
        }
    }
}

With this solution, you're gonna have each repository cloned in a distinct folder. So after that, you should do a cd to that folder before you do any specific tasks to that project. For better organization, you can have distinct pipelines for distinct repositories, containing its tasks if you want to, but as a con of these two last solutions, you're gonna have to pick a main repository to trigger your jobs.

Best regards!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM