【问题标题】:Jenkins project that checks pull requests from multiple GitHub repositories检查来自多个 GitHub 存储库的拉取请求的 Jenkins 项目
【发布时间】:2021-04-12 23:57:29
【问题描述】:

我正在设置一个 Jenkins 项目来测试 GitHub 组织中的许多存储库。 我的意图是拥有一个 Jenkins 项目,它能够在我的 GitHub 组织中的一组 GitHub 存储库中检查 PR。然后我使用这个项目来触发另一个 Jenkins 项目,该项目在我的 GitHub 存储库上签出/构建/测试代码。

到目前为止,我已经能够设置一个可以在单个 GitHub 存储库上检查 PR 的 Jenkins 项目,但我还没有想出是否有办法检查属于同一 GitHub 组织的多个 GitHub 存储库上的 PR一个 Jenkins 项目。有没有办法做到这一点?

【问题讨论】:

    标签: jenkins github


    【解决方案1】:

    很遗憾,Jenkins Bitbucket 和 Jenkins GitHub 插件不提供监视来自多个 git 存储库的钩子的功能。

    您的问题的可能解决方案包括:

    • 使用分支:我不知道您尝试将哪种代码组织到存储库中,但根据我的经验,有些人经常尝试将属于同一项目的代码组织到不同的存储库中,而不是组织到不同的存储库中相同的 git 存储库。也许这可以解决您的问题?

    • 拥有一个克隆其他 git 存储库的管道:使用 Jenkins 声明式管道时,您可以使用 checkout()git() 函数。看下面的例子:

    // 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']]])
                    }
                }
            }
        }
    }
    
    • 有两条管道。一个主要的(我们称之为 PIPELINE-A),它将被钩子调用,一个辅助(我们称之为 PIPELINE-B),它将克隆其他存储库并做一些有趣的事情:
    // 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']]])
                    }
                }
            }
        }
    }
    

    使用此解决方案,您可以将每个存储库克隆到不同的文件夹中。因此,在那之后,您应该在对该项目执行任何特定任务之前对该文件夹执行cd。为了更好地组织,您可以为不同的存储库设置不同的管道,如果您愿意,可以包含其任务,但作为最后两种解决方案的结合,您将不得不选择一个主存储库来触发您的工作。

    最好的问候!

    【讨论】:

    • 感谢您的回答。我的 GitHub 存储库实际上托管着不同的代码,每个代码库都有自己的分支/标签列表。对于您所说的每个存储库的样子,我需要一个专用的 Jenkins 项目来检查该存储库的 PR,然后触发另一个 Jenkins 项目来测试我的存储库中的代码。就目前而言,我已经拥有了测试通过 Execute Shell 运行的代码所需的一切,但我喜欢管道的想法,在某些时候我可以切换到它。
    • 如果你使用 Cucumber 作为你的测试工具,你可以通过环境变量来控制它的行为。这样,您可以拥有一个 Cucumber 项目来测试所有其他项目,这些项目将接受 Jenkins 管道中的参数、由其他管道触发或独立地触发。
    猜你喜欢
    • 2016-07-21
    • 2022-08-24
    • 2016-01-15
    • 1970-01-01
    • 2016-12-26
    • 2012-02-17
    • 2019-12-21
    • 2020-02-13
    • 2017-12-01
    相关资源
    最近更新 更多