目录
Jenkins
官网 https://jenkins.io
Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,指在提供一个开放易用的软件平台,使软件的持续集成变成可能。
01. 安装准备
主机 | ip | 内存 | 硬盘 |
---|---|---|---|
Jenkins | 10.0.0.201 | 2G | 50G+ |
nexus | 10.0.0.202 | 2G | 50G+ |
02 .安装Jdk和Jenkins
#上传JDK和Jenkins安装包,使用rpm ‐ivh进行安装,安装完JDK运行Java测试是否安装成功
[root@jenkins ~]# ll
-rw-r--r-- 1 root root 170023183 2018-08-14 11:05 jdk-8u181-linux-x64.rpm
-rw-r--r-- 1 root root 74141787 2018-08-13 20:23 jenkins-2.99-1.1.noarch.rpm
[root@jenkins ~]# rpm -ivh jdk-8u181-linux-x64.rpm
warning: jdk-8u181-linux-x64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03:
NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:jdk1.8-2000:1.8.0_181-fcs ################################# [100%]
Unpacking JAR files...
tools.jar...
plugin.jar...
javaws.jar...
deploy.jar...
rt.jar...
jsse.jar...
charsets.jar...
localedata.jar...
[root@jenkins ~]# rpm -ivh jenkins-2.99-1.1.noarch.rpm
warning: jenkins-2.99-1.1.noarch.rpm: Header V4 DSA/SHA1 Signature, key ID d50582e6:
NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:jenkins-2.99-1.1 ################################# [100%]
03 .配置Jenkins
#启动用户修改为root
[root@jenkins ~]# vim /etc/sysconfig/jenkins
JENKINS_USER="root"
#启动
[root@jenkins ~]# systemctl start jenkins
#查看端口
[root@jenkins ~]# netstat -lntp | grep 8080
tcp6 0 0 :::8080 :::* LISTEN 8504/java
#查看进程
[root@jenkins ~]# ps aux |grep jenkins
root 8504 8.8 11.6 2618104 235556 ? Ssl 15:29 0:25 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -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
#访问页面进行配置
http://10.0.0.201:8080
#查看密码
[root@jenkins ~]# cat /var/lib/jenkins/secrets/initialAdminPassworde8e69c5646cc4f3b88315fb20156ce6c
修改密码
04. 插件安装
插件安装(跳过安装插件,直接上传插件到目录)
1.自动安装可选插件
2.手动下载插件上传安装
3.插件放入插件目录
[root@jenkins ~]# cd /var/lib/jenkins/
[root@jenkins jenkins]# ll #jobs为每次构建后构建的结果目录,plugins为插件目录
总用量 36
‐rw‐‐‐‐‐‐‐ 1 root root 1822 8月 26 00:35 config.xml
‐rw‐‐‐‐‐‐‐ 1 root root 156 8月 26 00:31 hudson.model.UpdateCenter.xml
‐rw‐‐‐‐‐‐‐ 1 root root 1712 8月 26 00:32 identity.key.enc
‐rw‐‐‐‐‐‐‐ 1 root root 94 8月 26 00:32 jenkins.CLI.xml
‐rw‐r‐‐r‐‐ 1 root root 4 8月 26 00:35 jenkins.install.InstallUtil.lastExecVersion
‐rw‐r‐‐r‐‐ 1 root root 4 8月 26 00:35 jenkins.install.UpgradeWizard.state
drwxr‐xr‐x 2 root root 6 8月 26 00:31 jobs
drwxr‐xr‐x 3 root root 18 8月 26 00:32 logs
‐rw‐‐‐‐‐‐‐ 1 root root 907 8月 26 00:32 nodeMonitors.xml
drwxr‐xr‐x 2 root root 6 8月 26 00:32 nodes
drwxr‐xr‐x 2 root root 6 8月 26 00:31 plugins
‐rw‐‐‐‐‐‐‐ 1 root root 64 8月 26 00:31 secret.key
‐rw‐r‐‐r‐‐ 1 root root 0 8月 26 00:31 secret.key.not‐so‐secret
drwx‐‐‐‐‐‐ 4 root root 4096 8月 26 00:32 secrets
drwxr‐xr‐x 2 root root 23 8月 26 00:32 userContent
drwxr‐xr‐x 3 root root 18 8月 26 00:34 users
#上传插件包解压到plugins
[root@jenkins jenkins]# cd plugins/
[root@jenkins plugins]# ll
total 160580
-rw-r--r-- 1 root root 164431230 2018-08-14 21:00 plugins.tar.gz
[root@jenkins plugins]# tar xf plugins.tar.gz
[root@jenkins plugins]# rm -f plugins.tar.gz
[root@jenkins plugins]# mv plugins/* ./
[root@jenkins plugins]# rm -rf plugins/
#重启生效
[root@jenkins plugins]# systemctl restart jenkins
#4.jenkins主要的目录
/usr/lib/jenkins/: #jenkins安装目录,WAR包会放在这里
/etc/sysconfig/jenkins: #jenkins配置文件,"端口","JENKINS_HOME"等都可以在这里配置
/var/lib/jenkins/: #默认的JENKINS_HOME
/var/log/jenkins/jenkins.log: #Jenkins日志文件
05. 创建项目
进行构建
进入控制台
[root@jenkins plugins]# ll /var/lib/jenkins/workspace/freestyle-jobtotal 0
[root@jenkins plugins]# ll /var/lib/jenkins/workspace/freestyle-job
total 0
-rw-r--r-- 1 root root 0 2019-11-19 16:19 test.txt
06. Jenkins获取Git源代码
#这里我们有码云导入一个HTML页面的监控平台到gitlab仓库,打开码云,找到一个大转盘项目,将其代码路径进行复制
在Gitlab上面新创建一个项目仓库。将源代码导入进去。
dev用户端配置从git获取代码。
[root@dev ~]# git clone git@10.0.0.100:OPS/dzp.git
Cloning into 'dzp'...
remote: Counting objects: 19, done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 19 (delta 2), reused 0 (delta 0)
Receiving objects: 100% (19/19), 104.36 KiB | 0 bytes/s, done.
Resolving deltas: 100% (2/2), done.
[root@dev ~]# ll
drwxr-xr-x 6 root root 87 Nov 19 20:41 dzp
drwxr-xr-x 3 root root 52 Nov 17 17:21 git_test
[root@dev ~]# cd dzp/
[root@dev ~/dzp]# ll
total 8
drwxr-xr-x 2 root root 25 Nov 19 20:41 css
drwxr-xr-x 2 root root 84 Nov 19 20:41 img
drwxr-xr-x 2 root root 41 Nov 19 20:41 js
-rw-r--r-- 1 root root 2170 Nov 19 20:41 lottery.html
-rw-r--r-- 1 root root 113 Nov 19 20:41 README.md
#dev用户修改了源代码
[root@dev ~/dzp]# vim lottery.html
[root@dev ~/dzp]# git commit -am "modify html"
[master 1e2125e] modify html
1 file changed, 5 insertions(+), 5 deletions(-)
[root@dev ~/dzp]# git push -u origin master
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 365 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To git@10.0.0.100:OPS/dzp.git
3935eb7..1e2125e master -> master
Branch master set up to track remote branch master from origin.
#Jenkins配置从Git获取代码,由于我们dev用户不是配置在Jenkins上,所以需认证即可下载代码。进行面认证方法,需要配置deploy key
[root@jenkins ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:1z+H0mc9hUwOWiO3mFOGswuOW9jNr2bmOwRE50eECQU root@jenkins
The key's randomart image is:
+---[RSA 2048]----+
| .E+o+o |
| .ooo |
| . = O . |
| . # B . |
| S.B o + .|
| = =.o o .o|
| o +.+ . =.=|
| o =. . =.|
| . =++. |
+----[SHA256]-----+
[root@jenkins ~]# cat .ssh/
id_rsa id_rsa.pub known_hosts
[root@jenkins ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCY3WrgTCmZeapjV9Tx6GmmHX+SVigxHnz37f8iUUjAexmR2/gSAjvsY3ez5iITmamf2I62+5n+gx9f1OPXUsUAzOApD6R8zHmvzQ/yheDO1y5XHcmRtklN1HpOq2g0PChrRjrr4QcXITKafU30OjTu4Fx3vndxqD/3RpHD3IVSZGAsiJC6T+C/PYW8YfeLNEAA7O3QKskjuSkoH2PZyF7qQgwjxfudno1g3qLQkmB+CjFFwgj0vkam/W4OwUwapC1O591CU7+VmSWL/z8uMTpSV+FQkyH04qS/HSs27pfpAI0wpBB/gBxx+wU8FCUh++2SWpuJR8/TPqkS8YKhfl+p root@jenkins #复制该串代码
#Jenkins配置从Git获取代码,配置好后保存
07. 立即构建获取源代码
#Jenkins服务器查看代码
[root@jenkins ~]# ll /var/lib/jenkins/workspace/freestyle-job/
total 8
drwxr-xr-x 2 root root 25 2019-11-19 21:12 css
drwxr-xr-x 2 root root 84 2019-11-19 21:12 img
drwxr-xr-x 2 root root 41 2019-11-19 21:12 js
-rw-r--r-- 1 root root 2205 2019-11-19 21:12 lottery.html
-rw-r--r-- 1 root root 113 2019-11-19 21:12 README.md
08.Jenkins代码推送到Web
#写一个脚本把从git仓库里获取的代码上传到web服务器站点目录下
[root@jenkins ~]# mkdir -p /server/script
[root@jenkins ~]# cd /server/script
[root@jenkins script]# vim deploy.sh
[root@jenkins script]# cat deploy.sh
#!/bin/sh
Date=$(date +%s)
Code_Dir=/var/lib/jenkins/workspace/freestyle-job
Web_Dir=/code
Ip=10.0.0.202
Code_Tar() {
cd $Code_Dir && tar zcf /opt/web_${Date}.tar.gz ./*
}
Scp_Code_Web() {
scp /opt/web_${Date}.tar.gz root@${Ip}:/opt
}
Code_Tar_Xf() {
ssh root@$Ip " cd /opt && mkdir web_$Date && tar xf web_${Date}.tar.gz -C web_$Date "
}
Ln_Html() {
ssh root@$Ip " rm -rf $Web_Dir && ln -s /opt/web_$Date /code "
}
Code_Tar;
Scp_Code_Web;
Code_Tar_Xf;
Ln_Html
#分发公钥
[root@jenkins script]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.202
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '10.0.0.202 (10.0.0.202)' can't be established.
ECDSA key fingerprint is SHA256:K8NapPzlTxhCMXC/bRFTtI9mdwr63FH4Wu7psrXXqBs.
ECDSA key fingerprint is MD5:73:9f:67:f1:5d:39:10:3d:b2:be:f7:c1:66:aa:00:6e.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.0.0.202's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@10.0.0.202'"and check to make sure that only the key(s) you wanted were added.
[root@jenkins script]# ssh root@10.0.0.202
Last login: Tue Nov 19 15:10:55 2019 from 10.0.0.1
[root@nexus ~]# logout
Connection to 10.0.0.202 closed.
Jenkins上面添加脚本进行构建
查看网站是否更新成功
09. 配置自动触发构建
#需要设置安全令牌Secret token
Gitlab上面操作
10. 测试是否触发
#dev用户修改源代码后进行推送测试是否自动触发
[root@dev ~/dzp]# vim lottery.html
[root@dev ~/dzp]# git commit -am "modify html test"
[master d8849aa] modify html test
1 file changed, 3 insertions(+), 3 deletions(-)
[root@dev ~/dzp]# git push -u origin master
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 303 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To git@10.0.0.100:OPS/dzp.git
1e2125e..d8849aa master -> master
Branch master set up to track remote branch master from origin.
11. 返回构建状态
#Jenkins配置Jenkins返回构建状态到Gitlab
#先获得gitlab的token
将获得的token值进行复制备用
进行测试
添加构建后操作
检查结果