Jenkins自动化部署-配置项目(下)

一、新建项目,设置Git地址,账号密码。

多种方式,ssh方式,需要配置私钥。

第1个错误:Git没安装

Linux机器上,先安装Git软件。 CentOs系统,yum install git

git -version 检查是否安装成功

解决之后,再次构建之后,产生了项目目录。

cd /root/.jenkins/workspace/test-auto-deploy

第2个错误:权限不对,说明git clone,http访问的账号密码没有设置正确。

The requested URL returned error: 401 Unauthorized while accessing

生成公钥-私钥对

ssh-keygen -t rsa -C xxx@qq.com 密钥目录,不输入,使用默认目录。 密码不输入,为空,直接登录。

cat /root/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAygIraVbu9xWAJClr3OD5d5fXzqN55DnZ6HCwmxh/n59VuxSYU+aA7YO0x7hC1gQmQVyhkteoPOglxR1oNjykKiuLPMjuohjq+7uN4/tPtV4J0bEVisYyHcfRbGiMqg1AwIXGdu1ZFkkq8N1QRUcVFvIMtJNv79En2IgXjBpso+mZDw7P5NYbewwVF9aYEunm0dmA4MZktu8IZx5t9IdTJdACQSQQ5uGLZCJCfowAfborroAb5N8LtlOlkP+U0t303zybT4KqgOLfGvERN9oOMcrI2MCOTsI4ch64J8AOZ7xi8JiO7J66mOEVSqUYjcziaihOo0ljxzkdEFCV430IHw== xxx@qq.com

cat /root/.ssh/id_rsa

----BEGIN RSA PRIVATE KEY----- MIIEogIBAAKCAQEAygIraVbu9xWAJClr3OD5d5fXzqN55DnZ6HCwmxh/n59VuxSY U+aA7YO0x7hC1gQmQVyhkteoPOglxR1oNjykKiuLPMjuohjq+7uN4/tPtV4J0bEV isYyHcfRbGiMqg1AwIXGdu1ZFkkq8N1QRUcVFvIMtJNv79En2IgXjBpso+mZDw7P 5NYbewwVF9aYEunm0dmA4MZktu8IZx5t9IdTJdACQSQQ5uGLZCJCfowAfborroAb 5N8LtlOlkP+U0t303zybT4KqgOLfGvERN9oOMcrI2MCOTsI4ch64J8AOZ7xi8JiO 7J66mOEVSqUYjcziaihOo0ljxzkdEFCV430I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NS6dSKdEVBRHDR63NOTgNNBBBr4E7BLQGuy88iXuBVbTEo7GfdMOTR pr7PJzaiNgHQqSmsIxW08Zst9aYM/siQ/h6ssVadJ8R3AjidP2BMXVKpyWEfEB/N nx0Wc+7kvOtEdQMQ1dQe6qMgvWSOSQW7+lIHYkPtIvlE00/igMU= -----END RSA PRIVATE KEY-----

Gitee项目,增加公钥

部署公钥管理-添加公钥

Git仓库地址:git@gitee.com:xxx/techplatform.git (有网友用的是 git://开头,git代码托管在gitee,难道是这个不太一样)

配置Credentials:Username with password 账号:aa@qq.com 密码:123456

二、Jenkins首次控制台输出

Started by user fansunion Running as SYSTEM Building in workspace /root/.jenkins/workspace/test-auto-deploy using credential jtn

git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository git config remote.origin.url git@gitee.com:xxx/techplatform.git # timeout=10 Fetching upstream changes from git@gitee.com:xxx/techplatform.git git --version # timeout=10 using GIT_ASKPASS to set credentials git fetch --tags --progress git@gitee.com:xxx/techplatform.git +refs/heads/:refs/remotes/origin/ git rev-parse refs/remotes/origin/master^{commit} # timeout=10 git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10 Checking out Revision 7f90f556a423e67fe90da7abf1e57557d99b5e53 (refs/remotes/origin/master) git config core.sparsecheckout # timeout=10 git checkout -f 7f90f556a423e67fe90da7abf1e57557d99b5e53 Commit message: "like,config,photo,comment,collect" First time build. Skipping changelog. Triggering test-auto-deploy » default test-auto-deploy » default completed with result SUCCESS Finished: SUCCESS

说明Git clone代码成功了。

三、配置Maven

下载Maven http://maven.apache.org/download.cgi

解压,配置环境变量

vim /etc/profile export MAVEN_HOME=/home/soft/apache-maven-3.6.1 export MAVEN_HOME export PATH=$PATH:$MAVEN_HOME/bin

mvn -version

Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-05T03:00:29+08:00) Maven home: /home/soft/apache-maven-3.6.1 Java version: 1.8.0_181, vendor: Oracle Corporation, runtime: /home/soft/jdk1.8/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "2.6.32-696.3.2.el6.i686", arch: "i386", family: "unix"

