本文共 2663 字,大约阅读时间需要 8 分钟。
背景:使用gitlab+jenkins+pipeline打包java项目
1.配置jenkins+pipeline项目备注上面的代码项 目 地址:http://git.bqjr.club/NCP/new_crm_platform.git<br/><br/>服务器地址:PRD 环境192.168.5.75 <br/><br/>项目 经理:<br/><br/>开发 人员:<br/><br/><p style="color: #e9013b !important;font-weight: 700;">注意事项: 由于生产环境是负载均衡, 请注意单台部署! </p>
2.配置pipeline备注:上面的代码<p style="color: #e9013b !important;font-weight: 700;">选择分支,根据项目组提供的分支构建</p>
3.pipeline语法`node { try { stage('代码拉取') { //input id: 'ncpprd', message: '发布生产请找-/admin--批准?', ok: '确认', submitter: 'admin,xiongxinjiang'sh "rm -rf /var/lib/jenkins/jobs/prd-mt-ncp75/workspace/*"git branch: "${BranchName}", credentialsId: 'xiongxj', url: ''}stage('项目构建') { sh "/usr/local/maven/bin/mvn -Dmaven.test.skip=true clean package"}stage('上传WAR包') { sh "scp -P 10022 target/ncp-0.0.1-SNAPSHOT.war bqadm@192.168.5.75:/home/bqadm/sftp/"}stage('项目部署'){ sh "ssh -p 10022 bqadm@192.168.5.75 'sudo sh -x /opt/shell/ncp_deploy75.sh'" } }catch (any) { currentBuild.result = 'FAILURE' throw any}
}`
4.ncp_deploy75.代码
#/bin/bash#买买提新客服平台系统自动部署脚本 适用tomcat程序JAVA_HOME=/usr/local/jdk1.8.0_144JRE_HOME=/usr/local/jdk1.8.0_144/jreCLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/libPATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/binCATALINA_HOME=/opt/ncp-tomcatexport JAVA_HOME JRE_HOME CLASS_PATH PATH############################################app_name=ncp-tomcatecho "==============[停止tomcat]=================="/opt/ncp-tomcat/bin/catalina.sh stop#ps -ef |grep -v grep |grep "${app_name}"ps -ef |grep java |grep "${app_name}" |grep -v grep if [ "$?" == 0 ];then for x in `ps -ef |grep -v grep |grep java |grep "${app_name}" |awk '{print $2}'`;do echo "Killing pid $x ..." sudo kill -15 $x doneelse echo "Stop app [ SUCCESS ]"fisleep 3ps -ef |grep -v grep |grep java |grep "${app_name}" if [ "$?" == 0 ];then for x in `ps -ef |grep -v grep |grep java |grep "${app_name}" |awk '{print $2}'`;do echo "Killing pid $x ..." sudo kill -9 $x doneelse echo "Stop app [ SUCCESS ]"sleep 3fiecho "==============文件备份========================"if [ ! -f "/home/bqadm/sftp/ncp-0.0.1-SNAPSHOT.war" ];then echo "没有文件或已为最新文件"else sudo mv /opt/ncp-tomcat/webapps/ncp.war /opt/war_bak/ncp.war_$(date +%Y-%m-%d_%H%M%S) sudo rm -rf /opt/ncp-tomcat/webapps/*echo "============拷贝文件======================" mv /home/bqadm/sftp/ncp-0.0.1-SNAPSHOT.war /opt/ncp-tomcat/webapps/ncp.war sleep 5 echo "================重启tomcat============" /opt/ncp-tomcat/bin/startup.sh sleep 10 ps -ef |grep java |grep "${app_name}" |grep -v grepfi
转载于:https://blog.51cto.com/whitehat/2399403