jenkinsfile
/*
jenkins GUI上的变量配置
String: TAG,REPO_URL,BRANCH,EMAIL,credentialsId,DeployNum,SERVER,PROXY,URL_CHECK
* TAG值:
0:正常发布新版本
1:不打包发包,直接进行远程服务重启
其他,则回退到该tag版本git checkout tag,并发布
* SERVER:服务器列表,以逗号分隔
* DeployNum: 每轮发布数量
* PROXY:
空值: 不进行上下线操作
第1个参数: upstream_name
第2个参数: upstream_port
* URL_CHECK: 检测服务的端口/${uri}组合
*/
env.ENV="" //pro,uat,fat,dev
if (BRANCH == "master"){
ENV = "pro"
}else if(BRANCH == "develop"){
ENV = "fat"
}else if(BRANCH == "release"){
ENV = "uat"
}
env.APP_NAME=JOB_NAME.split("_${BRANCH}_")[0] //全局变量
String upstream_name = ""
String upstream_port = ""
node {
try{
stage('检出代码'){
git branch: "${BRANCH}",credentialsId: "${credentialsId}", url: "${REPO_URL}"
}
SERVER=SERVER.split('"')[1].split(',')
// 循环ip列表,逐个发布
for (String node_ip : SERVER){
if(PROXY != ""){
upstream_name=PROXY.split(',')[0]
upstream_port=PROXY.split(',')[1]
proxy_ip = PROXY.split(',')[2]
offlineServices(node_ip,upstream_name,upstream_port, proxy_ip)
}
if(TAG == "0"){
sendPackage(node_ip)
}else if(TAG != "0" && TAG != "1"){
sh "git checkout ${TAG}"
sendPackage(node_ip)
}
restartService(node_ip)
if(PROXY != ""){
onlineServices(node_ip,upstream_name,upstream_port, proxy_ip)
}
}
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}"
sh 'exit 1'
} finally{
// 清空工作空间
// cleanWs notFailBuild: true
}
}
def sendPackage(String node_ip){
stage("发包: ${node_ip}"){
sh "/usr/bin/rsync -avz --delete --password-file=/etc/rsync_jenkins_gprp_${ENV}.pass solr/ gprp@${node_ip}::${env.APP_NAME}/"
}
}
def restartService(String node_ip){
stage("重启服务${node_ip}"){
sh "sudo salt \"${node_ip}\" --async cmd.run \"/workspace/gprp/${env.APP_NAME}/bin/solr restart\" env='{\"LC_ALL\": \"zh_CN.UTF-8\"}'"
sh "sudo salt ${node_ip} cmd.script salt://4399scripts/gprp/curl_solr.sh"
// 初始化数据
//sh "/var/lib/jenkins/4399scripts/gprp/curl_solr.sh ${node_ip}"
sleep 20
}
}
def offlineServices(String node_ip, String upstream_name, String upstream_port, String proxy_ip){
stage("下线${node_ip}节点"){
sh "/var/lib/jenkins/4399scripts/gprp/gprp_nginx.sh 'down' \"${upstream_name}\" \"${upstream_port}\" \"${node_ip}\" \"${proxy_ip}\""
}
}
def onlineServices(String node_ip, String upstream_name, String upstream_port, String proxy_ip){
stage("检测节点${node_ip}服务,正常则上线"){
sh "/var/lib/jenkins/4399scripts/gprp/gprp_nginx.sh 'up' \"${upstream_name}\" \"${upstream_port}\" \"${node_ip}\" \"${proxy_ip}\""
}
}
gprp_nginx.sh
#!/bin/bash
wan_ip=$4
proxy_ip=$5
lan_ip=`/var/lib/jenkins/4399scripts/gprp/get_lan_ip.sh ${wan_ip}`
curl "http://${proxy_ip}:8887/dynamic?upstream=$2&server=${lan_ip}:$3&$1="
curl_solr.sh
# 考虑到端口暴露的风险,后期检测采用远程脚本执行方式检测服务
# solr的数据载入也在此完成
# curl命令已有删减
#!/bin/bash
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 'localhost:8983/solr/collection6/select?q=%E5%BC%80%E5%BF%83&rows=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"
#node_ip=$1
node_ip='localhost'
_time=`echo $[$(date +%s%N)/1000000]`
curl "http://${node_ip}:83/solr/collectionn=true&wt=json&command=full-import&_=${_time}&verbose=false"