1 Jenkinsfile
-
可以对 jenkins pipline 中的指令进行备份
-
避免 pipline 中配置被误操作
-
可以对 jenkinsfile 进行版本控制
[root@jenkins-master ~]# cd /opt/
[root@jenkins-master /opt]# git clone http://172.18.8.9/baichuan/app1.git
Cloning into 'app1'...
Username for 'http://172.18.8.9': song
Password for 'http://song@172.18.8.9':
remote: Enumerating objects: 60, done.
remote: Counting objects: 100% (21/21), done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 60 (delta 6), reused 17 (delta 5), pack-reused 39
Unpacking objects: 100% (60/60), done.
[root@jenkins-master /opt]# cd app1/
[root@jenkins-master /opt/app1]# mkdir jenkins_dir
[root@jenkins-master /opt/app1]# cd jenkins_dir/
[root@jenkins-master /opt/app1/jenkins_dir]# vim Jenkinsfile
[root@jenkins-master /opt/app1/jenkins_dir]# cd ../
[root@jenkins-master /opt/app1]# git add .
[root@jenkins-master /opt/app1]# git commit -m "add jenkinsfile"
[main 3bad88a] add jenkinsfile
1 file changed, 60 insertions(+)
create mode 100644 jenkins_dir/Jenkinsfile
[root@jenkins-master /opt/app1]# git push
Username for 'http://172.18.8.9': song
Password for 'http://song@172.18.8.9':
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 1.03 KiB | 1.03 MiB/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To http://172.18.8.9/baichuan/app1.git
a419d16..3bad88a main -> main
[root@jenkins-master /opt/app1]#
2 视图
3 参数话构建
[root@jenkins-master ~]# cat /data/script/baichuan-deploy.sh
#!/bin/bash
#
DATE=$(date +%Y-%m-%d_%H-%M-%S)
METHOD=$1
BRANCH=$2
GROUP_LIST=$3
STDIO="socat stdio /run/haproxy/admin.sock"
HAIPADDR="172.18.8.79 172.18.8.89"
function IP_list(){
if [[ ${GROUP_LIST} == "GROUP1" ]];then
Server_IP="172.18.8.49"
echo ${Server_IP}
elif [[ ${GROUP_LIST} == "GROUP2" ]];then
Server_IP="172.18.8.59 172.18.8.69"
echo ${Server_IP}
for ip in $HAIPADDR
do
ssh root@$ip "which socat >/dev/null;if [ $? -ne 0 ];then apt -y install socat;fi"
ssh root@$ip ""echo enable server webserver-80/172.18.8.49" | ${STDIO}"
done
elif [[ ${GROUP_LIST} == "GROUP3" ]];then
Server_IP="172.18.8.49 172.18.8.59 172.18.8.69"
echo ${Server_IP}
fi
}
function clone_code(){
echo "即将开始从clone ${BRANCH}分支的代码"
cd /data/gitdata/baichuan && rm -rf app1 && git clone -b ${BRANCH} git@172.18.8.9:baichuan/app1.git
echo "${BRANCH}分支代码clone完成,即开始编译"
}
function scanner_code(){
cd /data/gitdata/baichuan/app1 && /usr/local/sonar-scanner/bin/sonar-scanner
echo "代码扫描完成,请打开sonarqube查看扫描结果"
}
function code_maven(){
echo "cd /data/gitdata/baichuan/app1 && mvn clean package -Dmaven.test.skip=true"
echo "代码编译完成"
}
function make_zip(){
which zip > /dev/null
if [ $? -ne 0 ];then
apt -y install zip unzip
fi
cd /data/gitdata/baichuan/app1 && zip -r app1.war ./index.html
echo "代码打包完成"
}
down_node(){
for node in ${Server_IP};do
for ip in $HAIPADDR
do
ssh root@$ip "echo "disable server webserver-80/${node}" | ${STDIO}"
if [ $? -eq 0 ];then
echo "${node} 从负载均衡 $ip 下线成功"
fi
done
done
}
function scp_zipfile(){
tomcat_webapps="/data/tomcat/tomcat_webapps/myapp"
tomcat_webdir="/data/tomcat/tomcat_webdir"
tomcat_appdir="/data/tomcat/tomcat_appdir"
for node in ${Server_IP};do
scp /data/gitdata/baichuan/app1/app1.war song@${node}:${tomcat_appdir}/app1-${DATE}.war
ssh song@${node} "which unzip >/dev/null;if [ $? -ne 0 ];then apt -y install unzip;fi"
ssh song@${node} "unzip ${tomcat_appdir}/app1-${DATE}.war -d ${tomcat_webdir}/app1-${DATE} && rm -rf ${tomcat_webapps} && ln -sv ${tomcat_webdir}/app1-${DATE} ${tomcat_webapps}"
done
}
function stop_tomcat(){
for node in ${Server_IP};do
ssh song@${node} "sudo systemctl stop tomcat"
if [ $? -ne 0 ];then
exit 1
fi
done
}
function start_tomcat(){
for node in ${Server_IP};do
ssh song@${node} "sudo systemctl start tomcat"
sleep 3
done
}
function web_test(){
sleep 2
for node in ${Server_IP};do
NUM=`curl -s -I -m 10 -o /dev/null -w %{http_code} http://${node}:8080/myapp/index.html`
if [[ ${NUM} -eq 200 ]];then
echo "${node} myapp URL 测试通过,即将添加到负载"
add_node ${node}
else
echo "${node} 测试失败,请检查该服务器是否成功启动tomcat"
fi
done
}
function add_node(){
node=$1
echo ${node},"----->"
#if [[ ${GROUP_LIST} == "GROUP3" ]];then
# ssh root@172.18.8.79 ""echo enable server web_port/172.18.8.49" | socat stdio /run/haproxy/admin.sock"
# ssh root@172.18.8.89 ""echo enable server web_port/172.18.8.49" | socat stdio /run/haproxy/admin.sock"
#fi
##########################################
#if [ ${node} == "172.18.8.49" ];then
# echo "灰度部署环境服务器-->172.18.8.49部署完毕,请进行代码测试!"
#else
for ip in $HAIPADDR
do
ssh root@$ip ""echo enable server web_port/${node}" | ${STDIO}"
done
#fi
}
function rollback_last_version(){
tomcat_webapps="/data/tomcat/tomcat_webapps"
tomcat_webdir="/data/tomcat/tomcat_webdir"
for node in ${Server_IP};do
echo $node
NOW_VERSION=$(ssh song@${node} ""/bin/ls -ltr ${tomcat_webapps} | awk -F"->" '{print $2}' | tail -n1"")
NOW_VERSION=`basename ${NOW_VERSION}`
echo $NOW_VERSION,"NOW_VERSION"
NAME=$(ssh song@${node} ""ls -ltr ${tomcat_webdir} | grep -B 1 ${NOW_VERSION} | head -n1 | awk '{print $9}'"")
echo $NAME,""NAME
ssh song@${node} "rm -rf ${tomcat_webapps}/myapp && ln -sv ${tomcat_webdir}/${NAME} ${tomcat_webapps}/myapp"
done
}
function delete_history_version(){
for node in ${Server_IP};do
ssh song@${node} "rm -rf /data/tomcat/tomcat_appdir/*"
NUM=$(ssh song@${node} ""/bin/ls -ldrt /data/tomcat/tomcat_webdir/app1-* | wc -l"")
echo "服务器${node} 当前历史版本统计值是:${NUM}"
if [ ${NUM} -gt 5 ];then
NAME=$(ssh song@${node} ""/bin/ls -ldrt /data/tomcat/tomcat_webdir/app1-* | head -n1 | awk '{print $9}'"")
ssh song@${node} "rm -rf ${NAME}"
echo "${node} 删除历史版本${NAME}成功!"
fi
done
}
main(){
case $1 in
deploy)
IP_list;
clone_code;
#scanner_code;
make_zip;
down_node;
stop_tomcat;
scp_zipfile;
start_tomcat;
web_test;
delete_history_version;
;;
rollback_last_version)
IP_list;
#echo ${Server_IP}
down_node;
stop_tomcat;
rollback_last_version;
start_tomcat;
web_test;
;;
esac
}
main $1 $2 $3
[root@jenkins-master ~]#
同步到 jenkins-slave1 和 jenkins-slave2
[root@jenkins-master /]# scp -r /data/script/ 172.18.8.29:/data/
root@172.18.8.29's password:
test.sh 100% 81 103.3KB/s 00:00
test1.sh 100% 209 349.5KB/s 00:00
baichuan-app1-deploy.sh 100% 624 914.9KB/s 00:00
baichuan-deploy.sh 100% 5149 5.6MB/s 00:00
[root@jenkins-master /]# scp -r /data/script/ 172.18.8.39:/data/
root@172.18.8.39's password:
test.sh 100% 81 142.8KB/s 00:00
test1.sh 100% 209 489.8KB/s 00:00
baichuan-app1-deploy.sh 100% 624 688.3KB/s 00:00
baichuan-deploy.sh 100% 5149 4.5MB/s 00:00
[root@jenkins-master /]#