C语言学习网

Jenkins自动化脚本应用

发表于:2022-08-12 作者:安全数据网编辑
编辑最后更新 2022年08月12日,vi project.sh#!/bin/bash#jenkins参数选项time=date +%Y-%m-%d_%H-%M-%Smethod=$1group=$2branch=$3#后端tomcat服

vi project.sh

#!/bin/bash
#jenkins参数选项
time=date +%Y-%m-%d_%H-%M-%S
method=$1
group=$2
branch=$3

#后端tomcat服务ip地址组
ip_value(){
if [[ $group == "group1" ]];then
ip_list="192.168.1.20"
echo ${ip_list}
elif [[ $group == "group2" ]];then
ip_list="192.168.1.21"
echo ${ip_list}
ssh root@192.168.1.10 "echo "enable server web_port/192.168.1.20" | socat stdio /usr/local/src/haproxy/haproxy.sock"
ssh root@192.168.1.11 "echo "enable server web_port/192.168.1.20" | socat stdio /usr/local/src/haproxy/haproxy.sock"
elif [[ $group == "group3" ]];then
ip_list="192.168.1.20 192.168.1.21"
echo ${ip_list}
fi
}

#代码先部署至Jenkins服务端
code_deploy(){
cd /data/jenkins/worker
rm -rf ./*
git clone -b $branch git@192.168.1.30:jie/web-page.git
}
#代码测试
code_test(){
cd /data/jenkins/worker/web-page
cat > sonar-project.properties <sonar.projectKey=one123456
sonar.projectName=code-test
sonar.projectVersion=1.0
sonar.sources=./
sonar.language=php
sonar.sourceEncoding=UTF-8
eof
/data/scanner/sonar-scanner/bin/sonar-scanner
}

#代码压缩
code_compress(){
cd /data/jenkins/worker/
rm -f web-page/sonar-project.properties
tar czvf code-tar.gz web-page
}

#调度器剥离后端服务
haproxy_down(){
for ip in ${ip_list};do
echo $ip
ssh root@192.168.1.10 "echo "disable server web_port/${ip}"|socat stdio /usr/local/src/haproxy/haproxy.sock"
ssh root@192.168.1.11 "echo "disable server web_port/${ip}"|socat stdio /usr/local/src/haproxy/haproxy.sock"
done
}
#后端服务下线
backend_stop(){
for ip in ${ip_list};do
echo $ip
ssh root@$ip "source /etc/profile && /usr/local/src/tomcat/tomcat/bin/catalina.sh stop"
done
}

#部署代码到后端服务站点
scp_backend(){
for ip in ${ip_list};do
echo $ip
scp /data/jenkins/worker/code-tar.gz root@${ip}:/usr/local/src/tomcat/tomcat/web-code/${time}-code-tar.gz
ssh root@${ip} "tar xvf /usr/local/src/tomcat/tomcat/web-code/${time}-code-tar.gz -C /usr/local/src/tomcat/tomcat/webapps"
done
}

#启动后端服务
backend_start(){
for ip in ${ip_list};do
echo $ip
ssh root@$ip "source /etc/profile && /usr/local/src/tomcat/tomcat/bin/catalina.sh start"
sleep 6
done
}

#测试访问后端服务
backend_test(){
for ip in ${ip_list};do
echo $ip
status_code=curl -I -s -m 6 -o /dev/null -w %{http_code} http://${ip}:8080
if [ ${status_code} -eq 200 ];then
echo "访问测试成功,后端代码部署成功"
if [[ $ip == "192.168.1.21" ]];then
ssh root@192.168.1.10 "echo "enable server web_port/${ip}" | socat stdio /usr/local/src/haproxy/haproxy.sock"
ssh root@192.168.1.11 "echo "enable server web_port/${ip}" | socat stdio /usr/local/src/haproxy/haproxy.sock"
fi
else
echo "访问测试失败,请重新部署代码至后端服务"
fi
done
}

#代码回滚
code_rollback(){
for ip in ${ip_list};do
echo $ip
last_version=ssh root@${ip} "ls -l -t /usr/local/src/tomcat/tomcat/web-code/" | awk 'NR==3{print $NF}'
ssh root@${ip} " tar xvf /usr/local/src/tomcat/tomcat/web-code/$last_version -C /usr/local/src/tomcat/tomcat/webapps"
done
echo "tomcat代码回滚成功,回到上一版本,下一步进行访问测试"
}

#主菜单命令
main(){
case $1 in
deploy)
ip_value;
code_deploy;
code_test;
code_compress;
haproxy_down;
backend_stop;
scp_backend;
backend_start;
backend_test;
;;
rollback)
ip_value;
haproxy_down;
backend_stop;
code_rollback;
backend_start;
backend_test;
;;
esac
}
main $1 $2 $3

#
#
vi deploy.sh

#!/bin/bash
#Jenkins工程构建通用TOMCAT部署脚本
#@author abcdocker
#@create_time 2017-08-19
#
#在Jenkins内配置部署单元参数
#参数格式:MAVEN_MODULE_NAME:TOMCAT_ABSOLUTE_PATH MAVEN模块名称:需要部署的目标TOMCAT绝对路径
#只有单个部署单元且没有Maven子模块时,模块名称参数可以没有,参数格式为:TOMCAT_ABSOLUTE_PATH
#
#注意:
#在本部署脚本内会执行TOMCAT启动脚本,为避免Jenkins在构建成功以后杀掉所有衍生的后台进程,需要在Jenkins内配置全局环境变量 BUILD_ID 值为 allow_to_run_as_daemon
#
#

DEPLOY_TARGET_TOMCAT=$TOMCAT

#校验部署参数,不能为空
if [ -z "$DEPLOY_TARGET_TOMCAT" ]
then
echo
echo 部署参数为空,部署失败!
echo "#####################################################################"
echo
echo 单个部署单元参数格式:
echo MAVEN_MODULE_NAME:TOMCAT_ABSOLUTE_PATH MAVEN模块名称:需要部署的目标TOMCAT绝对路径
echo
echo 多个部署单元参数格式:(多个部署单元使用空格分割)
echo MAVEN_MODULE_NAME:TOMCAT_ABSOLUTE_PATH MAVEN_MODULE_NAME:TOMCAT_ABSOLUTE_PATH
echo
echo "#####################################################################"
exit 1
fi

echo
echo 部署参数:${DEPLOY_TARGET_TOMCAT}

TOMCAT_ARR=${DEPLOY_TARGET_TOMCAT//;/ }
ARR=($TOMCAT_ARR)
ARR_LEN=${#ARR[*]}
echo 共 ${ARR_LEN} 个部署单元

i=1
#获取Jenkins传入的目标TOMCAT组
for T in $TOMCAT_ARR
do
echo
echo 开始 处理第 ${i} 个部署单元
echo 第一个部署单元:$T

#获取目标TOMCAT的WAR路径和TOMCATA的绝对路径TOMCAT_PARAM=(${T//:/ })MODULE_NAME=${TOMCAT_PARAM[0]}TARGET_TOMCAT_PATH=${TOMCAT_PARAM[1]}WAR_PATH="$WORKSPACE/$MODULE_NAME/target/*.war"echo 部署单元模块名称:"${MODULE_NAME}"echo 部署WAR包路径:"${WAR_PATH}"echo 部署TOMCAT路径:"${TARGET_TOMCAT_PATH}"#需要考虑MAVEN单模块下的部署问题#if [ "${#ARR[*]}" -eq 1 -a -z "$TARGET_TOMCAT_PATH" ]if [ "$ARR_LEN" -eq 1 -a -z "$TARGET_TOMCAT_PATH" ]then    #MAVEN过程没有子模块,单个部署单元    TARGET_TOMCAT_PATH=$MODULE_NAMEMODULE_NAME="NULL"fi#校验参数,WORKSPACE变量来自于Jenkins环境变量if [ -z "$MODULE_NAME" -o ! -f $WAR_PATH ]then    echo 错误:MAVEN部署模块名称 参数为空 或 找不到WAR包!    echo 部署失败!    exit 1fiif [ -z "$TARGET_TOMCAT_PATH" -o ! -d "$TARGET_TOMCAT_PATH" ]then    echo 错误:目标TOMCAT绝对路径 参数为空 或 该TOMCAT目录不存在!    echo 部署失败!    exit 1fiecho 开始清理目标TOMCAT启动进程...TOMCAT_PID=`ps -ef |grep "$TARGET_TOMCAT_PATH" |grep  "start" |awk '{print $2}'`if [ -n "$TOMCAT_PID" ]then    echo TOMCAT_${i},PID${TOMCAT_PID},正在结束该进程...    kill -9 $TOMCAT_PID && echo PID${TOMCAT_PID} 已被干掉!else    echo TOMCAT_${i} 进程未启动!fiecho 开始清理目标TOMCAT缓存...rm -rf $TARGET_TOMCAT_PATH/webapps/*rm -rf $TARGET_TOMCAT_PATH/logs/*rm -rf $TARGET_TOMCAT_PATH/work/*echo 开始部署WAR包...cp -a $WAR_PATH $TARGET_TOMCAT_PATH/webapps/ROOT.war && echo WAR包部署完毕。echo 开始启动目标TOMCAT服务...sleep 10/bin/bash $TARGET_TOMCAT_PATH/bin/startup.shecho 开始配置web目录的FTP权限...#启动过程会自动解压WAR包,所以在这里需要等待WAR包解压完成再调整目录权限sleep 30chown -R vftpuser.vftpuser ${TARGET_TOMCAT_PATH}/webapps/ && echo 目录权限配置完毕。echo 部署成功echo 完成 第 ${i} 个部署单元处理。echo((i++))

done

0