Jenkins (1、自动化发布war包、2、自动化发布nodejs)

 

1、持续集成javaweb

 

首先咱们需要安装一个 Jenkins,这个就不必多说了,晚上一搜索一大把,然后安装各种插件,配置各种环境变量,

今天我的实验环境是

使用Jenkins 拉取gitlap上项目的代码,然后自动编译并发布到tomcat中。

 

配置Jenkins

1、这里配置的就是你代码所存放的位置,还有你的项目名称和描述之类的

 

2、配置触发器所需执行的一些东西,我这里配置了一个定时检测拉取的动作。

3、配置tomcta

 

 

这里要注意,最好单独创建一个账号,并赋予相关权限

<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
  <user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
  <user username="role1" password="<must-be-changed>" roles="role1"/>
-->

<!-- <role rolename="admin"/> -->
<!-- <role rolename="admin-gui"/> -->
<!-- <role rolename="admin-script"/> -->
<!-- <role rolename="manager"/> -->
<!-- <role rolename="manager-gui"/> -->
<!-- <role rolename="manager-script"/> -->
<!-- <role rolename="manager-jmx"/> -->
<!-- <role rolename="manager-status"/> -->
<user name="admin" password="adminadmin" roles="admin,manager,admin-gui,admin-script,manager-gui,manager-script,manager-jmx,manager-status" />
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="upload" password="upload" roles="manager-gui,manager-script"/>
</tomcat-users>

 

构建并查看日志

点击开始构建,然后查看输出日志结果,如果没有报错,大概就没有啥问题,说下我这里遇到的一个坑

 

 

查看了一下大概报错原因,然后我将context关掉了,这个是否要求上下文路径,这个要看代码里pom文件里有没有强制要求,如果没有就不用写,

然后我清除了一下工作目录,自动部署成功日志输出也正常了,

解决问题链接地址:

 https://blog.csdn.net/alanzyy/article/details/60596402

http://www.alanzeng.cn/2017/03/jenkins-random-failed-deploy-tomcat/

 

正确输出

