根据jenkins任务名成进行编译,打包,部署项目到指定的环境
- 以下是根据项目进行编写的归档部署脚本,后续添加其他项目时可以灵活扩展;
- 美中不足的是,ansilbe执行语句调用变量还没写出来.用脚本了,就不想再写个playbook(这个暂时写不出来);
- 那么就麻烦一点,ansible部署的部分,暂时很不灵活.只能固定格式了.
[root@localhost ~]# cat auto_archive_project_and_deploy.sh
#!/bin/bash
# by author HsLM_Z
# Tue Mar 10 09:43:33 CST 2020
# 根据jenkins任务名称,判断分支选择归档方式并部署到指定分支
# jenkins任务名称规则:liwanjia+项目名+分支名
######################################################################
# Step1: [参数1,为项目名称(项目名成包含项目类型,项目发布环境)]
# Step2: [菜单]
# Step3: [创建临时目录],[打包备份],[删除冗余文件]
# Step4: [对beta项目归档],[对manage项目归档],[对store-app项目归档]
# Step5: [对beta分支进行推送],[对manage分支进行推送],[对store-app分支进行推送]
######################################################################
######################################################################
######################################################################
# Step3
# 函数:创建临时目录;用途:存放临时编译后的代码文件以及备份文件
function ArchiveBeforeMkdir(){
echo "开始创建临时目录"
[ ! -d $Jenkins_Dest ] && mkdir -p $Jenkins_Dest
[ ! -d ${Jenkins_Dest}/backup ] && mkdir -p ${Jenkins_Dest}/backup
[ ! -d ${Jenkins_Dest}/${ProjectName}/${Time_Now} ] && mkdir -p ${Jenkins_Dest}/${ProjectName}/${Time_Now}
}
# 函数:打包此次项目文件
# cd 切换到临时目录
# 将目录名改为 项目文件所在目录
# tar 打包项目文件所在目录 打包名格式为 当前时间_任务名_tar.gz (此为备份文件)
# tar 打包项目文件所在目录 打包名格式为 项目文件所在目录.tar.gz (此为用ansible批量部署使用名称,固定名称,方便管理)
# mv 将备份文件 copy至 备份目录
function TarProject(){
echo "开始打包并备份本次部署项目代码"
cd ${Jenkins_Dest}/${ProjectName}
mv ${Time_Now} $BeforeName
tar -czPf ${ProjectName}_${Time_Now}_tar.gz $BeforeName
tar -czPf ${BeforeName}.tar.gz $BeforeName
mv ${Jenkins_Dest}/${ProjectName}/${ProjectName}_${Time_Now}_tar.gz ${Jenkins_Dest}/backup
}
# 函数:删除临时打包文件
# 删除项目文件所在目录
# 删除项目文件所在目录.tar.gz
function Del_TarProject(){
rm -rf ${Jenkins_Dest}/${ProjectName}/${BeforeName}
rm -rf ${Jenkins_Dest}/${ProjectName}/${BeforeName}.tar.gz
}
######################################################################
# Step4
# 函数:归档java项目
# 定义变量,此变量定义java项目名称
# 调用函数:创建临时目录
# 创建/lib目录,用于存放java项目运行所需的jar包
# 使用for循环
# 调用java项目名称变量,依次将jar包和项目运行依赖的jar包拷贝至指定目录
# 如果找不到java项目名,输出提示:找不到项目名称
function Project_Beta_Archive(){
echo "开始归档beta项目代码"
Jar_Name="lwjwlkj-service-product lwjwlkj-service-order lwjwlkj-service-user lwjwlkj-service-payment lwjwlkj-web"
[ ! -d ${Jenkins_Dest}/${ProjectName}/${Time_Now}/lib ] && mkdir -p ${Jenkins_Dest}/${ProjectName}/${Time_Now}/lib
for i in $Jar_Name;
do
Jar_Wrap=${i}-1.0-SNAPSHOT.jar
if [ -f ${Jenkins_Source}/$i/target/${Jar_Wrap} -a -d ${Jenkins_Source}/$i/target/lib ];then
cp ${Jenkins_Source}/$i/target/${Jar_Wrap} ${Jenkins_Dest}/${ProjectName}/${Time_Now}
cp ${Jenkins_Source}/$i/target/lib/* ${Jenkins_Dest}/${ProjectName}/${Time_Now}/lib
else
echo -e "-33[31mFile $Jar_Wrap cannot be found, please check. 33[0m";exit
fi
done
}
# 函数:归档manage项目
# 进入项目任务目录
# 用npm命令编译
# 判断编译成功后,将项目移动至临时目录
## yum install npm -y
## npm install
## npm run build
function Project_Manage_Archive(){
echo "开始归档manage项目"
cd ${Jenkins_Source}
npm run build
if [ $? -eq 0 ];then
cp -r ${Jenkins_Source}/dist/* ${Jenkins_Dest}/${ProjectName}/${Time_Now}/
else
echo -e "-33[31merror: npm run build exec error. 33[0m";exit
fi
# /bin/chown -R www.root /usr/local/nginx/html/
}
# 函数:归档store_app项目;[liwanjia/store-app]
function Project_StoreApp_Archive(){
echo "开始归档store-app项目"
cp -r ${Jenkins_Source}/* ${Jenkins_Dest}/${ProjectName}/${Time_Now}/
}
######################################################################
# Step5
#### 根据项目分支选择部署操作 ####
## [root@localhost ~]# cat /etc/ansible/hosts
## [preprod]
##
## [master]
##
## [frontpreprod]
##
## [frontmaster]
##
# beta项目
function Select_Beta_Master_Branch(){
ansible master -m copy -a 'src=/data/jenknins_arichive/liwanjia-beta-master/lwjwlkj_jar.tar.gz dest=/data/'
ansible master -m shell -a 'ls -lh /data/lwjwlkj_jar.tar.gz'
ansible master -m shell -a 'tar xf /data/lwjwlkj_jar.tar.gz -C /data/'
ansible master -m shell -a 'ls -lh /data/lwjwlkj_jar'
ansible master -m script -a '/data/sh/liwanjia/auto_kill_and_start_java_project.sh'
}
function Select_Beta_Preprod_Branch(){
ansible preprod -m copy -a 'src=/data/jenknins_arichive/liwanjia-beta-preprod/lwjwlkj_jar.tar.gz dest=/data/'
ansible preprod -m shell -a 'ls -lh /data/lwjwlkj_jar.tar.gz'
ansible preprod -m shell -a 'tar xf /data/lwjwlkj_jar.tar.gz -C /data/'
ansible preprod -m shell -a 'ls -lh /data/lwjwlkj_jar'
ansible preprod -m script -a '/data/sh/liwanjia/auto_kill_and_start_java_project.sh'
}
# manage项目
function Select_Manage_Master_Branch(){
ansible frontmaster -m copy -a 'src=/data/jenknins_arichive/liwanjia-manage-master/admin.lwjwlkj.com.tar.gz dest=/data/'
ansible frontmaster -m shell -a 'ls -lh /data/admin.lwjwlkj.com.tar.gz'
ansible frontmaster -m shell -a 'tar xf /data/admin.lwjwlkj.com.tar.gz -C /usr/local/nginx/html/'
ansible frontmaster -m shell -a '/bin/chmod -R 755 /usr/local/nginx/html/'
ansible frontmaster -m shell -a 'ls -lh /usr/local/nginx/html/admin.lwjwlkj.com'
}
function Select_Manage_Dev_Branch(){
ansible frontpreprod -m copy -a 'src=/data/jenknins_arichive/liwanjia-manage-dev/preadmin.lwjwlkj.com.tar.gz dest=/data/'
ansible frontpreprod -m shell -a 'ls -lh /data/preadmin.lwjwlkj.com.tar.gz'
ansible frontpreprod -m shell -a 'tar xf /data/preadmin.lwjwlkj.com.tar.gz -C /usr/local/nginx/html/'
ansible frontpreprod -m shell -a '/bin/chmod -R 755 /usr/local/nginx/html/'
ansible frontpreprod -m shell -a 'ls -lh /usr/local/nginx/html/preadmin.lwjwlkj.com'
}
# store-app项目
function Select_StoreApp_Master_Branch(){
ansible frontmaster -m copy -a 'src=/data/jenknins_arichive/liwanjia-store-app-master/onlinestore.lwjwlkj.com.tar.gz dest=/data/'
ansible frontmaster -m shell -a 'ls -lh /data/onlinestore.lwjwlkj.com.tar.gz'
ansible frontmaster -m shell -a 'tar xf /data/onlinestore.lwjwlkj.com.tar.gz -C /usr/local/nginx/html/'
ansible frontmaster -m shell -a '/bin/chmod -R 755 /usr/local/nginx/html/'
ansible frontmaster -m shell -a 'ls -lh /usr/local/nginx/html/onlinestore.lwjwlkj.com'
ansible frontmaster -m shell -a 'ls -lh /usr/local/nginx/html'
}
function Select_StoreApp_Dev_Branch(){
ansible frontpreprod -m copy -a 'src=/data/jenknins_arichive/liwanjia-store-app-dev/preonlinestore.lwjwlkj.com.tar.gz dest=/data/'
ansible frontpreprod -m shell -a 'ls -lh /data/preonlinestore.lwjwlkj.com.tar.gz'
ansible frontpreprod -m shell -a 'tar xf /data/preonlinestore.lwjwlkj.com.tar.gz -C /usr/local/nginx/html/'
ansible frontpreprod -m shell -a '/bin/chmod -R 755 /usr/local/nginx/html/'
ansible frontpreprod -m shell -a 'ls -lh /usr/local/nginx/html/preonlinestore.lwjwlkj.com'
ansible frontpreprod -m shell -a 'ls -lh /usr/local/nginx/html'
}
######################################################################
# Step2
#### 根据项目分支选择部署操作 ####
# 函数:菜单
# 输出:项目开始执行
# 判断:jenkins任务目录是否存在
# 不存在:输出找不到项目
# 匹配任务名称
# 定义变量路径
# 定义变量目录
# 调用函数:创建临时目录
# 调用函数:根据项目进行归档
# 调用函数:进行打包备份
# 调用函数:选择分支进行推送
# 调用函数:删除冗余文件
function menu(){
echo -e " 33[32m##### $0 Begin $Time_Now 33[0m"
if [ ! -d $Jenkins_Source ];then
echo -e " 33[31mNot found Project $ProjectName 33[0m"
exit
# [liwanjia/beta] java项目
elif [ $ProjectName == "liwanjia-beta-master" ];then
Project_Path="/data"
BeforeName="lwjwlkj_jar"
ArchiveBeforeMkdir
Project_Beta_Archive
TarProject
Select_Beta_Master_Branch
Del_TarProject
elif [ $ProjectName == "liwanjia-beta-preprod" ];then
Project_Path="/data"
BeforeName="lwjwlkj_jar"
ArchiveBeforeMkdir
Project_Beta_Archive
TarProject
Select_Beta_Preprod_Branch
Del_TarProject
# [liwanjia/manage] 后台管理系统项目
elif [ $ProjectName == "liwanjia-manage-master" ];then
Project_Path="/usr/local/nginx/html"
BeforeName="admin.lwjwlkj.com"
ArchiveBeforeMkdir
Project_Manage_Archive
TarProject
Select_Manage_Master_Branch
Del_TarProject
elif [ $ProjectName == "liwanjia-manage-dev" ];then
Project_Path="/usr/local/nginx/html"
BeforeName="preadmin.lwjwlkj.com"
ArchiveBeforeMkdir
Project_Manage_Archive
TarProject
Select_Manage_Dev_Branch
Del_TarProject
# [liwanjia/store-app] 商城系统
elif [ $ProjectName == "liwanjia-store-app-master" ];then
Project_Path="/usr/local/nginx/html"
BeforeName="onlinestore.lwjwlkj.com"
ArchiveBeforeMkdir
Project_StoreApp_Archive
TarProject
Select_StoreApp_Master_Branch
Del_TarProject
elif [ $ProjectName == "liwanjia-store-app-dev" ];then
Project_Path="/usr/local/nginx/html"
BeforeName="preonlinestore.lwjwlkj.com"
ArchiveBeforeMkdir
Project_StoreApp_Archive
TarProject
Select_StoreApp_Dev_Branch
Del_TarProject
fi
}
######################################################################
# Step1
ProjectName=$1
# jenkins项目家目录
Jenkins_Source="/root/.jenkins/workspace/${ProjectName}"
# jenkins临时存放文件目录
Jenkins_Dest="/data/jenknins_arichive"
# 当前时间
Time_Now=`date +%Y%m%d-%H%M`
menu $*
[root@localhost ~]# cat /data/sh/liwanjia/auto_kill_and_start_java_project.sh
#!/bin/bash
source /etc/profile
echo "ps -ef|grep lwjwlkj|grep -v grep"
ps -ef|grep lwjwlkj|grep -v grep
echo "kill -9 `ps -ef|grep lwjwlkj|grep -v grep|awk '{print $2}'`"
kill -9 `ps -ef|grep lwjwlkj|grep -v grep|awk '{print $2}'`
sleep 3
cd /data/lwjwlkj_jar
echo "start java project"
nohup java -jar lwjwlkj-service-user-1.0-SNAPSHOT.jar >/tmp/log/lwjwlkj_jar/lwjwlkj-service-user.log 2>&1 &
sleep 1
nohup java -jar lwjwlkj-service-product-1.0-SNAPSHOT.jar > /tmp/log/lwjwlkj_jar/lwjwlkj-service-product.log 2>&1 &
sleep 1
nohup java -jar lwjwlkj-service-order-1.0-SNAPSHOT.jar > /tmp/log/lwjwlkj_jar/lwjwlkj-service-order.log 2>&1 &
sleep 1
nohup java -jar lwjwlkj-service-payment-1.0-SNAPSHOT.jar >/tmp/log/lwjwlkj_jar/lwjwlkj-service-payment.log 2>&1 &
sleep 1
nohup java -jar lwjwlkj-web-1.0-SNAPSHOT.jar > /tmp/log/lwjwlkj_jar/lwjwlkj-web.log 2>&1 &
sleep 1
echo "ps -ef|grep lwjwlkj|grep -v grep"
ps -ef|grep lwjwlkj|grep -v grep