安装Maven集成插件(Maven Integration)

重启Jenkins

http://47.15.99.4:9090/restart

新建项目,多了1个选项"构建一个maven项目"

构建一个maven项目.Jenkins利用你的POM文件,这样可以大大减轻构建配置.

配置Maven

构建-增加构建步骤-调用顶层Maven目标 配置如下: clean install -U -Dmaven.test.skip=true

执行构建,找不到mvn

[default] $ mvn -f pom.xml clean install -U -Dmaven.test.skip=true FATAL: command execution failed java.io.IOException: error=2, No such file or directory at java.lang.UNIXProcess.forkAndExec(Native Method) at java.lang.UNIXProcess.(UNIXProcess.java:247) at java.lang.ProcessImpl.start(ProcessImpl.java:134) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029) Caused: java.io.IOException: Cannot run program "mvn" (in directory "/root/.jenkins/workspace/jtn-techplatform/default"): error=2, No such file or directory at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)

系统管理-全局工具配置-Maven

Name: maven MAVEN_HOME:/home/soft/apache-maven-3.6.1

JDK配置

别名: JDK1.8 JAVA_HOME : /home/soft/jdk1.8

项目的配置Maven选择指定的名字“maven”,而不是默认

目标: clean install -U -Dmaven.test.skip=true

四、配置Shell脚本

自动化部署3步走,Git Clone代码,Maven打包,执行Jar包。

将应用停止,Copy jar到指定目录,增加权限,再执行jar

Shell脚本 echo "Stopping SpringBoot Application jtn-techplatform-impl.jar" pid=ps -ef | grep jtn-techplatform-impl.jar | grep -v grep | awk '{print $2}' if [ -n "$pid" ] then kill -9 $pid fi cp -f /root/.jenkins/workspace/jtn-techplatform/default/techplatform-impl/target/jtn-techplatform-impl.jar /home/app/jtn-techplatform-impl.jar cd /home/app chmod 777 /home/app/jtn-techplatform-impl.jar nohup java -jar jtn-techplatform-impl.jar >techplatform.log 2>&1 &

最难的提示:获取进程id grep -v grep,排除grep本身的简称

grep -v 可以实现 NOT 操作。 -v 选项用来实现反选匹配的( invert match)。如,可匹配得到除下指定pattern外的所有lines。 参考资料:https://www.jianshu.com/p/4ec50fdaf388

www 3142 2491 99 16:36 pts/0 00:01:15 java -jar system-release-1.0.0.jar awk '{print $2}' 获得第2个参数,3142就是想要的线程数/进程数

五、最后1个问题

nohup命令执行之后,Linux机器上并没有发现java应用正常运行。 在Jenkins里配置的是“nohup java -jar jtn-techplatform-impl.jar >techplatform.log 2>&1 &” 但是Jenkins控制台显示的是 “+ nohup java -jar jtn-techplatform-impl.jar”

一直以为是最后一个“&”没有让他在后台运行,发现怎么改都不行。 只好搜索“Jenkins nohup”,找到一篇文章:

背景

jenkins持续集成,需要任务后台执行(nohup执行)结果发现jenkins的job执行完后,看不到运行的进程

步骤

原因就是这么一个情况:Jenkins任务结束时候自动关掉了所有的子进程

不过可以设置一些东西让其可以在后台运行

其实就是在脚本中加入一句BUILD_ID=DONTKILLME