Started by user starry
Building in workspace /home/Jenkins-home/.jenkins/workspace/test-raymedy
Cloning the remote Git repository
remote: Counting objects
Receiving objects
Resolving deltas
Updating references
Checking out Revision 9ea6a0d4951c90b9c62cc9c03c4b3e575804e5ec (refs/remotes/origin/master)
Commit message: "Merge remote-tracking branch 'origin/master'"
Parsing POMs
Modules changed, recalculating dependency graph
Established TCP socket on 58075
[test-raymedy] $ /home/java/jdk1.8.0_65/bin/java -cp /home/Jenkins-home/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven33-agent-1.11.jar:/home/maven339/boot/plexus-classworlds-2.5.2.jar:/home/maven339/conf/logging jenkins.maven3.agent.Maven33Main /home/maven339 /home/apache-tomcat-jenkins/webapps/ROOT/WEB-INF/lib/remoting-3.7.jar /home/Jenkins-home/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven33-interceptor-1.11.jar /home/Jenkins-home/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-commons-1.11.jar 58075
<===[JENKINS REMOTING CAPACITY]===>channel started
Executing Maven:  -B -f /home/Jenkins-home/.jenkins/workspace/test-raymedy/pom.xml install
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building raymedy Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ raymedy ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 9 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ raymedy ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 114 source files to /home/Jenkins-home/.jenkins/workspace/test-raymedy/target/classes
[WARNING] /home/Jenkins-home/.jenkins/workspace/test-raymedy/src/com/topsci/Tools/FileTool.java:[11,16] sun.misc.BASE64Encoder is internal proprietary API and may be removed in a future release
[WARNING] /home/Jenkins-home/.jenkins/workspace/test-raymedy/src/com/topsci/Tools/AESEncryptTool.java:[5,16] sun.misc.BASE64Decoder is internal proprietary API and may be removed in a future release
[WARNING] /home/Jenkins-home/.jenkins/workspace/test-raymedy/src/com/topsci/Tools/AESEncryptTool.java:[6,16] sun.misc.BASE64Encoder is internal proprietary API and may be removed in a future release
[WARNING] /home/Jenkins-home/.jenkins/workspace/test-raymedy/src/com/topsci/Tools/FileTool.java:[11,16] sun.misc.BASE64Encoder is internal proprietary API and may be removed in a future release
[WARNING] /home/Jenkins-home/.jenkins/workspace/test-raymedy/src/com/topsci/Tools/AESEncryptTool.java:[5,16] sun.misc.BASE64Decoder is internal proprietary API and may be removed in a future release
[WARNING] /home/Jenkins-home/.jenkins/workspace/test-raymedy/src/com/topsci/Tools/AESEncryptTool.java:[6,16] sun.misc.BASE64Encoder is internal proprietary API and may be removed in a future release
[WARNING] /home/Jenkins-home/.jenkins/workspace/test-raymedy/src/com/topsci/Tools/FileTool.java:[11,16] sun.misc.BASE64Encoder is internal proprietary API and may be removed in a future release
[WARNING] /home/Jenkins-home/.jenkins/workspace/test-raymedy/src/com/topsci/Tools/AESEncryptTool.java:[5,16] sun.misc.BASE64Decoder is internal proprietary API and may be removed in a future release
[WARNING] /home/Jenkins-home/.jenkins/workspace/test-raymedy/src/com/topsci/Tools/AESEncryptTool.java:[6,16] sun.misc.BASE64Encoder is internal proprietary API and may be removed in a future release
[WARNING] /home/Jenkins-home/.jenkins/workspace/test-raymedy/src/com/topsci/Tools/FileTool.java:[11,16] sun.misc.BASE64Encoder is internal proprietary API and may be removed in a future release
[WARNING] /home/Jenkins-home/.jenkins/workspace/test-raymedy/src/com/topsci/Tools/AESEncryptTool.java:[5,16] sun.misc.BASE64Decoder is internal proprietary API and may be removed in a future release
[WARNING] /home/Jenkins-home/.jenkins/workspace/test-raymedy/src/com/topsci/Tools/AESEncryptTool.java:[6,16] sun.misc.BASE64Encoder is internal proprietary API and may be removed in a future release
[WARNING] /home/Jenkins-home/.jenkins/workspace/test-raymedy/src/com/topsci/Tools/FileTool.java:[389,26] sun.misc.BASE64Encoder is internal proprietary API and may be removed in a future release
[WARNING] /home/Jenkins-home/.jenkins/workspace/test-raymedy/src/com/topsci/Tools/AESEncryptTool.java:[38,20] sun.misc.BASE64Encoder is internal proprietary API and may be removed in a future release
[WARNING] /home/Jenkins-home/.jenkins/workspace/test-raymedy/src/com/topsci/Tools/AESEncryptTool.java:[54,37] sun.misc.BASE64Decoder is internal proprietary API and may be removed in a future release
[WARNING] /home/Jenkins-home/.jenkins/workspace/test-raymedy/src/com/topsci/Tools/Message/SMSUtil.java: /home/Jenkins-home/.jenkins/workspace/test-raymedy/src/com/topsci/Tools/Message/SMSUtil.java uses or overrides a deprecated API.
[WARNING] /home/Jenkins-home/.jenkins/workspace/test-raymedy/src/com/topsci/Tools/Message/SMSUtil.java: Recompile with -Xlint:deprecation for details.
[WARNING] /home/Jenkins-home/.jenkins/workspace/test-raymedy/src/com/topsci/Resources/BasicService.java: Some input files use unchecked or unsafe operations.
[WARNING] /home/Jenkins-home/.jenkins/workspace/test-raymedy/src/com/topsci/Resources/BasicService.java: Recompile with -Xlint:unchecked for details.
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ raymedy ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/Jenkins-home/.jenkins/workspace/test-raymedy/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ raymedy ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ raymedy ---
[INFO] No tests to run.
[JENKINS] Recording test results
[INFO] 
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ raymedy ---
[INFO] Packaging webapp
[INFO] Assembling webapp [raymedy] in [/home/Jenkins-home/.jenkins/workspace/test-raymedy/target/raymedy]
[INFO] Processing war project
[INFO] Copying webapp resources [/home/Jenkins-home/.jenkins/workspace/test-raymedy/WebContent]
[INFO] Webapp assembled in [122 msecs]
[INFO] Building war: /home/Jenkins-home/.jenkins/workspace/test-raymedy/target/raymedy.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ raymedy ---
[INFO] Installing /home/Jenkins-home/.jenkins/workspace/test-raymedy/target/raymedy.war to /root/.m2/repository/raymedy/raymedy/1.0-SNAPSHOT/raymedy-1.0-SNAPSHOT.war
[INFO] Installing /home/Jenkins-home/.jenkins/workspace/test-raymedy/pom.xml to /root/.m2/repository/raymedy/raymedy/1.0-SNAPSHOT/raymedy-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.680 s
[INFO] Finished at: 2019-01-13T10:48:17+08:00
[INFO] Final Memory: 36M/551M
[INFO] ------------------------------------------------------------------------
[JENKINS] Archiving /home/Jenkins-home/.jenkins/workspace/test-raymedy/pom.xml to raymedy/raymedy/1.0-SNAPSHOT/raymedy-1.0-SNAPSHOT.pom
[JENKINS] Archiving /home/Jenkins-home/.jenkins/workspace/test-raymedy/target/raymedy.war to raymedy/raymedy/1.0-SNAPSHOT/raymedy-1.0-SNAPSHOT.war
channel stopped
Deploying /home/Jenkins-home/.jenkins/workspace/test-raymedy/target/raymedy.war to container Tomcat 7.x Remote with context 
  Redeploying [/home/Jenkins-home/.jenkins/workspace/test-raymedy/target/raymedy.war]
  Undeploying [/home/Jenkins-home/.jenkins/workspace/test-raymedy/target/raymedy.war]
  Deploying [/home/Jenkins-home/.jenkins/workspace/test-raymedy/target/raymedy.war]
Finished: SUCCESS

最终效果

 

2、持续集成nodejs

nodejs相比java还要简单一些,Jenkins完成构建发送发布服务器直接执行就ok了

1、第一步跟java的一样,设置项目的一些必要参数,以及你的项目的git的地址

 

 2、选择你构建的环境,前提是你要装好nodejs,和Jenkins对nodejs支持的一下插件

我这里做的就是在Jenkins的目录下将项目安装并打包。

 

 3、推送到发布服务器,执行相应命令,来运行你的项目

这里如果比较简单的命令可以直接在  Exec command 里面填写,但是如果过多最好还是已脚本的形式,一个是便于修改,还有就是便于测试。

要记住一点就是,这个没有自动更新的功能,所以要设置,关闭老项目,清理旧的文件或者备份,并启动新的项目

脚本

我这里没有写日志,如果发生错误了,不便于排查,最好可以将脚本的执行结果打印到某一文件中方便查询排错。

#!/bin/bash
cd /home
killall node
rm -rf  /home/protct/
unzip healthy_protect_server.zip -d /home/protct/
rm -rf /home/healthy_protect_server.zip
setsid  /usr/local/src/node-v8.9.1-linux-x64/bin/node  /home/protct/bin/healthy_protect_server &
touch /okle
exit 0
          

 

posted @ 2019-01-13 11:13  SmallNine  阅读(1132)  评论(0编辑  收藏  举报