概述
环境准备
- jdk: jdk-8u192-linux-x64.tar 《持续集成环境搭建(2)nexus搭建和使用》中已安装
- tomcat: http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.37/bin/apache-tomcat-8.5.37.tar.gz
- jenkins: https://jenkins.io/download/
http://mirrors.jenkins.io/war-stable/latest/jenkins.war
Jenkins安装
- 添加tomcat用户
[root@localhost ~]# useradd tomcat
[root@localhost ~]# passwd tomcat
- 解压tomcat安装包
[root@localhost ~]# tar -zxvf apache-tomcat-8.5.37.tar.gz -C /usr/local/
- 修改tomcat文件的所有权为tomcat用户
[root@localhost local]# chown -R tomcat:tomcat apache-tomcat-8.5.37/
- 把下载好的jenkins.war放到tomat/webapps目录下
[root@localhost ~]# mv jenkins.war /usr/local/apache-tomcat-8.5.37/webapps/
- 修改jenkins.war的所有权为tomcat用户
[root@localhost ~]# cd /usr/local/apache-tomcat-8.5.37/webapps/
[root@localhost webapps]# chown tomcat:tomcat jenkins.war
- 切换为tomcat用户,启动tomcat
[root@localhost webapps]# su tomcat
[tomcat@localhost webapps]$ ../bin/startup.sh
- 开放防火墙端口8080**
[tomcat@localhost webapps]$ su root
[root@localhost ~]# firewall-cmd --zone=public --add-port=8080/tcp --permanent
[root@localhost ~]# sudo systemctl reload firewalld
- 发现端口被占用
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:309)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:492)
Caused by: org.apache.catalina.LifecycleException: Protocol handler initialization failed
at org.apache.catalina.connector.Connector.initInternal(Connector.java:995)
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)
... 12 more
Caused by: java.net.BindException: 地址已在使用
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:433)
at sun.nio.ch.Net.bind(Net.java:425)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:219)
at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:1087)
at org.apache.tomcat.util.net.AbstractJsseEndpoint.init(AbstractJsseEndpoint.java:265)
at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:581)
at org.apache.coyote.http11.AbstractHttp11Protocol.init(AbstractHttp11Protocol.java:68)
at org.apache.catalina.connector.Connector.initInternal(Connector.java:993)
... 13 more
- 查看8080端口占用的进程
[root@localhost webapps]# ss -lnp|grep 8080
tcp LISTEN 0 1024 127.0.0.1:8080 *:* users:(("bundle",pid=8027,fd=22),("bundle",pid=8025,fd=22),("bundle",pid=8022,fd=22),("bundle",pid=7675,fd=22))
- 查看进程详情,可发现是由于前面安装GitLab引起
[root@localhost webapps]# ps -ef | grep 8027
git 8027 7675 0 22:31 ? 00:00:04 unicorn worker[2] -D -E production -c /var/opt/gitlab/gitlab-rails/etc/unicorn.rb /opt/gitlab/embedded/service/gitlab-rails/config.ru
- 修改tomcat配置文件,把http端口改为9999
[root@localhost webapps]# vi ../conf/server.xml
<Connector port="9999" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
- 开放防火墙端口9999
[root@localhost ~]# firewall-cmd --zone=public --add-port=9999/tcp --permanent
[root@localhost ~]# sudo systemctl reload firewalld
- 打开Jenkins
http://192.168.1.177:9999/jenkins
Jenkins初始化
- 首次打开jenkins提示输入初始化密码 首次打开jenkins
- 按步骤,查看jenkins初始化密码
[root@localhost webapps]# cat /root/.jenkins/secrets/initialAdminPassword
b31e848496224e409c120f15a03fbc07
- 可看到jenkins提示离线状态,这种情况是无法安装插件,是因为插件地址为https缘故 离线
- 进入下面链接,修改插件安装路径,把https改为http
http://192.168.1.177:9999/jenkins/pluginManager/advanced
把https改成http - 重启tomcat
[root@localhost webapps]# ../bin/shutdown.sh
[root@localhost webapps]# ../bin/startup.sh
- 重新进入jenkins,输入初始化密码后,进入选择推荐安装插件 选择推荐安装插件
- 等待插件安装 等待安装插件
- 创建管理员账户,下一步
- 点击Save & Finish
- 点击开始使用
- 重启tomcat
Jenkins配置
首次登录后基础插件安装
系统管理-》插件管理-》Available
在Filter中输入需要安装的插件名称
安装提示 安装sonarqube scaner插件 安装sonarqube scaner插件 安装publish over ssh插件 安装publish over ssh插件 安装状态 安装状态
基础配置
系统管理-》全局工具配置
配置maven
配置maven配置完成记得点击Save
通知邮件配置
系统管理-》系统设置
与SonarQube整合配置
系统管理-》系统设置
系统管理-》全局工具配置
与GitLab整合配置
创建公钥和私钥
[tomcat@localhost apache-tomcat-8.5.37]$ ssh-keygen -t rsa
Enter file in which to save the key (/home/tomcat/.ssh/id_rsa):
Created directory '/home/tomcat/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/tomcat/.ssh/id_rsa.
Your public key has been saved in /home/tomcat/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:wzu+V1AU+PfNDUa9PtXHP90trM9W72lvj7Zmyq1nzjw tomcat@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| oo. . |
| . . . .|
| o . .o|
| . . . +.=|
| S . +.+X|
| o . o+@|
| o . . o+|
| . ....=Eo+|
| oo +%@**|
+----[SHA256]-----+
私钥放置于Jenkins
[tomcat@localhost apache-tomcat-8.5.37]$ more ~/.ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEArytKZhoFFIEfRZcLnGZFmDwu6mwQo3uPxcNLqnDivaahOJA0
e70Cma7qAPXqvGvAS35A1z4HYE+kVoDIECRTMoFxRyz2+FniN6rNkTpT75uUdxHP
KuvtzIIs0KexXCg3etPeIsn4Re3xQA9MRWHsFbSdx0TufZTE/ajF/htOwboKANM9
l4Fx/HzTGSBeys2tP5MIjWPFNI4y5CqPUw8tKg+7i/tepKjZcGYF+4XlVY0OX7bL
Nlq49rTLc6H+wMzg/1g1ejFtc+CRa9m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L1zs51avEZpo4FeFxjXWupxZLc47zzFJycv+vmS5yfi3qtruwu2vIX4D7Q56R
s8V12wKBgBoR33GjTc+b1zmZYnqlcMdgdo0LQPUcFgENoqLGlm2UCSIVEHuTJgh1
z4C6Ooz1H3uWfoST0boQRX0GdwJqVLJSlPSBG10MvH4khHEHfDrQMCtVCeG/qfBF
cKxbP2blB86nofkJsSWb/Bu667bCQv2uuSIGE6utBMyda2Ipi1XQ
-----END RSA PRIVATE KEY-----
凭据-》系统-》全局凭据-》添加凭据
添加凭据
公钥放置于GitLab
[tomcat@localhost apache-tomcat-8.5.37]$ more ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCvK0pmGgUUgR9FlwucZkWYPC7qbBCje4/Fw0uqcOK9pqE4kDR7vQKZruoA9eq8a8BLfkDXPgdgT6RWgMgQJFMyg
XFHLPb4WeI3qs2ROlPvm5R3Ec8q6+3MgizQp7FcKDd6094iyfhF7fFAD0xFYewVtJ3HRO59lMT9qMX+G07BugoA0z2XgXH8fNMZIF7Kza0/kwiNY8U0jjLkKo9TDy
0qD7uL+16kqNlwZgX7heVVjQ5ftss2Wrj2tMtzof7AzOD/WDV6MW1z4JFr2Y55xgmsSfUtUXMgTseneZvd94QRNjuOrBK8sfKRdPiDttVQNkcG6V6nAVUvZ2JwcsJ
ekC3j tomcat@localhost.localdomain
gitlab设置公钥
构建项目
- 新建一个任务,选择构建一个maven项目! 新建任务
- 点击OK后,填写项目的General信息 General
- 选择Source Code Management, 选择Git Source Code Management
输入项目的git地址和选择相应的私钥。
如无红色提示,则说明git能访问,配置正确。 Source Code Management- Build Triggers & Build Environment 去掉所有勾选,该例子暂不用 Build Triggers & Build Environment
- 配置构建命令和构建后步骤 Build&Post Steps
cp target/*.jar /home/tomcat/jenkins
cd /home/tomcat/jenkins
BUILD_ID= java -jar castle-boot-0.0.1-SNAPSHOT.jar > castle.log 2>&1 &
-
配置完成后,点击Save,保存并返回到构建项目页面
- 选择立即构建,在Build History会出现一个构建的进程#1 构建
- 点击进入#1,查看控制台输出 控制台输出
- 等待构建完成 构建完成提示
-
查看进程是否启动
[tomcat@localhost jenkins]$ ps aux | grep castle
tomcat 109213 128 7.8 3259248 226780 pts/0 Sl 08:27 0:10 java -jar castle-boot-0.0.1-SNAPSHOT.jar
tomcat 109258 0.0 0.0 112724 988 pts/0 S+ 08:27 0:00 grep --color=auto castle
提交到SonarQube进行代码质量检查
- 配置项目的Pre Steps Pre Steps
sonar.projectKey=castle-boot
sonar.projectName=CastleBoot
sonar.projectVersion=1.0
sonar.sourceEncoding=UTF-8
sonar.modules=java-module
java-module.sonar.projectName=MavenDemo
java-module.sonar.language=java
java-module.sonar.sources=src
java-module.sonar.projectBaseDir=.
java-module.sonar.java.binaries=target/
- 再次构建,构建完成后,访问SonarQube,会多了一个CastleBoot项目 SonarQube
代码提交到GitLab上,自动触发构建
- 安装gitlab插件 安装gitlab插件
- 进入maven-demo项目的配置 BuildTriggers BuildTriggers
- 到GitLab上进行相应的配置,使用管理员账户root,进行以下设置 GitLab系统配置
- 进入对应项目的配置 GitLab项目配置
URL和Token可在jenkins的项目配置中找到 URL Token
- 填写完URL和Token后,点击 add webhook,可看到配置已添加成功 webhook
-
修改代码,并提交,测试jenkins是否会自动构建
创建Pipeline
- 创建任务,选择流水线 流水线
- 安装Blue Ocean和Blue Ocean Pipeline Editor 插件安装
- 打开BlueOcean
- 点击右上角的创建流水线 BlueOcean
- 选择Git,输入git的地址 流水线创建
- 在GitLab上添加该公钥 image.png
- 跳转到Blue Ocean Pipeline Editor Blue Ocean Pipeline Editor
- 点击 + ,搜索Git,输入Url和Branch,然后点击保存 Blue Ocean Pipeline Editor
- 选择保存且运行 Save Pipeline
- 首次运行由于超时原因可能会出错,如出错,可点击重运行 image.png
- 选择能正确运行的任务,点击编辑
- 修改流程名字
- 添加stage:maven build,添加step:选择Shell Script
- 添加stage: deploy
cp target/*.jar /home/tomcat/jenkins
cd /home/tomcat/jenkins
nohup java -jar castle-boot-0.0.1-SNAPSHOT.jar > castle.log 2>&1 &
- 添加stage: nothing(测试并行Stage)
- 保存后,选择 Save & run image.png
- 跳转到构建列表 image.png
- 选择构建中的任务,查看详情
- 构建完毕
- 该构建,会在项目的git仓库增加Jenkinsfile文件,可以发现,该文件内容就是Pipeline的脚本
网友评论