1. jenkins基本介紹與使用
1.1 jenkins介紹
官網 https://jenkins.io
Jenkins是一個開源的、可擴展的持續集成、交付、部署(軟件/代碼的編譯、打包、部署)基於web界面的平台。
Jenkins是一個工具集,提供了各種各樣的插件
比如獲取git上最新的代碼
比如可以幫你編譯源代碼
比如可以調用自定義的shell腳本遠程執行命令
官方文檔 https://jenkins.io/doc/
1.2 jenkins安裝方式
(1)docker
(2)windows
(3)linux
(4)war
(5)yum
1.3 jenkins下載
2. 安裝jenkins
2.1 安裝java依賴環境
[root@jenkins ~]# yum -y install java
2.2 安裝jenkins並啟動
[root@jenkins ~]# yum -y localinstall jenkins-2.176.1-1.1.noarch.rpm
[root@jenkins ~]# systemctl start jenkins && systemctl enable jenkins
jenkins.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig jenkins on
[root@jenkins ~]# echo $?
0
[root@jenkins ~]# ps -ef|grep [j]enkins
jenkins 7031 1 64 01:40 ? 00:01:02 /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 # jenkins監聽8080端口
2.3 jenkins目錄說明
[root@jenkins ~]# rpm -ql jenkins
/etc/init.d/jenkins # jenkins啟動腳本
/etc/logrotate.d/jenkins # jenkins日志輪轉腳本
/etc/sysconfig/jenkins # jenkins 配置文件
/usr/lib/jenkins # jenkins主程序包(war包),升級的時候替換一下這個包就行了
/usr/lib/jenkins/jenkins.war # jenkins主程序包(war包),升級的時候替換一下這個包就行了
/usr/sbin/rcjenkins # /etc/init.d/jenkins的軟連接文件,也是一個啟動文件
/var/cache/jenkins # jenkins緩存目錄
/var/lib/jenkins # jenkins站點目錄
/var/log/jenkins # jenkins日志目錄
2.4 配置客戶端域名解析
# 在hosts文件中添加如下
192.168.1.62 jenkins.xts.com
2.5 訪問jenkins並配置
java項目啟動都比較慢,出現下圖,只要日志沒有報錯,請耐心等待(建議多給點內存)
啟動完畢
基本配置
注意:
(1)如果在安裝插件界面,沒有准備好拆件,可以使用安裝推薦的插件
(2)如果提前准備好了插件,選擇右上角的跳過插件安裝,然后更改admin的默認密碼
(3)離線狀態:連不上插件地址導致,選擇跳過插件安裝,然后會直接進入jenkins,這個時候第一時間更改admin的默認密碼
更改admin默認密碼
3. 安裝jenkisn插件與漢化
安裝插件注意事項
(1)如何對插件下載進行加速
(2)如何導入一個.hpi的插件
(3)如何批量導入備份過的插件
3.1 如何對插件下載進行加速
默認插件所有下載地址都是國外地址,這里只需要替換一下成國內地址即可。
步驟如下:
3.2 如何導入一個.hpi的插件
3.3 如何批量導入備份過的插件
[root@jenkins ~]# ls /var/lib/jenkins/plugins/ # 插件的安裝目錄
[root@jenkins ~]#
# 解壓准備好的插件到插件的安裝目錄
[root@jenkins ~]# rm -rf /var/lib/jenkins/plugins # 這里之所以要刪除plugins,是因為解壓出來的包也叫plugins,實際工作中,不一定是相同的名稱,建議操作的時候,先備份
[root@jenkins ~]# tar xf jenkins_2.176_plugins.tar.gz -C //var/lib/jenkins/
[root@jenkins ~]# ll /var/lib/jenkins/plugins/|wc -l
247
# 授權
[root@jenkins ~]# chown -R jenkins.jenkins /var/lib/jenkins/plugins/
# 重啟jenkins
[root@jenkins ~]# systemctl restart jenkins.service
瀏覽器訪問
注意:不要隨便升級插件,防止升級插件的同時把jenkins版本也給升級了,這樣就會導致其他原版本的無法使用升級后jenkins的插件
隱藏可忽略的警告
4. 創建jenkinsjob任務
4.1 實現jenkins+shell的集成
構建文件沒有寫絕對路徑時,生成的文件所在位置
4.2 實現jenkins+gitlab的集成
4.2.1 准備一個項目
monitor_html代碼包下載點我
提取碼:ohjg
[root@gitlab ~]# tar xf monitor_html.tar.gz
[root@gitlab ~]# ls
anaconda-ks.cfg gitlab-12-0-stable-zh gitlab-ce-12.0.3-ce.0.el7.x86_64.rpm monitor_html.tar.gz
demo gitlab-12-0-stable-zh.tar.gz monitor
[root@gitlab ~]# cd monitor/
[root@gitlab ~/monitor]# ls
404.html content-widgets.html fonts index.html media QHME.iml
alerts.html css form-components.html js media.html readme.md
assets deviceManager.html form-elements.html keyInfo.html messages.html real-time.html
buttons.html dianfei.html form-examples.html labels.html mstp_105_SuperAdmin.iml sa.html
calendar.html efficiencyAnalysis.html form-validation.html LICENSE mstp_map.html tables.html
charts.html energy_consumption.html images-icons.html list-view.html other-components.html typography.html
components.html file-manager.html img login.html profile-page.html userMng.html
4.2.2 前往gitlab創建一個倉庫
命令行操作
[root@gitlab ~/monitor]# git remote -v
origin https://gitee.com/kangjie1209/monitor.git (fetch)
origin https://gitee.com/kangjie1209/monitor.git (push)
# 干掉原有的遠程倉庫
[root@gitlab ~/monitor]# git remote remove origin
# 添加新的遠程倉庫(命令在gitlab頁面“推送現有文件夾”中)
[root@gitlab ~/monitor]# git remote add origin git@gitlab.xts.com:xts/monitor.git
4.2.3 本地倉庫提交項目到gitlab倉庫
[root@gitlab ~/monitor]# git add .
[root@gitlab ~/monitor]# git commit -m "第一次提交"
# On branch master
nothing to commit, working directory clean
[root@gitlab ~/monitor]# git push origin master
Counting objects: 435, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (372/372), done.
Writing objects: 100% (435/435), 8.78 MiB | 0 bytes/s, done.
Total 435 (delta 53), reused 435 (delta 53)
remote: Resolving deltas: 100% (53/53), done.
To git@gitlab.xts.com:xts/monitor.git
* [new branch] master -> master
瀏覽器刷新查看結果
4.2.4 配置jenkins job連接到gitlab
(1)jenkins安裝插件(gitlab plugins)
5. 搭建一套小的集群,實現手動代碼上線
5.1 服務器配置
主機名 | IP | 說明 |
---|---|---|
lb01 | 192.168.1.91 | 負載均衡 |
web01 | 192.168.1.92 | 后端節點 |
web02 | 192.168.1.93 | 后端節點 |
5.2 搭建過程
5.2.1 准備web集群環境
# lb01、web01、web02都安裝nginx
yum -y install nginx
# 配置負載(lb01)
[root@lb01 ~]# vim /etc/nginx/conf.d/proxy_html.xts.com.conf
[root@lb01 ~]# cat /etc/nginx/conf.d/proxy_html.xts.com.conf
upstream html {
server 192.168.1.92:80;
server 192.168.1.93:80;
}
server {
listen 80;
server_name html.xts.com;
location / {
proxy_pass http://html;
proxy_set_header Host $http_host;
}
}
[root@lb01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 ~]# systemctl restart nginx
[root@lb01 ~]# systemctl enable nginx
# 配置后端web節點
## web01
[root@web01 ~]# vim /etc/nginx/conf.d/html.xts.com.conf
[root@web01 ~]# cat /etc/nginx/conf.d/html.xts.com.conf
server {
listen 80;
server_name html.xts.com;
root /code/web;
location / {
index index.html;
}
}
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl restart nginx
[root@web01 ~]# systemctl enable nginx
## web02
[root@web02 ~]# vim /etc/nginx/conf.d/html.xts.com.conf
[root@web02 ~]# cat /etc/nginx/conf.d/html.xts.com.conf
server {
listen 80;
server_name html.xts.com;
root /code/web;
location / {
index index.html;
}
}
[root@web02 ~]# systemctl restart nginx
[root@web02 ~]# systemctl enable nginx
# 創建站點目錄
[root@web01 ~]# mkdir /code/web -p
[root@web01 ~]# echo web01 > /code/web/index.html
[root@web02 ~]# mkdir -p /code/web
[root@web02 ~]# echo web02 > /code/web/index.html
5.3 瀏覽器訪問
# hosts解析配置
192.168.1.91 html.xts.com
5.4 克隆代碼
[root@jenkins /tmp]# git clone git@gitlab.xts.com:xts/monitor.git
Cloning into 'monitor'...
The authenticity of host 'gitlab.xts.com (192.168.1.51)' can't be established.
ECDSA key fingerprint is SHA256:TTu0eIxmefjYUd12/DCcn8irm/rmOBQP/U31gIKhbDo.
ECDSA key fingerprint is MD5:a0:f0:7a:09:ef:b7:e8:4b:0f:3c:c8:6e:cb:6c:a4:7e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitlab.xts.com,192.168.1.51' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 435, done.
remote: Counting objects: 100% (435/435), done.
remote: Compressing objects: 100% (372/372), done.
remote: Total 435 (delta 53), reused 435 (delta 53)
Receiving objects: 100% (435/435), 8.78 MiB | 0 bytes/s, done.
Resolving deltas: 100% (53/53), done.
[root@jenkins /tmp]# ls
monitor
# 把代碼傳輸到兩個web節點
## 這里注意:jenkins和兩台web需要打通免密
[root@jenkins /tmp]# for i in 192.168.1.92 192.168.1.93;do scp -rp monitor/* ${i}:/code/web/;done
5.5 瀏覽器訪問
6. jenkins實現代碼自動上線
6.1 實現思路
# 腳本
(1)進入對應的倉庫目錄
(2)打包
(3)scp推送
6.2 實現步驟
6.2.1 編寫腳本
[root@jenkins /scripts]# cat html_deploy.sh
#!/bin/bash
Date=$(date +%F-%H:%M)
web_server="192.168.1.92 192.168.1.93"
code_tar(){
cd /var/lib/jenkins/workspace/freestyle-monitor
tar czf /opt/web-${Date}.tar.gz ./*
}
scp_web(){
for host in $web_server
do
scp -rp /opt/web-${Date}.tar.gz ${host}:/code
ssh ${host} "mkdir -p /code/web-${Date} && \
tar xf /code/web-${Date}.tar.gz -C /code/web-${Date} && \
rm -f /code/web-${Date}.tar.gz && \
rm -rf /code/web && \
ln -s /code/web-${Date} /code/web"
done
}
deploy(){
code_tar
scp_web
}
deploy
[root@jenkins /scripts]# chmod +x html_deploy.sh
# 打通免密
[root@jenkins /scripts]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.92
[root@jenkins /scripts]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.93
# 手動執行腳本,然后檢查執行結果
[root@jenkins /scripts]# sh html_deploy.sh
web-2020-06-26-14:59.tar.gz 100% 4545KB 28.3MB/s 00:00
web-2020-06-26-14:59.tar.gz 100% 4545KB 43.8MB/s 00:00
# web檢查執行結果
[root@web01 /code]# ls
web web-2020-06-26-14:59
[root@web02 /code]# ls
web web-2020-06-26-14:59
6.2.2 整合腳本到jenkins
編輯jenkins配置文件,解決報錯
瀏覽器重新驗證
修改網站代碼上傳,再次驗證