dubbo admin 需要账号密码登录进去看 每次发布都是一半一半发 经常发了一半忘记了,而且通过人工查看dubbo a。dmin 下的状态有点蠢,打算自动化掉。
健康检查 靠shell命令去做 然后通过result 判断是否发布成功
result=`(sleep 1;echo "status" ;sleep 1 ) | telnet $deploy_server $dubbo_port `
测试环境写的有点粗糙 勿喷。可以帮忙优化一下 pipeline如下
// Using git without checkout
pipeline {
agent any
parameters {
gitParameter branchFilter: 'origin/(.*)', defaultValue: 'master', name: 'Branch', sortMode: 'DESCENDING_SMART', type: 'PT_BRANCH'
}
stages {
stage('更新拉取最新代码') {
steps {
git branch: "${params.Branch}", url: '[email protected]:Java_Service/YJK-Java.git'
}
}
stage('输出项目的参数') {
steps {
sh "echo ${WORKSPACE} "
sh "echo ${Branch} "
//sh "echo ${deploy_step} "
sh "echo ${env.JOB_NAME} "
script {
wrap([$class: 'BuildUser']) {
def deploylog="${BUILD_USER} use pipeline '${JOB_NAME}(${BUILD_NUMBER})' "
println deploylog
buildName "#${BUILD_NUMBER}-^${BRANCH}^-${BUILD_USER}"
HTTPD_LOCATION= sh( returnStdout: true, script: 'git show -s |grep -vE "commit|Date" |grep -v "^$"')
//echo "HTTPD_LOCATION =${HTTPD_LOCATION}"
buildDescription "${HTTPD_LOCATION}"
}
}
script
{
switch(Model)
{
case "all":
sh "echo \u001B[31m部署全部模块\u001B[0m !!!"
default:
sh "echo others"
}
}
}
}
}
}
node {
timestamps {
env.PRO_TYPE = "test"
ansiColor('xterm') {
stage('Java编译') {
sh "echo ${WORKSPACE} "
sh "echo ${Branch} "
//sh "echo ${deploy_step} "
sh "echo ${env.JOB_NAME} "
script
{
switch(Model)
{
case "all":
sh "echo \u001B[31m部署全部模块\u001B[0m !!!"
sh "cd ${WORKSPACE} && rm dev-properties/ -rf && git clone [email protected]:dev-properties/dev-properties.git && cd ${WORKSPACE}/haozhuo/ && mvn clean install -T 1C -Dmaven.test.skip=true -Dmaven.compile.fork=true -Ptest -Dautoconfig.userProperties=${WORKSPACE}/dev-properties/test1.properties -Dautoconfig.charset=utf-8 -Dautoconfig.interactive=off"
default:
sh "echo 部署 \u001B[32m $Model \u001B[0m !!!"
sh "cd ${WORKSPACE} && rm dev-properties/ -rf && git clone [email protected]:dev-properties/dev-properties.git && cd ${WORKSPACE}/haozhuo/ && mvn clean install -pl $model -am -U -T 1C -Dmaven.test.skip=true -Dmaven.compile.fork=true -Ptest -Dautoconfig.userProperties=${WORKSPACE}/dev-properties/test1.properties -Dautoconfig.charset=utf-8 -Dautoconfig.interactive=off"
}
}
}
stage('构建镜像预发布') {
sh "echo ${Model}"
sh "echo ${WORKSPACE} "
sh "echo ${env.PRO_TYPE} "
//sh "echo ${deploy_step} "
sh "echo ${BUILD_NUMBER} "
sh "sh /srv/deploy_scripts/deploy.sh $Model $WORKSPACE ${env.PRO_TYPE} $BUILD_NUMBER"
sh "echo build successful"
}
}
}
}
发布结果如下
发布脚本如下
deploy.sh
#!/bin/sh
# create time: 2020-06-12
# author: xujiamin [email protected]
##set color##
echoRed() { echo $'\e[0;31m'"$1"$'\e[0m'; }
echoGreen() { echo $'\e[0;32m'"$1"$'\e[0m'; }
echoYellow() { echo $'\e[0;33m'"$1"$'\e[0m'; }
# 当前所有模块
yjk_all="haozhuo-account,haozhuo-admin,haozhuo-app-server,haozhuo-card,haozhuo-check,haozhuo-search,haozhuo-disc,haozhuo-ec,haozhuo-family,haozhuo-health,haozhuo-hm,haozhuo-info,haozhuo-job,haozhuo-openapi,haozhuo-point,haozhuo-reportContent,haozhuo-shoppingmall,haozhuo-system,haozhuo-wx,haozhuo-video,haozhuo-basic,haozhuo-health-manager"
# 需要构建的模块
arr=$1
# jenkins workspace
WORKSPACE=$2
# 发布的环境 dev test1 test2 gray prod
env=$3
# 当前jenkins构建的构建号
number=$4
# dubbo端口一定要放最后
#haozhuo_account_port="8080 20902"
if [[ "$arr" =~ yjk-all ]]; then
echo "本次部署model:all"
arr=${yjk_all}
else
echo "非全模块发布"
fi
IFS=','
for model in $arr; do
echo 构建镜像 $model
# mkdir -p /srv/dockerfile/$model/ROOT.war
source /srv/deploy_scripts/deploy_parameter.txt
expose_name=`echo ${model/-/_}`
deploy_port=`eval echo '$'"$expose_name"_port`
echo model:$model expose_name:$expose_name expose_port: $deploy_port
if [[ "$model" = "hanzhuo-app-server" ]] ;then
echo "项目 $model war包路径需要修改"
echo "争取后期换成url检测"
cp -r ${WORKSPACE}/haozhuo/$model/target/haozhuo-appServer.war /srv/dockerfile/$model/ROOT.war
else
cp -r ${WORKSPACE}/haozhuo/$model/target/$model.war /srv/dockerfile/$model/ROOT.war
fi
cd /srv/dockerfile/$model
pwd
echo 创建$model的dockerfile
echo FROM registry.cn-shanghai.aliyuncs.com/yjk-datag/tomcat:v1 > ./Dockerfile
echo 'MAINTAINER YJK Enterprise Container Images ' >> ./Dockerfile
echo 'ENV LANG C.UTF-8' >> ./Dockerfile
echo 'ENV PATH /usr/bin:$PATH' >> ./Dockerfile
echo "RUN mv /srv/tomcat/tomcat8/ /srv/tomcat/tomcat-$model" >> ./Dockerfile
echo "ADD ROOT.war /srv/tomcat/tomcat-$model/webapps/" >> ./Dockerfile
for expose_port in $deploy_port
do
echo "echo EXPOSE $expose_port >> ./Dockerfile"
echo EXPOSE $expose_port >> ./Dockerfile
echo $expose_port |awk -F[" "] '{print $NF}'
dubbo_port=`echo $expose_port |awk -F[" "] '{print $NF}'`
echo dubbo_port:$dubbo_port
done
# haozhuo_account_command="--privileged -p 8801:8080 -p 20902:20902 --cpus=1 -m 2048m -e DUBBO_IP_TO_REGISTRY=$(ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:" |egrep -v 172) -e DUBBO_PORT_TO_REGISTRY=20902 -h $model --name $model -v /data/tomcat_$model:/srv/tomcat/logs/local-yjklogs registry.cn-shanghai.aliyuncs.com/yjk-datag/$model:v$number "
echo 执行 docker build -t registry.cn-shanghai.aliyuncs.com/yjk-datag/$model:v$number ./
docker build -t registry.cn-shanghai.aliyuncs.com/yjk-datag/$model:v$number ./
echo 执行 docker push registry.cn-shanghai.aliyuncs.com/yjk-datag/$model:v$number
docker push registry.cn-shanghai.aliyuncs.com/yjk-datag/$model:v$number
deploy_com=`eval echo '$'"$expose_name"_command`
deploy_server=`eval echo '$'"$expose_name"_server`
echo docker stop $model > /tmp/start_$model.sh
echo docker rm $model >> /tmp/start_$model.sh
echo docker run -d -it --privileged $deploy_com -e DUBBO_IP_TO_REGISTRY=$deploy_server -h $model --name $model -v /data/tomcat_$model:/srv/tomcat/logs/local-yjklogs registry.cn-shanghai.aliyuncs.com/yjk-datag/$model:v$number >> /tmp/start_$model.sh
echo "docker run -d -it --privileged $deploy_com -e DUBBO_IP_TO_REGISTRY=$deploy_server -h $model --name $model -v /data/tomcat_$model:/srv/tomcat/logs/local-yjklogs registry.cn-shanghai.aliyuncs.com/yjk-datag/$model:v$number >> /tmp/start_$model.sh"
echo docker exec -i $model /bin/bash -c "/srv/tomcat/tomcat-$model/bin/startup.sh" >> /tmp/start_$model.sh
# 启动容器
# sh /tmp/start_$model.sh
scp -r /tmp/start_$model.sh $deploy_server:/tmp/start_$model.sh
ssh $deploy_server "/bin/sh /tmp/start_$model.sh"
# 健康检查
dubbo_port=`echo $expose_port |awk -F[" "] '{print $NF}'`
echo dubbo_port:$dubbo_port
######################
if [[ "$model" == "haozhuo-admin" ]] || [[ "$model" == "haozhuo-openapi" ]] || [[ "$model" == "haozhuo-app-server" ]];then
echoYellow "项目 $model 不适用dubbo_port健康检查 跳过检查"
echoYellow "争取后期换成url检测"
else
echo " result=`(sleep 1;echo 'status' ;sleep 1 ) | telnet 192.168.1.171 $dubbo_port `"
result=`(sleep 1;echo "status" ;sleep 1 ) | telnet $deploy_server $dubbo_port `
echo $result
# 设置健康检查初始值为2 因为第一次健康检查耗时2秒
second=2
while true
do
result=`(sleep 1;echo "status" ;sleep 1 ) | telnet $deploy_server $dubbo_port `
if [ ${second} -ge 150 ];then
echoRed "部署等待时间过长 判断为部署失败 请自行检查服务状态"
echoRed "部署等待时间过长 判断为部署失败 请自行检查服务状态"
echoRed "部署等待时间过长 判断为部署失败 请自行检查服务状态"
exit 1
#break
fi
if [[ "$result" =~ OK ]]; then
echo "[tags] result $result "
echo [tags] $model 项目启动成功 耗时 $second 秒
echoGreen "[tags] $model 项目启动成功 耗时 $second 秒"
break
fi
second=$((second + 5))
sleep 5
echo [tags] 目前健康检查已经耗费 $second 秒
done
fi
done
deploy_parameter.txt
# 放一些端口信息
# 参数说明
# xxx_port tomcat端口和dubbo端口 dubbo端口请写在最后面 用于健康检查
# xxx_command docker启动项目时的端口 资源限制 dubbo端口
# xxx_server 部署项目的服务器地址
# haozhuo_account 使用20901 需要修改test1.properties的端口配置
haozhuo_account_port="8080 20901"
haozhuo_account_command="-p 8801:8080 -p 20901:20901 --cpus=1 -m 2048m -e DUBBO_PORT_TO_REGISTRY=20901"
haozhuo_account_server=192.168.1.104
这里测试环境 全量发布是没有机子的 所以时间很短。
健康检查通过
参考
https://plugins.jenkins.io/build-name-setter/
https://blog.csdn.net/a910196454/article/details/106375630/