CI/CD简单代码发布脚本
残血版CI/CD脚本
如有需要,可根据博主的模板做自定义修改,具体流程博主不做过多描述了,此脚本支持双分支发布、测试、编译构建、web上下线、版本回滚等功能
说明:使用此脚本时需要在jenkins中传递4个变量参数
- METHOD 对应发布还是回滚
- BRANCH 代码分支是什么
- GROUP 运行代码的主机组
- OPS 用于选择回滚N个版本,仅在回滚代码时生效,发布时可不管
#!/bin/bash
DATE=`date +%m%d%H%M`
METHOD=$1
BRANCH=$2
GROUP=$3
BACK_VS=$4
function IP_list(){
if [[ ${GROUP} == "group1" ]];then
Server_IP="2.2.2.22"
proxy_ser="tomcat-develop"
echo ${Server_IP}
elif [[ ${GROUP} == "group2" ]];then
Server_IP="2.2.2.32 "
proxy_ser="tomcat-master"
echo ${Server_IP}
ssh root@2.2.2.12 "echo set weight tomcat-develop/2.2.2.22 1 |socat stdio /var/lib/haproxy/stats"
ssh root@2.2.2.25 "echo set weight tomcat-develop/2.2.2.22 1 |socat stdio /var/lib/haproxy/stats"
elif [[ ${GROUP} == "group3" ]];then
Server_IP="2.2.2.22 2.2.2.32"
proxy_ser="tomcat-develop tomcat-master"
echo ${Server_IP}
fi
}
function clone_code(){
cd ~/git/ && rm -rf ./*
git clone -b $BRANCH git@2.2.2.25:devgp/web1.git
echo "代码clone完成"
}
function scanner_code(){
cd ~/git/web1 && sonar-scanner
echo "代码扫描完成,请打开sonarqube查看扫描结果"
}
function code_maven(){
echo "cd ~/git/web1 && mvn clean package -Dmaven.test.skip=true"
echo "代码编译完成"
}
function make_tgz(){
cd ~/git/web1 && tar czf web1-${DATE}.tgz *
echo "代码打包完成"
}
down_node(){
for i in $proxy_ser ;do
for node in ${Server_IP};do
ssh root@2.2.2.25 "echo set weight ${i}/${node} 0 | socat stdio /var/lib/haproxy/stats"
echo "${node} 从负载均衡2.2.2.25下线成功";
ssh root@2.2.2.12 "echo set weight ${i}/${node} 0 | socat stdio /var/lib/haproxy/stats"
echo "${node} 从负载均衡2.2.2.22下线成功";
done
done
}
function scp_tgzfile(){
cd ~/git/web1
for node in ${Server_IP};do
ssh root@${node} "rm -rf /data/apps_tgz/*";
scp web1-${DATE}.tgz root@$node:/data/apps_tgz;
ssh root@${node} "cd /data/apps_tgz/ && mkdir /data/apps_dir/web1-${DATE} && tar xf web1-${DATE}.tgz -C /data/apps_dir/web1-${DATE} && rm -rf /data/apps/web1 && ln -sv /data/apps_dir/web1-${DATE} /data/apps/web1"
done
}
function stop_tomcat(){
for node in ${Server_IP};do
{ ssh root@${node} "systemctl stop tomcat.service"; }&
done
}
function start_tomcat(){
for node in ${Server_IP};do
ssh root@${node} "systemctl start tomcat.service"
sleep 5;
done
}
function web_test(){
sleep 5
for node in ${Server_IP};do
NUM=`curl -I http://${node}:8080/web1/ 2>/dev/null|awk 'NR==1{print $2}'`
if [[ ${NUM} -eq 200 ]];then
echo "${node} 测试通过,即将添加到负载"
up_node ${node}
else
echo "${node} 测试失败,请检查该服务器是否成功启动tomcat"
fi
done
}
function up_node(){
node=$1
if [[ $GROUP = 'group1' ]] ;then
echo "2.2.2.22部署完毕,请进行代码测试!"
else
ssh root@2.2.2.25 "echo set weight tomcat-develop/2.2.2.22 1 | socat stdio /var/lib/haproxy/stats"
for i in $proxy_ser ;do
for j in $node ;do
{ ssh root@2.2.2.25 "echo set weight ${i}/${j} 1 | socat stdio /var/lib/haproxy/stats"
echo "${j} 从负载均衡2.2.2.25上线成功"; }&
{ ssh root@2.2.2.12 "echo set weight ${i}/${j} 1 | socat stdio /var/lib/haproxy/stats"
echo "${j} 从负载均衡2.2.2.12上线成功"; }&
done
done
fi
}
function rollback_last_version(){
for node in ${Server_IP};do
NOW_VS=$(basename `ssh root@${node} ls -l /data/apps|awk '/.*-[0-9]+/{print $11}'`)
R_BAK_VS=$(ssh root@${node} ls -t /data/apps_dir|grep -A $BACK_VS "${NOW_VS}"|awk -v vs=$BACK_VS 'NR==1+vs')
echo "$NOW_VS is now version"
#echo "select rollback version is $R_BAK_VS,rolled back $BRANCH"
echo "选择回滚的版本是:${R_BAK_VS},进行了: $BACK_VS次回滚"
{ ssh root@${node} "rm -rf /data/apps/web1 && ln -sv /data/apps_dir/${R_BAK_VS} /data/apps/web1"; }&
done
}
function delete_history_version(){
for node in ${Server_IP};do
RE_BAKS=$(ssh root@${node} "ls -t /data/apps_dir/|wc -l")
RE_OLD_FILE=$(ssh root@${node} ls -t /data/apps_dir/|awk 'NR>5')
if [[ $RE_BAKS -gt 5 ]] ;then
for i in $RE_OLD_FILE ;do
{ ssh root@${node} "cd /data/apps_dir/ && rm -rf ./${i}"; }&
done
fi
echo "${node} 删除历史版本 /data/apps_dir/${i}成功!"
done
}
main(){
case $1 in
deploy)
IP_list;
clone_code;
scanner_code;
make_tgz;
down_node;
stop_tomcat;
scp_tgzfile;
start_tomcat;
web_test;
delete_history_version;
;;
rollback)
IP_list;
down_node;
stop_tomcat;
rollback_last_version;
start_tomcat;
web_test;
;;
esac
}
main $1 $2 $3
wait