jenkins 自动构建脚本
博客专区 > 成智 的博客 > 博客详情
jenkins 自动构建脚本
成智 发表于9个月前
jenkins 自动构建脚本
  • 发表于 9个月前
  • 阅读 10
  • 收藏 0
  • 点赞 0
  • 评论 0

【腾讯云】新注册用户域名抢购1元起>>>   

jenkins 任务上传到服务器后的执行脚本,

关闭tomcat-->备份删除旧应用-->解压war-->删除war-->正则替换发布环境配置文件-->启动tomcat

#!/bin/sh
#chmod u+x /ucap/jenkins/bashScript/user-center.sh
#sh /ucap/jenkins/bashScript/user-center.sh

#定义变量开始
killTime=1
jdkName="jdk_1_7_0_80"
tomcatName="tomcat_7_0_72_X64_8789_gxw"
tomcatPath="/tools/tomcat/$tomcatName"
serviceName="tomcat_7_0_72_X64_8789_gxw"
ucapPath="/ucap"

appName="user-center"
appPath="$ucapPath/apps/$appName"

jenkinsPath="$ucapPath/jenkins"
backAppPath="$jenkinsPath/bak/$appName"
warPath="$jenkinsPath/war"
bashPath="$jenkinsPath/bashScript/$appName.sh"
logPath="$jenkinsPath/publishLog/$appName-publish.log"



#定义变量结束







echo "#################################################" >> $logPath
echo "############## begin run $appName bash  $bashPath ###################" >> $logPath
echo "######################" $(date +"%Y-%m-%d %H:%M:%S:%N") "###########################" >> $logPath
echo "#################################################" >> $logPath




echo stop $tomcatPath
#sh $tomcatPath/bin/shutdown.sh

echo stop systemctl $serviceName
systemctl stop $serviceName

tomcatPid=`ps -ef | grep $jdkName | grep $tomcatName |awk '{print $2}'`

if [ -n "$tomcatPid" ]
then {  
	echo sleep $killTime 
	sleep $killTime 
	kill -9 $tomcatPid
	echo $(date +"%Y-%m-%d %H:%M:%S:%N")" 睡眠$killTime秒后,kill $tomcatName运行的pid为 "$tomcatPid >> $logPath
} else  {
	echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 已停止服务[stop systemctl $serviceName]或 未发现$tomcatName运行 " >> $logPath
}
fi  


