jenkinsfile脚本
node(NODE) {
try{
String ENV="" //pro,uat,fat,dev
String haproxyPre=""
String backup_dir="/data1/gprp/${JOB_NAME}"
String file=""
/*
选择回退到哪个tag版本,TAG值:
0:正常发布新版本
1:不打包,直接进行远程服务重启
2:不打包,发送上一个版本到服务器,并重启
其他,则回退到该tag版本git checkout tag,并发布
*/
echo "TAG: ${TAG}"
//本轮发布计数
DeployCurNum=0
//总共需要发布的节点
DeployAllNum=SERVER.tokenize(',').size()
SERVER=SERVER.split('"')[1].split(',')
//总共剩余未发布的节点
DeployLeftNum=DeployAllNum
node_ip_list=""
int DeployNum= env.DeployNum
if (isSubproject){
file = "./${APP_NAME}/target/${filename}"
}else {
file = "./target/${filename}"
}
if(TAG == "0"){
compile(file, backup_dir)
}else if(TAG != "2" && TAG != "1" && TAG != "0"){
sh "git checkout ${TAG}"
compile(file, backup_dir)
}
for (int i = 0; i < DeployAllNum; i++){
//构造本轮的发布列表
node_ip_list=SERVER[i] + "," + node_ip_list
DeployCurNum=DeployCurNum + 1
DeployLeftNum=DeployLeftNum - 1
//如果本轮当前已下线节点数量少于每一轮需要发布的数量,并且仍有未下线的节点,则继续构造本轮的发布列表
if ( DeployCurNum < DeployNum && DeployLeftNum != 0 ){
continue
}
if(HAPROXY != ""){
haproxyPre=HAPROXY.split(',')[2]
offlineServices(node_ip_list, haproxyPre)
}
if(TAG != "2"){
sendPackage(node_ip_list)
}
restartService(node_ip_list)
if(HAPROXY != ""){
onlineServices(node_ip_list, haproxyPre)
}
DeployCurNum=0
node_ip_list=""
}
stage('通知负责人'){
emailext body: "构建项目:${BUILD_URL}\r\n构建完成", subject: "构建项目${JOB_NAME}【成功】", to: "${EMAIL}"
}
} catch (e) {
echo "${e.toString()}"
emailext body: "构建项目:${BUILD_URL}\r\n构建失败,\r\n错误消息:${e.toString()}", subject: "构建项目${JOB_NAME}【失败】", to: "${EMAIL}"
} finally{
// 清空工作空间
// cleanWs notFailBuild: true
}
}
def compile(String file,String backup_dir){
stage('检出代码'){
git branch: "${BRANCH}",credentialsId: "${credentialsId}", url: "${REPO_URL}"
echo "检测若为master分支时,不可拉取snapshot包或取名包含snapshot的包名"
if (BRANCH == 'master') {
sh '''
if [[ `grep 'SNAPSHOT' pom.xml` ]];then
exit 1
fi
'''
}
}
stage('编译'){
if (BRANCH == "master"){
ENV = "pro"
}else if(BRANCH == "develop"){
ENV = "fat"
}else if(BRANCH == "release"){
ENV = "uat"
}else if(BRANCH == "patch-3"){
ENV = "pro"
}
if(isSubproject){
sh "/usr/local/maven/bin/mvn -Dmaven.test.skip=true -P ${ENV} -pl ${APP_NAME} -am clean package -U"
}else {
sh "/usr/local/maven/bin/mvn -Dmaven.test.skip=true -P ${ENV} clean package -U"
}
}
stage('备份'){
sh "if [[ ! -d ${backup_dir} ]];then mkdir ${backup_dir};fi"
//suf=`date \'+%s\'`
sh "/bin/cp -a ${file} ${backup_dir}/${filename}-`date \'+%s\'`"
}
}
def sendPackage(String node_ip_list){
stage("发包: ${node_ip_list}"){
//echo "/usr/bin/rsync --delete -avz --password-file=/etc/rsync_jenkins_gprp_test.pass ${file} root@${SERVER}::${APP_NAME}/webapps"
String APP_dir=""
if(APP_NAME == 'gprp_cg_app'){
APP_dir = 'gprp_cg'
}else if(APP_NAME == 'gprp_cg_pc'){
APP_dir = 'gprp_cg'
}else {
APP_dir = APP_NAME
}
sh "sudo salt -L \"${node_ip_list}\" cp.get_file salt://${JOB_NAME}/${APP_NAME}/target/$filename /workspace/gprp/${APP_dir}/webapps/${filename}"
}
}
def restartService(String node_ip_list){
stage("重启服务${node_ip_list}"){
//echo "sudo salt ${SERVER} --async cmd.run \"sudo supervisorctl restart ${APP_NAME}\""
String APP_dir=""
if(APP_NAME == 'gprp_cg_app'){
APP_dir = 'gprp_cg'
}else{
APP_dir = APP_NAME
}
sh "sudo salt -L \"${node_ip_list}\" --async cmd.run \"sudo supervisorctl restart ${APP_dir}\""
}
}
def offlineServices(String node_ip_list, String haproxyPre){
stage("下线${node_ip_list}节点"){
List l_node_ip_list = node_ip_list.split(',')
haproxy_place=HAPROXY.split(',')[1]
haproxy_ip=HAPROXY.split(',')[0]
for (node_ip in l_node_ip_list){
haproxySuf=node_ip.split('\\.')[-1]
//sh "haproxySuf=`echo ${node_ip} |cut -d '.' -f 4`;/var/lib/jenkins/4399scripts/gprp/gprp_haproxy45.sh 'maint' '#6' ${haproxyPre}${haproxySuf}"
sh "/var/lib/jenkins/4399scripts/gprp/gprp_haproxy.sh 'maint' \"${haproxy_place}\" \"${haproxyPre}${haproxySuf}\" \"${haproxy_ip}\""
}
}
}
def onlineServices(String node_ip_list, String haproxyPre){
stage("检测节点${node_ip_list}服务,正常则上线"){
List l_node_ip_list = node_ip_list.split(',')
haproxy_place=HAPROXY.split(',')[1]
haproxy_ip=HAPROXY.split(',')[0]
for (String node_ip : l_node_ip_list){
sh "/var/lib/jenkins/4399scripts/gprp/check_url.sh \"http://${node_ip}:${URL_CHECK}\""
haproxySuf=node_ip.split('\\.')[-1]
sh "/var/lib/jenkins/4399scripts/gprp/gprp_haproxy.sh 'ready' \"${haproxy_place}\" \"${haproxyPre}${haproxySuf}\" \"${haproxy_ip}\""
}
}
}
gprp_haproxy.sh
#!/bin/bash
# 简化脚本
# USER PASS IP PORT已隐藏
curl -u ${USER}:${PASS} '${IP}:${PORT}/admin?stats/' -d "action=$1&b=$2&s=$3"
check_url.sh
#!/bin/bash
#xujingbin 20180816
# 传入参数
# $0 url
# $1 timeout
# $2 app_name
signal=1
if [[ $2 != "" ]];then
timeout=$2
else
timeout=40
fi
node_ip=`echo $1 | awk -F '://' '{print $2}' |awk -F ':' '{print $1}'`
log_file="console.`date +%Y_%m_%d`.jetty.log"
app_name=$3
while [[ "$signal" -ne "$timeout" ]];do
sleep 5
curl -s --connect-time 4 -I $1 |grep HTTP |grep 200 > /dev/null && break || echo "wait for server starting .... 5*${signal} 秒" && let signal++
done
if [[ "$signal" -eq "$timeout" ]];then
echo "服务没起来..."
sudo salt ${node_ip} --async cmd.run "tail -200 /workspace/gprp/${app_name}/logs/${log_file}"
exit 1
fi
echo "service start success"