Jenkins介绍和安装使用

一、什么是持续集成?

(1)Continuous integration(CI)

持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员至少集成一次,也就意味着每天可能会发生多次集成。每次集成都通过自动化的构建(包括编译,发布,自动化测试)来验证,从而尽快地发现集成错误。许多团队发现这个过程可以大大减少集成的问题,让团队能够更快的开发内聚的软件。

(2)没有持续集成

项目做模块集成的时候,发现很多借口都不通==>浪费大量时间
需要手动去编译打包最新的代码==>构建过程不透明
发布代码,上线,基本靠手工==>脚本乱飞

(3)持续集成最佳实践:

维护一个单一的代码库
使构建自动化
执行测试是构建的一部分
集成日志及历史记录
使用统一的依赖包管理库
每天至少集成一次

(4)jenkins介绍

Jenkins只是一个平台,真正运作的都是插件。这就是jenkins流行的原因,因为jenkins什么插件都有
Hudson是Jenkins的前身,是基于Java开发的一种持续集成工具,用于监控程序重复的工作,Hudson后来被收购,成为商业版。后来创始人又写了一个jenkins,jenkins在功能上远远超过hudson
Jenkins官网:https://jenkins.io/
Jenkins下载:http://updates.jenkins-ci.org/
jenkins的全部镜像:http://mirrors.jenkins-ci.org/status.html

(5)部署代码上线流程:

1.代码获取(直接了拉取)
2.编译      (可选)
3.配置文件放进去
4.打包
5.scp到目标服务器
6.将目标服务器移除集群
7.解压并放置到Webroot
8.Scp 差异文件
9.重启      (可选)
10.测试
11.加入集群

二、安装使用

yum的repo中默认没有Jenkins,需要先将Jenkins存储库添加到yum repos,执行下面的命令:
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo

然后执行下面的命令:

rpm –import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

然后 安装Jenkins

yum install -y jenkins 默认安装最新的

或者
直接下载到本地:wget https://pkg.jenkins.io/redhat/jenkins-2.160-1.1.noarch.rpm
然后通过命令安装成功:rpm -ih jenkins-2.160-1.1.noarch.rpm

jenkins安装成功后,默认的用户是jenkins,端口是8080,为了防止冲突,并且给用户赋权限,我们修改用户名和端口。

输入命令,进入jenkins配置文件: vi /etc/sysconfig/jenkins

修改用户名root,端口,然后保存,退出

1
2
3
4
5
6
7
8
9
10
11
12
安装jenkins有两种方式,tomcat方式部署和java部署启动。

tomcat方式部署:
1、首先安装tomcat和JAVA;
2、官网下载下来的jenkins.war文件放入tomcat下的webapps目录下,进入tomcat的/bin目录下,启动tomcat即启动jenkins。
3、启动jenkins时,会自动在webapps目录下建立jenkins目录,访问地址为:http://localhost:8080/jenkins
4、如果启动时报错:Caused by:java.awt.AWTError: Can't connect to X11 window server using ':0' as the value of the DISPLAY varible...
解决:[jenkins@osb30 bin]$ vim apache-tomcat-8.0.30/bin/catalina.sh,修改为:JAVA_OPTS="-Xms1024m -Xmx1024m -Djava.awt.headless=true"

java部署启动jenkins:
1、切换到jenkins.war存放的目录,输入如下命令:java -jar jenkins.war,可以修改启动端口java -jar jenkins.war --httpPort=8000
2、然后在浏览器http://localhost:8080即可访问

启动jenkins

1
2
3
4
5
6
7
8
9
10
service jenkins start
启动报Starting Jenkins bash: /usr/bin/java: No such file or directory
修改Jenkins启动配置文件,指定java安装路径。
vim /etc/init.d/jenkins
在candidates中第一行添加java路径,如下:
candidates="
/usr/local/jdk8/bin/java
启动成功,浏览器访问http://192.168.1.110:8088

初始密码在:/var/lib/jenkins/secrets/initialAdminPassword

登录成功后显示

20200413162014

选择”Install suggested plugins”安装默认的插件,下面Jenkins就会自己去下载相关的插件进行安装。安装完成后配置账号,配置好账号之后hu/123456,进入Jenkins。

接下来在jenkins中配置jdk、svn、maven、git,并且构建web项目和spring boot项目。

三、安装Maven