问题的根本在于是Jenkins使用processTreeKiller杀掉了所有子进程,而且这是Jenkins的默认行为。 其实回头来看这个问题,就发现Jenkins的做法非常合理。当一次build异常结束,或被人终止时, 必然需要结束所有这次build启动的子进程。下面的link提供了更多细节,以及解决方法。 https://wiki.jenkins-ci.org/display/JENKINS/ProcessTreeKiller

六、SpringBoot项目打包的3个小问题

犯了一个错误,Java资源文件目录在代码中写成了src/main/resource,应该用“复数s”。 src/main/resources/${deploy.type}src/main/resources dev/ test/ pro/*

SpringBoot项目打包用这个,pom.xml的Packaging需要配置成jar,不是pom,也不是war。 org.springframework.boot spring-boot-maven-plugin 1.4.2.RELEASE com.jiutianniao.techplatform.JtnTechPlatformApplication repackage

最后,核心jar包,引入third.jar,想扫描里面的mapper等resource资源。 //classpath private static final String CLASSPATH_MAPPER_XML = "classpath:/mapper/jtn/*.xml";

七、一些关键的截图

源码管理Git配置

Maven命令

执行Shell

echo "Stopping SpringBoot Application jtn-techplatform-impl.jar"
pid=`ps -ef | grep jtn-techplatform-impl.jar | grep -v grep | awk '{print $2}'`
if [ -n "$pid" ]
then
   kill -9 $pid
fi
cp -f /root/.jenkins/workspace/jtn-techplatform/default/techplatform-impl/target/jtn-techplatform-impl.jar /home/app/jtn-techplatform-impl.jar
cd /home/app
chmod 777 /home/app/jtn-techplatform-impl.jar
BUILD_ID=DONTKILLME

nohup java -jar jtn-techplatform-impl.jar >techplatform.log 2>&1 &

其它配置

 

 

八、参考资料

1. Jenkins安装及自动部署Maven项目

https://blog.csdn.net/iamniconico/article/details/82023173

2. 使用Jenkins配置Git+Maven的自动化构建

https://blog.csdn.net/u011679955/article/details/75949329

3. Jenkins编译报错:Cannot run program "mvn" (in directory "/var/lib/jenkins/workspace/mall"):

error=2, No such file or directory https://www.oschina.net/question/1756518_2182111

4. jenkins 后台程序运行

https://www.cnblogs.com/jwentest/p/8831274.html

5. jenkins 自动化部署 spring boot 项目(多图)

https://blog.csdn.net/sinat_34344123/article/details/80310603

 

九、最后再赠送一点 控制台日志

[INFO] Building techplatform-parent 1.0.0-SNAPSHOT                        [5/5]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ techplatform-parent ---
[INFO] Installing /root/.jenkins/workspace/jtn-techplatform/default/pom.xml to /root/.m2/repository/com//techplatform-parent/1.0.0-SNAPSHOT/techplatform-parent-1.0.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] common 1.0.0-SNAPSHOT ............. SUCCESS [  9.923 s]
[INFO] client 1.0.0-SNAPSHOT ............. SUCCESS [  0.418 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  15.673 s
[INFO] Finished at: 2019-07-22T20:12:22+08:00
[INFO] ------------------------------------------------------------------------
[default] $ /bin/sh -xe /tmp/jenkins2776744439575037216.sh
+ echo 'Stopping SpringBoot Application jtn-techplatform-impl.jar'
Stopping SpringBoot Application jtn-techplatform-impl.jar
++ awk '{print $2}'
++ grep -v grep
++ grep jtn-techplatform-impl.jar
++ ps -ef
+ pid=
+ '[' -n '' ']'
+ cp -f /root/.jenkins/workspace/jtn-techplatform/default/techplatform-impl/target/jtn-techplatform-impl.jar /home/app/jtn-techplatform-impl.jar
+ cd /home/app
+ chmod 777 /home/app/jtn-techplatform-impl.jar
+ BUILD_ID=DONTKILLME
+ nohup java -jar jtn-techplatform-impl.jar
Finished: SUCCESS

 

©️2020 CSDN 皮肤主题: 猿与汪的秘密 设计师: 上身试试 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值