对几个github存储库使用相同的Jenkins文件

Mor*_*ajb 7 jenkins jenkins-pipeline

我有40-50个github存储库,每个repo包含一个maven工作.

我想为每个存储库创建multibranch管道作业.

我可以为所有项目使用相同的Jenkinsfile,而无需为每个存储库添加Jenkinsfile.(从另一个SCM回购中获取)?

我知道我可以使用共享库来创建一个完整的管道,但我更喜欢更清洁的东西.

Her*_*arn 0

为了实现这一目标,我建议创建一个具有两个参数的管道,并根据要构建的存储库传递值。1) GIT BRANCH - 构建和部署所需的分支

2) GIT URL - 提供 git URL 来签出代码。

提供参考模板。

node('NODE NAME') 
{ 
    withEnv([REQUIRED ENV VARIBALES]) 
    {   withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'CREDENTIALS ID', passwordVariable: 'PW', usernameVariable: 'USER']]) 
        {   try 
                {   stage 'Build' 
                        checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: gitbranch]], doGenerateSubmoduleConfigurations: false, 
                                  extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'CREDENTIALS ID', 
                                  url: 'GIT URL']]]

                                ****
                                MAVEN BUILD
                                ****

                    stage 'Docker Image build & Push'
                                *****
                                DOCKER BUILD AND PUSH TO REPO
                                *****
                }
              catch (err) {
                notify("Failed ${err}")
                currentBuild.result = 'FAILURE'
                }

                stage 'Deploy to ENV'

                *****
                DEPLOYMENT TO REQUIRED ENV
                *****

                notify('Success -Deployed to Environment')

                catch (err) {
                 notify("Failed ${err}")
                currentBuild.result = 'FAILURE'
                } 
        }   
    }
}

def notify(status)
{
****
NOTIFICATION FUCNTION
****
}
Run Code Online (Sandbox Code Playgroud)

在构建 Jenkins 作业时,在管道作业中链接 Jenkinsfile 并提供带有参数的值构建。

希望这可以帮助。