#删除日志
#rm -rf $ucapPath/log/$appName/*
#rm -rf $tomcatPath/logs/*
rm -rf $tomcatPath/work/Catalina/localhost/$appName/*
#删除日志


echo back $appPath
if [ -x "$appPath" ]
then {
	backName="$backAppPath/$appName-$(date +%Y%m%d-%H%M%S).tar.gz"
	tar -zcf $backName $appPath
	echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 备份$appPath目录到$backName" >> $logPath
} else {
	echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 未找到$appPath目录 " >> $logPath
}
fi



echo del $appPath
if [ -x "$appPath" ]
then {
	rm -rf $appPath/*
	echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 删除$appPath目录下所有文件 " >> $logPath
} else {
	echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 未找到$appPath目录 " >> $logPath
}
fi


echo zip $warPath/$appName.war
if [ -f "$warPath/$appName.war" ]
then {	
	unzip -oq $warPath/$appName.war -d $appPath
	echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 解压$warPath/$appName.war 到 $appPath " >> $logPath
} else {
	echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 未找到$warPath/$appName.war文件 " >> $logPath
}
fi


echo del $warPath/$appName.war
if [ -f "$warPath/$appName.war" ]
then {		
	rm -rf $warPath/$appName.war
	echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 删除$warPath/$appName.war " >> $logPath
} else {
	echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 未找到$warPath/$appName.war文件 " >> $logPath
}
fi



echo update config
echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 修改配置文件开始 " >> $logPath

log4jFile="$appPath/WEB-INF/classes/log4j.properties"
projectFile="$appPath/WEB-INF/classes/user.properties"
attrFile="$appPath/WEB-INF/classes/fileUpload.properties"
cacheFileLocal="$appPath/WEB-INF/classes/cache/ehcache-local.xml"
cacheFileRmi="$appPath/WEB-INF/classes/cache/ehcache-rmi.xml"

echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 修改日志配置 path=$log4jFile" >> $logPath
	replacePropertieKey="log4j.appender.RollingFile.File"
	echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 修改日志配置 处理$replacePropertieKey" >> $logPath
	sed -i "s/^$replacePropertieKey/#$replacePropertieKey/g" $log4jFile
	sed -i "s/^#ucap_$replacePropertieKey/$replacePropertieKey/g" $log4jFile

echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 修改项目配置 path=$projectFile" >> $logPath
	replacePropertieKey="cas.project.url"
	echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 修改附件配置 处理$replacePropertieKey" >> $logPath
	sed -i "s/^$replacePropertieKey/#$replacePropertieKey/g" $projectFile
	sed -i "s/^#ucap_$replacePropertieKey/$replacePropertieKey/g" $projectFile


echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 修改附件配置 path=$attrFile" >> $logPath
	replacePropertieKey="userfiles.basedir"
	echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 修改附件配置 处理$replacePropertieKey" >> $logPath
	sed -i "s/^$replacePropertieKey/#$replacePropertieKey/g" $attrFile
	sed -i "s/^#ucap_$replacePropertieKey/$replacePropertieKey/g" $attrFile

	replacePropertieKey="projectPath"
	echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 修改附件配置 处理$replacePropertieKey" >> $logPath
	sed -i "s/^$replacePropertieKey/#$replacePropertieKey/g" $attrFile
	sed -i "s/^#ucap_$replacePropertieKey/$replacePropertieKey/g" $attrFile
	
	replacePropertieKey="file.root.path"
	echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 修改附件配置 处理$replacePropertieKey" >> $logPath
	sed -i "s/^$replacePropertieKey/#$replacePropertieKey/g" $attrFile
	sed -i "s/^#ucap_$replacePropertieKey/$replacePropertieKey/g" $attrFile

	replacePropertieKey="file.system.url"
	echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 修改附件配置 处理$replacePropertieKey" >> $logPath
	sed -i "s/^$replacePropertieKey/#$replacePropertieKey/g" $attrFile
	sed -i "s/^#ucap_$replacePropertieKey/$replacePropertieKey/g" $attrFile

echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 修改缓存配置 path=$cacheFileLocal" >> $logPath
	sed -i "s/^\t<diskStore path=\"\(.\+\)\/>/<\!-- & -->/g" $cacheFileLocal
	sed -i -e "s/<\!-- ucap_//g" -e "s/ucap_ -->//g" $cacheFileLocal

echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 修改缓存配置 path=$cacheFileRmi" >> $logPath
	sed -i "s/^\t<diskStore path=\"\(.\+\)\/>/<\!-- & -->/g" $cacheFileRmi
	sed -i -e "s/<\!-- ucap_//g" -e "s/ucap_ -->//g" $cacheFileRmi


echo update config end
echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 修改配置文件开始 " >> $logPath



echo start $tomcatPath
#sh $tomcatPath/bin/startup.sh
#echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 启动$tomcatPath/bin/startup.sh " >> $logPath
systemctl start $serviceName
echo $(date +"%Y-%m-%d %H:%M:%S:%N") " 启动$serviceName 服务 " >> $logPath




echo "#################################################" >> $logPath
echo "############## end run $appName bash $bashPath ###################" >> $logPath
echo "######################" $(date +"%Y-%m-%d %H:%M:%S:%N") "###########################" >> $logPath
echo "#################################################" >> $logPath
echo "" >> $logPath
echo "" >> $logPath
echo "" >> $logPath


 

标签: jenkins linux bash
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 18
码字总数 3247
×
成智
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: