目的
通过Jenkins拉取项目,完成编译打包后,动态部署多个服务
插件安装
通过Extended Choice Parameter这个插件可实现Build参数可传多个
插件的使用
配置
效果
PipeLine脚本
//接收上面脚本定义的DeployServices,并且按,切分
def deployServices = "${DeployServices}".split(',')
pipeline {
agent any
stages {
stage('拉取代码' ) {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master2']], extensions: [], userRemoteConfigs: [[credentialsId: '538da12e-f04b-41f2-adc3-7e1728a5bd1e', url: 'https://gitee.com/RollBack2010/jekins-study.git']]])
}
}
stage('编译代码 ') {
steps {
sh 'mvn clean package'
}
}
stage('发布运行 ') {
steps {
//注意此处,如果是pipeline,必须加script{},否则for语法报错
script {
//循环最上面切分好的数组
for(int i=0;i<deployServices.size();i++){
//再把每个值按照定义好的符号切分
def service=deployServices[i].split("#")[0];
def project=deployServices[i].split("#")[1];
def active=deployServices[i].split("#")[2];
echo "要部署的服务器:${service}"
echo "要上传的目录,因为我是同一台,所以需要这个${project}"
echo "启动时要指定spring.profiles.active=${active}"
sshPublisher(publishers: [sshPublisherDesc(configName: "${service}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: """cd /usr/local/${project}
./start.sh ${active}""", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: 'target/', sourceFiles: 'target/demo-0.0.1-SNAPSHOT.jar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}
}
}
其他
Docker多台部署
原理一样,只不过循环时把替换成了发送docker拉取镜像运行的命令
pipeline与node循环语法差异
pipeline {
agent any
stages {
stage('pipeline循环' ) {
steps {
script {
for(int i=0;i<5;i++) {
echo "a${i}"
}
}
}
}
}
}
node {
stage('node循环') {
for(int i=0;i<5;i++) {
echo "a${i}"
}
}
}