我可以通过多分支管道插件自动加载 Jenkinsfile,但每个分支只能加载一个 jenkinsfile。
我想加载每个分支有多个 Jenkinsfile,我通过创建主 Jenkins 文件并加载特定文件尝试了以下方法。在下面的代码中,它将 1.Jenkinsfile
和 2.Jenkinsfile
合并为一个管道。
node {
git url: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3d4dac7f3d1dac7d1c6d0d8d6c79ddcc1d4" rel="noreferrer noopener nofollow">[email protected]</a>:xxxxxxxxx/pipeline.git', branch: 'B1P1'
sh "ls -latr"
load '1.Jenkinsfile'
load '2.Jenkinsfile'
}
有没有一种方法可以从一个分支单独加载多个 Jenkins 管道代码?
最佳答案
我编写了一个共享库(引用 https://jenkins.io/doc/book/pipeline/shared-libraries/ ),其中包含以下文件(在 vars/generateJobsForJenkinsfiles.groovy 中):
/**
* Creates jenkins pipeline jobs from pipeline script files
* @param gitRepoName name of github repo, e.g. <organisation>/<repository>
* @param filepattern ant style pattern for pipeline script files for which we want to create jobs
* @param jobPath closure of type (relativePathToPipelineScript -> jobPath) where jobPath is a string of formated as '<foldername>/../<jobname>' (i.e. jenkins job path)
*/
def call(String gitRepoName, String filepattern, def jobPath) {
def pipelineJobs = []
def base = env.WORKSPACE
def pipelineFiles = new FileNameFinder().getFileNames(base, filepattern)
for (pipelineFil in pipelineFiles) {
def relativeScriptPath = (pipelineFil - base).substring(1)
def _jobPath = jobPath(relativeScriptPath).split('/')
def jobfolderpath = _jobPath[0..-2]
def jobname = _jobPath[-1]
echo "Create jenkins job ${jobfolderpath.join('/')}:${jobname} for $pipelineFil"
def dslScript = []
//create folders
for (i=0; i<jobfolderpath.size(); i++)
dslScript << "folder('${jobfolderpath[0..i].join('/')}')"
//create job
dslScript << """
pipelineJob('${jobfolderpath.join('/')}/${jobname}') {
definition {
cpsScm {
scm {
git {
remote {
github('$gitRepoName', 'https')
credentials('github-credentials')
}
branch('master')
}
}
scriptPath("$relativeScriptPath")
}
}
configure { d ->
d / definition / lightweight(true)
}
}
"""
pipelineJobs << dslScript.join('\n')
//println dslScript
}
if (!pipelineJobs.empty)
jobDsl sandbox: true, scriptText: pipelineJobs.join('\n'), removedJobAction: 'DELETE', removedViewAction: 'DELETE'
}
关于jenkins - 多分支 Pipeline 插件每个分支加载多个 jenkinsfile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41146952/