jenkins 的Pipeline 使用创建(基于jenkins的流水线自动构建发布)
配置服务器之间的免密登录(可百度)
1、配置项目参数
2、配置Pipeline script脚本
可自行学习groovy语法
node{
stage("checkOut"){
git branch: "${branch}",
credentialsId: '09fdfa28-6c9f-4bb4-bfb6-d6b9d2434be7',
url: 'https://xxxxxx.git'
}
}
def jarMap = ['xxx': ['host']]
pipeline {
agent any
parameters {
choice(
description: 'branch',
name: 'branch',
choices: ['test']
)
choice(
description: 'build',
name: 'build',
choices: ['yes', 'no']
)
choice(
description: 'Choose a jar',
name: 'serverName',
choices: ['serverName']
)
}
stages {
stage('Build') {
steps {
script {
if(build.equals('yes')) {
sh '/home/maven/bin/mvn clean package -Dmaven.test.skip=true'
}
}
}
}
stage('deploy') {
steps {
script {
def addressArray = jarMap[serverName]
// def month = new Date().format('yyyyMM')
// def version = new Date().format('yyyyMMddHHmmss')
for(String serviceAddress in addressArray) {
if(serverName.equals('xxx')) {
// sh "mkdir -p /home/serverTag/${month}/sdx-gateway/${version}"
// sh "cp target/${serverName} /home/serverTag/${month}/sdx-gateway/${version}"
sh "scp target/${serverName} root@${serviceAddress}:/home"
} else {
sh "echo The server target is not configured!"
}
def pid = sh returnStdout: true ,script: "ssh root@${serviceAddress} ps -ef | grep \"${serverName}\" | grep -v \"grep\" | awk '{print \$2}'"
if(pid) {
def killPid = ''
def pidArray = pid.split("\n")
for(def temp : pidArray) {
killPid += 'kill -9 ' + temp + ';'
}
sh "ssh root@${serviceAddress} '${killPid}'"
sleep 5
}
sh "ssh root@${serviceAddress} '(cd /home ;source /etc/profile;BUILD_ID=dontKillMe; nohup java -jar /home/${serverName} --spring.profiles.active=test >/dev/null 2>&1 &)'"
sleep 5
}
}
}
}
}
}