Jenkins 配置 SpringBoot 自动构建部署

服务器版本

Linux version 3.10.0-957.12.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) ) #1 SMP Mon Apr 29 14:59:59 UTC 2019

准备 :

Jenkins 官网 : https://jenkins.io

Jenkins执行安装

启动 Jenkins :

[root@hybn guns-logs]# service jenkins start/stop/restart
Usage: /etc/init.d/jenkins {start|stop|status|try-restart|restart|force-reload|reload|probe}
[root@hybn guns-logs]# chkconfig jenkins on

查看端口 :

[root@hybn guns-logs]# cat /etc/sysconfig/jenkins 

## Type:        integer(0:65535)
## Default:     8080
## ServiceRestart: jenkins
#
# Port Jenkins is listening on.
# Set to -1 to disable
#
JENKINS_PORT="8080"

开放端口 :

[root@hybn guns-logs]# firewall-cmd --zone=public --add-port=8080/tcp --permanent

配置Java路径 :

[root@hybn guns-logs]# vim /etc/init.d/jenkins 
-----------------S----------------------
candidates="
/hybn/package/jdk/jdk1.8.0_211/bin/java --添加Java路径
/etc/alternatives/java
/usr/lib/jvm/java-1.8.0/bin/java
/usr/lib/jvm/jre-1.8.0/bin/java
/usr/lib/jvm/java-1.7.0/bin/java
/usr/lib/jvm/jre-1.7.0/bin/java
/usr/bin/java
"
-----------------E----------------------

[root@hybn init.d]# service jenkins start 
Starting jenkins (via systemctl):  Warning: jenkins.service changed on disk. Run 'systemctl daemon-reload' to reload units.
                                                           [  OK  ]

运行服务 :

[root@hybn jenkins]# firewall-cmd --zone=public --add-port=8080/tcp --permanent 

jenkins.war nohup.out start.sh

[root@hybn jenkins]# firewall-cmd --zone=public --list-ports
3306/tcp 22/tcp 443/tcp 80/tcp 8080/tcp 1001/tcp

[root@hybn jenkins]# service jenkins start
Starting jenkins (via systemctl): [ OK ]

浏览器打开:http:<ip>:<host>

在配置第一个管理员账户后会出现卡死,这时候重启jenkins服务即可 :

[root@hybn jenkins]# systemctl reload jenkins

配置构建环境 :

Post Steps 构建后执行Shell :

网上的看不懂,自己捣鼓了一个,能用。

#!/bin/bash

echo "===================开始部署ERP================"
pid=`pgrep -f erp.jar`
#判断进程
if [ -n "$pid" ];then
	kill -9 $pid
fi

#生成当前时间戳
date_dir="/hybn/jar/history/$(date +%y_%m_%d-%H:%M:%S)"
echo "$date_dir"
#创建历史文件夹 根据时间戳命名
mkdir "$date_dir"
#移动到历史记录
if [ ! -f "/hybn/jar/run/erp.jar" ];then
	echo "jar不存在 直接部署"
	mv /var/lib/jenkins/workspace/erp_web/target/erp.jar /hybn/jar/run
else	
	echo "jar存在 备份"
	mv /hybn/jar/run/erp.jar $date_dir
	mv /var/lib/jenkins/workspace/erp_web/target/erp.jar /hybn/jar/run
fi


cd /hybn/jar/run

#将生成的jar移动到项目目录

cd /hybn/jar/run

./start.sh

echo "===================部署成功!================"

结果 :