1
2
3
4
5
6
7
8
9
10
11
12
官网下载安装包apache-maven-3.6.0-bin.tar.gz到Linux上
解压[root@localhost local]# tar -zxvf apache-maven-3.6.0-bin.tar.gz
// 配置环境变量到/etc/profile
export PATH=$PATH:cbin
//查看版本
[root@localhost local]# mvn --version
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-25T02:41:47+08:00)
Maven home: /usr/local/apache-maven-3.6.0
Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /usr/local/jdk8/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "2.6.32-431.el6.x86_64", arch: "amd64", family: "unix"
修改一下源:

四、安装git

安装过程 略

有的插件安装失败,通过换源来解决或者手动下载:jenkins->系统管理->管理插件->高级,如
https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.jsonss

五、使用Jenkins

使用构建

配置DK、MAVEN、Git为实际对应的安装目录

20200413161952

安装以下插件:

1、 Maven Integration plugin
2、 Subversion Plugin
3、 Checkstyle Plugin
4、 Findbugs Plugin
5、 PMD Plugin
6、 Warnings Plugin
7、 Maven Info Plugin
8、 Maven Repository Scheduled Cleanup Plugin
9、 Unleash Maven Plugin
10、 Maven Invoker plugin

Subversion Plug-in ## 版本管理 SVN 的插件
Git plugin ## 版本管理 GIT 的插件
Maven Integration plugin ## 项目构建 Maven 的插件
Gradle Plugin ## 项目构建 Gradle 的插件

无需重启 Jenkins 插件即生效。如遇失败可重试或离线安装。

一般情况下,常使用到如下这些插件:

FindBugs Plug-in: 是一个静态分析工具,它检查类或者 JAR 文件,将字节码与一组缺陷模式进行对        比以发现可能的问题。
Checkstyle Plug-in:是一个静态分析工具,检查Java程序代码。
Deploy to container Plugin:用于构建项目后,自动发布war包重新部署的插件
SSH Plugin:这个插件使用 SSH 协议执行远程 shell 命令。
Multijob Plugin:这个插件是一个将多个项目连接在一起的插件。

1、构建Maven项目

20200413162214

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
由用户 hu 启动
Running as SYSTEM
构建中 在工作空间 /var/lib/jenkins/workspace/maven-jenkins-test 中
using credential dd5c879e-a6f2-4902-95fc-81219b15096f
Cloning the remote Git repository
Cloning repository https://github.com/huingsn/demo
> git init /var/lib/jenkins/workspace/maven-jenkins-test # timeout=10
Fetching upstream changes from https://github.com/huingsn/demo
> git --version # timeout=10
using GIT_ASKPASS to set credentials
> git fetch --tags --progress https://github.com/huingsn/demo +refs/heads/*:refs/remotes/origin/*
> git config remote.origin.url https://github.com/huingsn/demo # timeout=10
> git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
> git config remote.origin.url https://github.com/huingsn/demo # timeout=10
Fetching upstream changes from https://github.com/huingsn/demo
using GIT_ASKPASS to set credentials
> git fetch --tags --progress https://github.com/huingsn/demo +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
> git rev-parse origin/master^{commit} # timeout=10
ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.
Finished: FAILURE

原因分析:jenkins无法准确识别revision

操作方法:

20200413162306

再次构建,成功!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
由用户 hu 启动
Running as SYSTEM
构建中 在工作空间 /var/lib/jenkins/workspace/dsafsdfdsafas 中
using credential dd5c879e-a6f2-4902-95fc-81219b15096f
Cloning the remote Git repository
Cloning repository https://github.com/huingsn/demo
> git init /var/lib/jenkins/workspace/dsafsdfdsafas # timeout=10
Fetching upstream changes from https://github.com/huingsn/demo
> git --version # timeout=10
using GIT_ASKPASS to set credentials
> git fetch --tags --progress https://github.com/huingsn/demo +refs/heads/*:refs/remotes/origin/*
> git config remote.origin.url https://github.com/huingsn/demo # timeout=10
> git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
> git config remote.origin.url https://github.com/huingsn/demo # timeout=10
Fetching upstream changes from https://github.com/huingsn/demo
using GIT_ASKPASS to set credentials
> git fetch --tags --progress https://github.com/huingsn/demo +refs/heads/*:refs/remotes/origin/*
Seen branch in repository origin/github.com/huingsn/demo
Seen 1 remote branch
> git show-ref --tags -d # timeout=10
Checking out Revision c8688fb590873bd7b77f3537d11640a5139c0da4 (origin/github.com/huingsn/demo)
> git config core.sparsecheckout # timeout=10
> git checkout -f c8688fb590873bd7b77f3537d11640a5139c0da4
Commit message: "dfsdfsdfs"
First time build. Skipping changelog.
Parsing POMs
Discovered a new module com.hu:maven-jenkins-test maven-jenkins-test Maven Webapp
Modules changed, recalculating dependency graph
Established TCP socket on 37646
[dsafsdfdsafas] $ /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/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 37646
<===[JENKINS REMOTING CAPACITY]===>channel started
Executing Maven: -B -f /var/lib/jenkins/workspace/dsafsdfdsafas/pom.xml install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-jenkins-test Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/3.0.2/maven-resources-plugin-3.0.2.pom
//省略下载包过程
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.15/plexus-utils-3.0.15.jar (234 KB at 17.8 KB/sec)
[INFO] Installing /var/lib/jenkins/workspace/dsafsdfdsafas/target/maven-jenkins-test.war to /root/.m2/repository/com/hu/maven-jenkins-test/1.0-SNAPSHOT/maven-jenkins-test-1.0-SNAPSHOT.war
[INFO] Installing /var/lib/jenkins/workspace/dsafsdfdsafas/pom.xml to /root/.m2/repository/com/hu/maven-jenkins-test/1.0-SNAPSHOT/maven-jenkins-test-1.0-SNAPSHOT.pom

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6:17.736s
[INFO] Finished at: Sat Apr 06 21:22:19 CST 2019
[INFO] Final Memory: 14M/79M
[INFO] ------------------------------------------------------------------------
Waiting for Jenkins to finish collecting data
[JENKINS] Archiving /var/lib/jenkins/workspace/dsafsdfdsafas/pom.xml to com.hu/maven-jenkins-test/1.0-SNAPSHOT/maven-jenkins-test-1.0-SNAPSHOT.pom
[JENKINS] Archiving /var/lib/jenkins/workspace/dsafsdfdsafas/target/maven-jenkins-test.war to com.hu/maven-jenkins-test/1.0-SNAPSHOT/maven-jenkins-test-1.0-SNAPSHOT.war
channel stopped

Finished: SUCCESS

构建触发器

指定的项目完成构建后,触发此项目的构建。

Poll SCM

1.首先格式为:* * * * *(五个星);

2.第一个*表示分钟,取值0~59

第二个*表示小时,取值0~23
第三个*表示一个月的第几天,取值1~31
第四个*表示第几月,取值1~12
第五个*表示一周中的第几天,取值0~7,其中0和7代表的都是周日

3.使用举例:

每隔10分钟构建一次:H/5 * * * *
每隔1小时构建一次: H/1* *
每月30号构建一次:   H 30 * *
Build periodically:此选项仅仅通知Jenkins按指定的频率对项目进行构建,而不管SCM是否有变化。如果想在这个Job中运行一些测试用例的话,它就很有帮助。

20200413162444

构建

  这部分主要是配置构建的相关内容,用于定时触发构建或者手动执行构建的时候,对代码检验、编译时进行的操作。构建概念到处可查到,形象来说,构建就是要把代码从某个地方拷贝过来,编译,再拷贝到某个地方去等等操作,当然不仅与此,但是主要用来干这个。

因为我的项目是用ant脚本实现的编译和打包,所以我选择的是Invoke Ant,Ant Version选择我Ant配置的那个名字(这里可以参见3.4.2),注意不要选择default喔,那个选择了没有用。

构建后操作

  用于定义当前项目构建完之后的一些操作,比如构建完之后将checkstyle结果输出到指定日志文件,重新发布项目,去执行其他项目构建等。

20200413162513

构建后发布项目

注意,首先必须安装好Deploy Plugin插件,然后在tomcat的conf目录配置tomcat-users.xml文件,如我这里配置的是manager, 在节点里添加如下内容:

20200413162537

WAR/EAR files:是war包的相对路径(相对于工作区路径,即在工作区中war包的相对路径.)如我的maven执行完成之后会在工作区的target目录下生成项目.war,所以这里我的路径就写target\项目.war.
Context path:访问时需要输入的内容http://192.168.1.112:8000/maven-jenkins-test,如果为空,默认是war包的名字。
Container:选择你的web容器,如tomca 7.x
Manager user name:填入tomcat-users.xml配置的username内容
Manager password:填入tomcat-users.xml配置的password内容

Tomcat URL:http://192.168.1.112:8000

Deploy on failure:构建失败依然部署,一般不选择

  注意:虽然这种部署方法可能会导致tomcat加载时出现卡死的现象。但是也是最简单的部署方式。如果卡死了重启下就好了,将tomcat的java内存参数调高可以解决这个问题。

上面写入错入,Tomcat端口是8080,开始部署:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
Running as SYSTEM
[EnvInject] - Loading node environment variables.
构建中 在工作空间 /var/lib/jenkins/workspace/first 中
using credential dd5c879e-a6f2-4902-95fc-81219b15096f
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://github.com/huingsn/demo # timeout=10
Fetching upstream changes from https://github.com/huingsn/demo
> git --version # timeout=10
using GIT_ASKPASS to set credentials
> git fetch --tags --progress https://github.com/huingsn/demo +refs/heads/*:refs/remotes/origin/*
Seen branch in repository origin/github.com/huingsn/demo
Seen 1 remote branch
> git show-ref --tags -d # timeout=10
Checking out Revision c8688fb590873bd7b77f3537d11640a5139c0da4 (origin/github.com/huingsn/demo)
> git config core.sparsecheckout # timeout=10
> git checkout -f c8688fb590873bd7b77f3537d11640a5139c0da4
Commit message: "dfsdfsdfs"
> git rev-list --no-walk c8688fb590873bd7b77f3537d11640a5139c0da4 # timeout=10
Parsing POMs
Established TCP socket on 36677
[first] $ /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/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 36677
<===[JENKINS REMOTING CAPACITY]===>channel started

Executing Maven: -B -f /var/lib/jenkins/workspace/first/pom.xml install

[INFO] Scanning for projects...

[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-jenkins-test Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ maven-jenkins-test ---

[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /var/lib/jenkins/workspace/first/src/main/resources

[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ maven-jenkins-test ---

[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ maven-jenkins-test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /var/lib/jenkins/workspace/first/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ maven-jenkins-test ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ maven-jenkins-test ---

[INFO] No tests to run.
[JENKINS] Recording test results
[WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.BuildInfoRecorder$1; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/
[INFO]
[INFO] --- maven-war-plugin:3.2.2:war (default-war) @ maven-jenkins-test ---

[INFO] Packaging webapp
[INFO] Assembling webapp [maven-jenkins-test] in [/var/lib/jenkins/workspace/first/target/maven-jenkins-test]
[INFO] Processing war project
[INFO] Copying webapp resources [/var/lib/jenkins/workspace/first/src/main/webapp]
[INFO] Webapp assembled in [75 msecs]
[INFO] Building war: /var/lib/jenkins/workspace/first/target/maven-jenkins-test.war

[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ maven-jenkins-test ---

[INFO] Installing /var/lib/jenkins/workspace/first/target/maven-jenkins-test.war to /root/.m2/repository/com/hu/maven-jenkins-test/1.0-SNAPSHOT/maven-jenkins-test-1.0-SNAPSHOT.war
[INFO] Installing /var/lib/jenkins/workspace/first/pom.xml to /root/.m2/repository/com/hu/maven-jenkins-test/1.0-SNAPSHOT/maven-jenkins-test-1.0-SNAPSHOT.pom

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.804s
[INFO] Finished at: Sat Apr 06 23:06:26 CST 2019
[INFO] Final Memory: 13M/98M
[INFO] ------------------------------------------------------------------------
Waiting for Jenkins to finish collecting data
[JENKINS] Archiving /var/lib/jenkins/workspace/first/pom.xml to com.hu/maven-jenkins-test/1.0-SNAPSHOT/maven-jenkins-test-1.0-SNAPSHOT.pom
[JENKINS] Archiving /var/lib/jenkins/workspace/first/target/maven-jenkins-test.war to com.hu/maven-jenkins-test/1.0-SNAPSHOT/maven-jenkins-test-1.0-SNAPSHOT.war
channel stopped

Deploying /var/lib/jenkins/workspace/first/target/maven-jenkins-test.war to container Tomcat 7.x Remote with context
[/var/lib/jenkins/workspace/first/target/maven-jenkins-test.war] is not deployed. Doing a fresh deployment.
Deploying [/var/lib/jenkins/workspace/first/target/maven-jenkins-test.war]

Finished: SUCCESS

成功部署到Tomcat,http://192.168.1.112:8080/maven-jenkins-test/成功访问。

好了!到此一个项目的获取源码,打包,远程部署