Jenkins 中如何一次构建多个项目
jenkins实用插件1-Active Choices Plugin
如果用Jenkins中的 pipeline 的话,
这个可以用jenkins pipline语法帮忙生成,
1. 同时 build 多个 stage
stage('test') {
build job: 'testPowerShell', parameters: [
[$class: 'StringParameterValue', name: 'serviceNames', value: 'reveal,TestCert'],
[$class: 'StringParameterValue', name: 'filelocation', value: 'D:\\serverlist.json'],
[$class: 'StringParameterValue', name: 'groupNo', value: '0']
]
}
stage('test2') {
build job: 'testPowerShell', parameters: [
[$class: 'StringParameterValue', name: 'serviceNames', value: 'reveal,TestCert'],
[$class: 'StringParameterValue', name: 'filelocation', value: 'D:\\serverlist.json'],
[$class: 'StringParameterValue', name: 'groupNo', value: '0']
]
}
2. 使用 parallel 平行处理
parallel(
job1:{stage('testPowerShell') {
build job: 'testPowerShell', parameters: [
[$class: 'StringParameterValue', name: 'serviceNames', value: 'reveal,TestCert'],
[$class: 'StringParameterValue', name: 'filelocation', value: 'D:\\serverlist.json'],
[$class: 'StringParameterValue', name: 'groupNo', value: '0']
]
}}
,
job2:{stage('TestCopy') {
build job: 'TestCopy', parameters: [
[$class: 'StringParameterValue', name: 'excludeFolders', value: 'D:\\Downloads\\NewReveal\\reveal.js-master\\test D:\\Downloads\\NewReveal\\reveal.js-master\\js']
]
}}
)
将多个 stage 视为同一个 node 会同时启动,会等待较长的 stage 完成后才统一返回结果
3. 在同一个 stage build 多个 job
有基础依赖的 job 先完成后再进行其他 job 的建置
只在 stage 2 传入多个 job 并使用 parallel build
def transformIntoStep(jobFullName) {
return {
build job: jobFullName,
parameters: [string(name: 'Environ', value: 'QA')],
quietPeriod: 3
}
}
stage('Step1') {
build job: 'A01-TestDev', parameters: [
[$class: 'StringParameterValue', name: 'serviceNames', value: 'reveal,TestCert'],
[$class: 'StringParameterValue', name: 'filelocation', value: 'D:\\serverlist.json'],
[$class: 'StringParameterValue', name: 'groupNo', value: '0']
]
}
stage('Step2') {
def branches = [: ]
def jobs = "${buildjobs}".tokenize(',')
def i = 0;
for (jobName in jobs) {
//println(jobName);
branches["job${i}"] = transformIntoStep(jobName);
i++;
}
//println(branches);
parallel branches;
}
stage('Step3') {
build job: 'A01-TestDev', parameters: [
[$class: 'StringParameterValue', name: 'serviceNames', value: 'reveal,TestCert'],
[$class: 'StringParameterValue', name: 'filelocation', value: 'D:\\serverlist.json'],
[$class: 'StringParameterValue', name: 'groupNo', value: '0']
]
}
4.在Jenkins中安装插件
Manage Jenkins --> Manage Plugins,搜索 Extended Choice Parameter
选择 checkbox
Choose Source for Value
Groovy Script
def jobs = jenkins.model.Jenkins.instance.getJobNames()
def matchjobs = jobs.findAll{ name -> name =~/(public-cloud).*/ }
return matchjobs