我可以通过多分支管道插件自动加载Jenkinsfile,每个分支只有一个jenkinsfile限制。
我想要加载每个分支多个Jenkinsfiles,我已经尝试使用以下方法创建主Jenkins文件并加载特定文件。在下面的代码中,它将1.Jenkinsfile
和2.Jenkinsfile
合并为一个管道。
node {
git url: 'git@bitbucket.org:xxxxxxxxx/pipeline.git', branch: 'B1P1'
sh "ls -latr"
load '1.Jenkinsfile'
load '2.Jenkinsfile'
}
有没有办法可以从一个分支单独加载多个Jenkins管道代码?
答案 0 :(得分:1)
我写了一个包含以下文件的共享库(ref 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'
}
答案 1 :(得分:0)
很可能你想要映射旧詹金斯&#39;在某个项目的单个分支上运行的作业(预管道)到单个多分支管道。适当的方法是创建依赖于输入的阶段(如问题用户,如果他/她想部署到登台/现场)。
或者你可以创建一个新的单独的Pipeline jenkins作业,它实际引用你的项目的SCM并指向你的其他Jenkins文件(然后是每个其他jenkins文件的一个管道作业)。