通过选择传入参数,执行不同的任务,如:选择不同的项目名称并发布到不通的的环境。效果如第6步。
1、安装Extended Choice Parameter Plug-In插件
2、创建一个Pipeline项目
3、添加参数
4、配置Extended Choice Parameter
5、添加Pipeline Script
```
def project_name = project_names.replaceAll('"','').split(',')
def deploy_host = deploy_hosts.replaceAll('"','').split(',')
node('office-node') {
env.ENVIRONMENT = 'FIX'
stage('checkout') {
git branch: "${ENVIRONMENT}", url: 'git@****.git'
sh 'git clean -fdx; sleep 4;'
}
stage('build') {
buildpb_for_loop(project_name)
}
stage('部署') {
deploypb_for_loop(deploy_host,project_name)
}
}
def buildpb_for_loop(list) {
def mvnHome = tool 'mvn3.5'
sh "echo 开始编译依赖包"
for (int i = 0; i < list.size(); i++) {
sh "echo 开始编译:${list[i]};cd ${list[i]};ls -l"
sh "cd ${list[i]};${mvnHome}/bin/mvn clean -Dmaven.test.skip=true"
}
}
def deploypb_for_loop(host,list) {
env.deploy_dir = '/dir/deploy/'
env.pro_dir = '/dir/soft/'
env.currnettime = "`date +%Y%m%d%H`"
for (int i = 0; i < host.size(); i++) {
for (int j = 0; j < list.size(); j++) {
if ( "${list[j]}" == "tomcat") {
sh "echo 开始部署服务器:${host[i]} 项目名称:${list[j]}"
sh "ssh -p 2022 -o 'StrictHostKeyChecking no' root@${host[i]} \"mkdir -p ${env.deploy_dir} \""
sh "cd ${list[j]} && scp -P 2022 -o 'StrictHostKeyChecking no' target/${list[j]}-assembly.tar.gz root@${host[i]}:${env.deploy_dir}"
sh "ssh -p 2022 -o 'StrictHostKeyChecking no' root@${host[i]} \"mkdir -p ${env.pro_dir}${list[j]}/${env.currnettime} && tar zxvf ${env.deploy_dir}${list[j]}-assembly.tar.gz -C ${env.pro_dir}${list[j]}/${env.currnettime} && chown -R root:root ${env.pro_dir}${list[j]} && rm -f ${env.deploy_dir}${list[j]}-assembly.tar.gz \""
sh "ssh -p 2022 -o 'StrictHostKeyChecking no' root@${host[i]} \"ln -sfT ${env.pro_dir}${list[j]}/${env.currnettime} /dir/tomcat && sudo systemctl restart tomcat.service \""
} else {
sh "echo 开始部署服务器:${host[i]} 项目名称:${list[j]}"
sh "ssh -p 2022 -o 'StrictHostKeyChecking no' root@${host[i]} \"mkdir -p ${env.deploy_dir} \""
sh "cd ${list[j]} && scp -P 2022 -o 'StrictHostKeyChecking no' target/${list[j]}-assembly.tar.gz root@${host[i]}:${env.deploy_dir}"
sh "ssh -p 2022 -o 'StrictHostKeyChecking no' root@${host[i]} \"mkdir -p ${env.pro_dir}${list[j]}/${env.currnettime} && tar zxvf ${env.deploy_dir}${list[j]}-assembly.tar.gz -C ${env.pro_dir}${list[j]}/${env.currnettime} && chown -R root:root ${env.pro_dir}${list[j]} && rm -f ${env.deploy_dir}${list[j]}-assembly.tar.gz \""
sh "ssh -p 2022 -o 'StrictHostKeyChecking no' root@${host[i]} \"ln -sfT ${env.pro_dir}${list[j]}/${env.currnettime} /dir/${list[j]} && sudo systemctl restart ${list[j]}.service \""
}
}
}
}
```
6、构建