Jenkins- CICD流水線 python/Java代碼升級(jí)與回退
1、執(zhí)行思路
1.1、代碼升級(jí)
jenkins上點(diǎn)擊 upgrade和 代碼版本號(hào) --${tag}
jenkins 推送 代碼 和 執(zhí)行腳本 到目標(biāo)服務(wù)器/opt目錄下
執(zhí)行命令 sh run.sh 代碼名稱 版本號(hào) upgrade
版本號(hào) 來(lái)自jenkins的 構(gòu)建參數(shù)中的 標(biāo)簽參數(shù),標(biāo)簽值從gitee處獲取
run.sh 升級(jí)代碼執(zhí)行邏輯
1、在目標(biāo)服務(wù)器上 新代碼名稱+標(biāo)簽 cp 到 bak目錄下
2、停服,新代碼 mv 到執(zhí)行目錄下,啟動(dòng)
1.2、代碼回退
jenkins上點(diǎn) rollbak 和 代碼版本號(hào)
run.sh 回退代碼執(zhí)行邏輯
停服,cp bak/代碼-版本號(hào) 到執(zhí)行目錄,啟動(dòng)
2、gitee代碼如下
run.sh 執(zhí)行腳本
test_flask.py python代碼
2.1、執(zhí)行腳本run.sh代碼如下
#!/bin/bash
# cd `dirname $0`
APP_NAME=$1
APP_file=/opt/test/${APP_NAME}
tag=$2
#使用說(shuō)明,用來(lái)提示輸入?yún)?shù)
usage() {
echo "Usage: ./run.sh [start|stop|status|restart|upgrade]"
exit 1
}
#檢查程序是否在運(yùn)行
is_exist(){
# pid=`jps -l|grep $APP_NAME|grep -v grep|awk '{print $1}'` >> 此次獲取java進(jìn)程pid <<
pid=`curl localhost:10080/pid 2>/dev/null`
#如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
echo "pid不存在"
return 1
else
return 0
fi
}
#啟動(dòng)方法
start(){
echo "${APP_file} is starting ..."
is_exist
if [ $? -eq 0 ]; then
echo "${APP_file} is already running. pid=${pid}"
else
# nohup java -Xmx100m -Xms100m -jar ${APP_file} --server.port=8181 >/dev/null 2>&1 & >> 此處啟動(dòng)java jar包 <<
nohup python3 test/test_flask.py &
echo "START..."
sleep 5
is_exist
if [ $? -eq 0 ]; then
echo "${APP_file} is running success. pid=${pid}"
fi
fi
}
#停止方法
stop(){
echo "${APP_file} is stopping ..."
is_exist
if [ $? -eq "0" ]; then
kill -15 $pid
echo "..."
sleep 2
is_exist
if [ $? -eq 0 ]; then
echo "${APP_file} still in the running. pid=${pid}"
else
echo "${APP_file} has stopped running."
fi
else
echo "${APP_file} is not running"
fi
}
#輸出運(yùn)行狀態(tài)
status(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_file} is running. Pid is ${pid}"
else
echo "${APP_file} is NOT running."
fi
}
#重啟
restart(){
echo "${APP_file} is restarting ..."
stop
#sleep 5
start
}
#程序升級(jí)
#upgrade(){
# ./run.sh stop
# cd ..
# mv $APP_NAME $backup
# cp $rjxf ./
# ./bin/run.sh start
#}
upgrade(){
cd /opt/
cp ./${APP_NAME} ./bak/${APP_NAME}-${tag}
./run.sh ${APP_NAME} ${tag} stop
mv ${APP_NAME} test/${APP_NAME}
./run.sh ${APP_NAME} ${tag} start;exit
}
rollback(){
cd /opt/
./run.sh ${APP_NAME} ${tag} stop
cp ./bak/${APP_NAME}-${tag} test/${APP_NAME}
./run.sh ${APP_NAME} ${tag} start
echo "slepp 5s ..."
sleep 5
}
#根據(jù)輸入?yún)?shù),選擇執(zhí)行對(duì)應(yīng)方法,不輸入則執(zhí)行使用說(shuō)明
case "$3" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
"upgrade")
upgrade
;;
"rollback")
rollback
;;
*)
usage
;;
esac
2.1、python代碼如下
from flask import Flask
import time,os
app = Flask(__name__)
@app.route("/")
def status():
msg = time.strftime("%Y-%m-%d %H:%M:%S")+"\tMyPid: "+str(os.getpid())+"\tv2.1.0"+"\n"
return msg
@app.route("/pid")
def pid():
msg = str(os.getpid()) + "\n"
return msg
if __name__ == '__main__':
app.run(port=10080,host="0.0.0.0")
2.3、pipeline流水線,Jenkinsfile配置如下
pipeline {
agent any
stages {
stage('代碼部署') {
when {
expression { params.ACTION == 'upgrade'}
}
steps {
script {
echo "從git上拉取代碼"
checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'c2b40745-be98-4627-93af-5cc975b0e355', url: 'https://gitee.com/****/test.git']])
echo '新代碼+tag標(biāo)簽 推送到目標(biāo)服務(wù)器備份目錄下'
sshPublisher(publishers: [sshPublisherDesc(configName: 'aly-arm', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'test_flask.py,run.sh')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
sshPublisher(publishers: [sshPublisherDesc(configName: 'aly-arm', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: """cd /opt;chmod +x run.sh;sh run.sh test_flask.py ${tag} ${ACTION};exit""", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '',usePty: true)], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
stage('代碼回退') {
when {
expression { params.ACTION == 'rollback'}
}
steps {
script {
sshPublisher(publishers: [sshPublisherDesc(configName: 'aly-arm', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "cd /opt;sh run.sh test_flask.py ${tag} ${ACTION};exit", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '',usePty: true)], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}
}
3、Jenkins配置
3.1、添加2個(gè)構(gòu)建化參數(shù)
1、選項(xiàng)參數(shù),用于選擇upgrade升級(jí)或者rollback回退
2、git參數(shù),用于從gitee上拉取對(duì)于標(biāo)簽的值
3.2、添加流水線配置
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-648116.html
3.3、執(zhí)行
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-648116.html
到了這里,關(guān)于Jenkins-CICD-python/Java包升級(jí)與回退的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!