0:31:38 Started by user 杨超杰
00:31:38 Building in workspace /var/lib/jenkins/workspace/erp_web
00:31:38 using credential b9b73622-ceb7-4d43-8ede-005f4d817712
00:31:38  > /usr/bin/git rev-parse --is-inside-work-tree # timeout=10
00:31:38 Fetching changes from the remote Git repository
00:31:38  > /usr/bin/git config remote.origin.url https://gitee.com/huawin_bainer_information/erp.git # timeout=10
00:31:38 Fetching upstream changes from https://gitee.com/huawin_bainer_information/erp.git
00:31:38  > /usr/bin/git --version # timeout=10
00:31:38 using GIT_ASKPASS to set credentials 
00:31:38  > /usr/bin/git fetch --tags --progress https://gitee.com/huawin_bainer_information/erp.git +refs/heads/*:refs/remotes/origin/*
00:31:39  > /usr/bin/git rev-parse refs/remotes/origin/master^{commit} # timeout=10
00:31:39  > /usr/bin/git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
00:31:39 Checking out Revision d69a25f8c207ebad5dd6a7274322fd7f0878d091 (refs/remotes/origin/master)
00:31:39  > /usr/bin/git config core.sparsecheckout # timeout=10
00:31:39  > /usr/bin/git checkout -f d69a25f8c207ebad5dd6a7274322fd7f0878d091
00:31:39 Commit message: "Sql更新,账户Bug修复,费用中心选择账户,费用计算,新增费用记录表。"
00:31:39  > /usr/bin/git rev-list --no-walk d69a25f8c207ebad5dd6a7274322fd7f0878d091 # timeout=10
00:31:39 Parsing POMs
00:31:40 Established TCP socket on 34128
00:31:40 [erp_web] $ /hybn/package/jdk/jdk1.8.0_211//bin/java -cp /var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-agent-1.12.jar:/usr/share/maven/boot/plexus-classworlds.jar org.jvnet.hudson.maven3.agent.Maven3Main /usr/share/maven /var/cache/jenkins/war/WEB-INF/lib/remoting-3.29.jar /var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-1.12.jar /var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-commons-1.12.jar 34128
00:31:43 <===[JENKINS REMOTING CAPACITY]===>channel started
00:31:46 Executing Maven:  -B -f /var/lib/jenkins/workspace/erp_web/pom.xml clean package -Dmaven.test.skip=true
00:31:48 [INFO] Scanning for projects...
00:31:49 [INFO]                                                                         
00:31:49 [INFO] ------------------------------------------------------------------------
00:31:49 [INFO] Building guns-admin 1.0.0
00:31:49 [INFO] ------------------------------------------------------------------------
00:31:54 [INFO] 
00:31:54 [INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ erp ---
00:31:54 [INFO] Deleting /var/lib/jenkins/workspace/erp_web/target
00:31:55 [INFO] 
00:31:55 [INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ erp ---
00:31:55 [INFO] Using 'UTF-8' encoding to copy filtered resources.
00:31:55 [INFO] Copying 601 resources
00:31:55 [INFO] Copying 8 resources
00:31:56 [INFO] Copying 59 resources
00:31:56 [INFO] 
00:31:56 [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ erp ---
00:31:57 [INFO] Changes detected - recompiling the module!
00:31:57 [INFO] Compiling 510 source files to /var/lib/jenkins/workspace/erp_web/target/classes
00:32:18 [WARNING] /var/lib/jenkins/workspace/erp_web/src/main/java/cn/hybn/erp/modular/system/service/impl/InventoryTransactionServiceImpl.java: Some input files use unchecked or unsafe operations.
00:32:18 [WARNING] /var/lib/jenkins/workspace/erp_web/src/main/java/cn/hybn/erp/modular/system/service/impl/InventoryTransactionServiceImpl.java: Recompile with -Xlint:unchecked for details.
00:32:19 [INFO] 
00:32:19 [INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ erp ---
00:32:19 [INFO] Not copying test resources
00:32:19 [INFO] 
00:32:19 [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ erp ---
00:32:19 [INFO] Not compiling test sources
00:32:19 [INFO] 
00:32:19 [INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ erp ---
00:32:19 [INFO] Tests are skipped.
00:32:20 [WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.BuildInfoRecorder$1; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/
00:32:20 [INFO] 
00:32:20 [INFO] --- maven-jar-plugin:3.1.0:jar (default-jar) @ erp ---
00:32:20 [INFO] Building jar: /var/lib/jenkins/workspace/erp_web/target/erp.jar
00:32:21 [INFO] 
00:32:21 [INFO] --- spring-boot-maven-plugin:2.1.1.RELEASE:repackage (repackage) @ erp ---
00:32:22 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
00:32:22 SLF4J: Defaulting to no-operation (NOP) logger implementation
00:32:22 SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
00:32:24 [INFO] Replacing main artifact with repackaged archive
00:32:24 [INFO] 
00:32:24 [INFO] --- spring-boot-maven-plugin:2.1.1.RELEASE:repackage (default) @ erp ---
00:32:24 [INFO] Replacing main artifact with repackaged archive
00:32:25 [INFO] ------------------------------------------------------------------------
00:32:25 [INFO] BUILD SUCCESS
00:32:25 [INFO] ------------------------------------------------------------------------
00:32:25 [INFO] Total time: 37.107s
00:32:25 [INFO] Finished at: Thu May 16 00:32:25 CST 2019
00:32:26 [INFO] Final Memory: 66M/477M
00:32:26 [INFO] ------------------------------------------------------------------------
00:32:26 Waiting for Jenkins to finish collecting data
00:32:28 [JENKINS] Archiving /var/lib/jenkins/workspace/erp_web/pom.xml to cn.hybn/erp/1.0.0/erp-1.0.0.pom
00:32:28 [JENKINS] Archiving /var/lib/jenkins/workspace/erp_web/target/erp.jar to cn.hybn/erp/1.0.0/erp-1.0.0.jar
00:32:29 [erp_web] $ /bin/bash /tmp/jenkins1615489592064061878.sh
00:32:29 channel stopped
00:32:29 ===================开始部署ERP================
00:32:29 /tmp/jenkins1615489592064061878.sh: line 7: kill: (13629) - Operation not permitted
00:32:29 /hybn/jar/history/19_05_16-00:32:29
00:32:29 mkdir: cannot create directory ‘/hybn/jar/history/19_05_16-00:32:29’: Permission denied
00:32:29 jar存在 备份
00:32:29 mv: cannot move ‘/hybn/jar/run/erp.jar’ to ‘/hybn/jar/history/19_05_16-00:32:29’: Permission denied
00:32:29 mv: cannot move ‘/var/lib/jenkins/workspace/erp_web/target/erp.jar’ to ‘/hybn/jar/run/erp.jar’: Permission denied
00:32:29 ===================部署成功!================
00:32:34 2019-05-16 00:32:34.536 ERROR 14039 --- [           main] o.s.boot.SpringApplication               : Application run failed

Jenkins Root 运行 :

[root@hybn tmp]# vim /etc/sysconfig/jenkins 
JENKINS_USER="root"
[root@hybn tmp]# chown -R root:root /usr/lib/jenkins
[root@hybn tmp]# chown -R root:root /var/cache/jenkins
[root@hybn tmp]# chown -R root:root /var/log/jenkins
[root@hybn jenkins]# systemctl reload jenkins
[root@hybn jenkins]# ps -ef | grep jenkins
root     20115     1 99 13:13 ?        00:02:17 /hybn/package/jdk/jdk1.8.0_211/bin/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/libjenkins -jar /usr/lib/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --daemon --httpPort=8080 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20
root     20295 20115 99 13:14 ?        00:00:30 /hybn/package/jdk/jdk1.8.0_211//bin/java -cp /var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-agent-1.12.jar:/usr/share/maven/boot/plexus-classworlds.jar org.jvnet.hudson.maven3.agent.Maven3Main /usr/share/maven /var/cache/jenkins/war/WEB-INF/lib/remoting-3.29.jar /var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-1.12.jar /var/lib/jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-commons-1.12.jar 41182
root     20452 19582  0 13:14 pts/0    00:00:00 grep --color=auto jenkins

DontKillME :

#!/bin/bash

echo "===================开始部署ERP================"
pid=`pgrep -f erp.jar`
#判断进程
if [ -n "$pid" ];then
	kill -9 $pid
fi

#生成当前时间戳
date_dir="/hybn/jar/history/$(date +%y_%m_%d-%H:%M:%S)"
echo "$date_dir"
#创建历史文件夹

mkdir "$date_dir"
#移动到历史记录
if [ ! -f "/hybn/jar/run/erp.jar" ];then
	echo "jar不存在 直接部署"
	mv /var/lib/jenkins/workspace/erp_web/target/erp.jar /hybn/jar/run
else	
	echo "jar存在 备份"
	mv /hybn/jar/run/erp.jar $date_dir
	mv /var/lib/jenkins/workspace/erp_web/target/erp.jar /hybn/jar/run
fi


cd /hybn/jar/run

#将生成的jar移动到项目目录


#启动

project_path=/hybn/jar/run
cd $project_path

BUILD_ID=dontKillMe nohup java -Dfile.encoding=UTF-8 -jar erp.jar   --spring.profiles.active=dev --server.port=8089 2>&1 &

dpid=`pgrep -f erp.jar`
echo "PID----------$dpid------------------"
echo "===================部署成功!================"
posted @ 2019-06-28 14:10  杨超杰  阅读(1549)  评论(0编辑  收藏  举报