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

时间:2017-11-10 11:46:30

标签: jenkins jenkins-pipeline

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

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

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

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

1 个答案:

答案 0 :(得分:0)

要实现这一点,我建议创建一个包含两个参数的管道,并根据要构建的repo传递值。 1) GIT BRANCH - 构建和部署所需的分支

2) GIT网址 - 提供检测代码的git网址。

提供参考模板。

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
****
}

在构建Jenkins作业的同时,在管道作业中链接Jenkinsfile并使用参数提供values-build。

希望这会有所帮助。