Jenkins持續集成工具 - 安裝部署以及參數化構建

目錄

一、部署git環境準備與jenkins聯動

1.1、git服務器下載git並初始化

1.2、ssh公鑰實現免交互

1.3、git提交同步測試

二、jenkins安裝部署訪問

2.1、確認JDK環境(Jenkins是java編寫)

2.2、啓動jenkins訪問頁面安裝插件

三、git參數化構建

3.1、job+git+master構建模式

3.2、job+git+多分支構建模式


常用應用場景
• 創建一個項目,手動可以構建,完成一些簡單任務,比如下 載代碼後進行編譯 • 編譯失敗郵件通知用戶 • 用戶可以選擇不同參數進行構建 • 代碼改動後自動觸發某個構建 或者定時觸發某個項目構建 • 一個項目構建完成後可以自動調用另一個項目的構建,完成 一連串的任務 • 併發
官網:https://jenkins.io/zh/

一、部署git環境準備與jenkins聯動

#規劃
linux-node1.example.com  jenkins服務器,部署服務頁面展示
linux-node2.example.com  web測試、git本地服務器、jenkins-slave節點
linux-node3.example.com  git總服務器,相當於中轉站共享庫,自身不創建git文件,只接收本地分支上傳

1.1、git服務器下載git並初始化

#創建用戶及初始化git庫

[root@linux-node3 ~]# yum install -y git
[root@linux-node3 ~]# useradd git
[root@linux-node3 ~]# passwd git
[root@linux-node3 /]# su git
[git@linux-node3 ~]$ mkdir -p repos/app.git && cd ./repos/app.git 
[git@linux-node3 app.git]$ git --bare init  # 總庫初始化、總庫一般都是空庫

# web服務器安裝git作客戶端

[root@linux-node1 ~]# yum install -y git  # node1是jenkins服務器job需要調用git
[root@linux-node2 ~]# yum install -y git  # node2是本地git庫

1.2、ssh公鑰實現免交互

#同步本地文件到git服務器庫就不要密碼了

[root@linux-node2 app]# ssh-keyscan
[root@linux-node2 app]# cat /root/.ssh/id_rsa.pub  # 公鑰放入git服務器存放文件中
[root@linux-node3 /]# cat /etc/ssh/sshd_config | grep AuthorizedKeysFile
AuthorizedKeysFile      .ssh/authorized_keys
[git@linux-node3 /]$ mkdir /home/git/.ssh && chmod 700 /home/git/.ssh  # 創建存放ssh-key目錄文件
[git@linux-node3 /]$ vim /home/git/.ssh/authorized_keys && chmod 600 /home/git/.ssh/authorized_keys
[root@linux-node3 /]# vim  /home/git/.ssh/authorized_keys && echo "node2公鑰內容" >> authorized_keys

1.3、git提交同步測試

# 測試能否正常使用git,本地先創建文件準備提交git服務器

[root@linux-node2 test-git]# mkdir test-git/app && cd  test-git/app && touch index.html && echo 123 > index.html

# 同步git服務器倉庫成功

[root@linux-node2 test-git]# git clone [email protected]:/home/git/repos/app.git  # 目前git服務器沒有本地提交還是空
warning: 您似乎克隆了一個空版本庫。

# 創建文件先推送本地git庫

[root@linux-node2 app]# git add .
[root@linux-node2 app]# git status
# 位於分支 master
#
# 初始提交
#
# 要提交的變更:
#   (使用 "git rm --cached <file>..." 撤出暫存區)
#
#       新文件:    1.html
#
[root@linux-node2 app]# git commit -m 'test01'

# 推送本地git到服務器git庫

[root@linux-node2 app]# git push origin master  # master主分支

# 再次拉去git庫可以拉到我們剛纔上傳的文件

[root@linux-node2 app]# git clone [email protected]:/home/git/repos/app.git 
[root@linux-node2 app]# ls app/
123.html  1.html  index.html

# 至此,一個簡單的git服務端和客戶端完畢

二、jenkins安裝部署訪問

2.1、確認JDK環境(Jenkins是java編寫)

# 確認有沒有java環境,沒有需要部署jdk

[root@linux-node1 ~]# cp jdk-8u91-linux-x64.tar.gz /usr/local && cd /usr/local
[root@linux-node1 local]# ln -s jdk1.8.0_91 jdk1.8
[root@linux-node1 jdk1.8.0_91]# vim /etc/profile
#set java environment
JAVA_HOME=/usr/local/jdk1.8
JRE_HOME=$JAVA_HOME/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export JAVA_HOME JRE_HOME PATH CLASSPATH
[root@linux-node1 jdk1.8.0_91]# source /etc/profile
[root@linux-node1 jdk1.8.0_91]# java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

2.2、啓動jenkins訪問頁面安裝插件

# 添加jenkins源和官方證書認證
# 官網:https://pkg.jenkins.io/redhat-stable/

[root@linux-node1 ~]# sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
[root@linux-node1 ~]# sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

# 安裝

[root@linux-node1 yum.repos.d]# yum install -y jenkins  # 或者本地yum更快
[root@linux-node1 ~]# yum localinstall jenkins-2.204.6-1.1.noarch.rpm -y

# 此時啓動報錯:Starting Jenkins bash: /usr/bin/java: 沒有那個文件或目錄
# 原因爲jdk爲自己安裝,路徑與jenkins默認jdk路徑不一致,需要軟鏈接

[root@linux-node1 local]# ln -s /usr/local/jdk1.8/bin/java /usr/bin/java
[root@linux-node1 local]# systemctl stop jenkins
[root@linux-node1 local]# systemctl start jenkins

# 進程端口日誌確認

[root@linux-node1 local]# ps -ef | grep jenkins
jenkins   39598      1 11 17:56 ?        00:00:13 /usr/bin/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
[root@linux-node1 local]# lsof -i:8080
COMMAND   PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
java    39598 jenkins  161u  IPv6  57195      0t0  TCP *:webcache (LISTEN)
[root@linux-node1 local]# tailf /var/log/jenkins/jenkins.log

# 瀏覽器訪問(關閉代理)

[root@linux-node1 ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
b892e58974424681bd41a4bb0ccba2fc  # 獲取默認密鑰
瀏覽器:http://192.168.56.11:8080/

# 解決Jenkins插件不能下載安裝的問題

# 本人安裝時插件下載速度很慢並且有失敗,建議使用第三方源
換源地址:http://192.168.56.11:8080/pluginManager/advanced
升級站點URL:http://mirror.esuni.jp/jenkins/updates/update-center.json
參考文章:
-- https://blog.csdn.net/weixin_42248864/article/details/88366791
-- https://www.cnblogs.com/sxdcgaq8080/p/10489326.html
# 插件下載完成後創建用戶密碼保存完成 Rui 123456

三、git參數化構建

3.1、job+git+master構建模式

當前主要利用jenkins的job遠程訪問git庫,同步數據,默認需要手動輸入同步哪個分支

# 首先插件安裝Git Parameter

# 首先構建freestyle-job

# 構建shell參數化獲取變量

# 源碼管理負責連接git服務器庫

# 根據分支獲取對應變量

# 開始構建job

構建時報錯:

****報錯****:
The default value has been returned
An error occurred while download data
Command "git ls-remote -h [email protected]:/home/git/repos/app.git" returned status code 128:
stdout:
stderr: Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Please look at the Log
Please check the configuration

解決方法:
1、jenkins默認通過用戶jenkins訪問git服務器,權限不夠
[root@linux-node1 web-01]# cat /etc/sysconfig/jenkins | grep root
JENKINS_USER="root"
2、jenkins服務克隆git庫時默認需要交互確認輸入yes,導致job執行失敗
實現jenkins服務器和git服務器ssh免交互,參考1.2章節
解決方法來源:http://www.eryajf.net/562.html


3.2、job+git+多分支構建模式

當前利用Git Parameter插件實現自動識別git庫有哪些分支供用戶選擇
job構建時不會直接開始構建,可以先選擇構建哪個git分支

# 默認git庫只有一個master分支,現在本地創建一個名爲test01的新分支
# 然後利用test01分支提交一個文件test01.html到git服務器庫
# 那麼現在git庫就總共有兩個分支了

[root@linux-node2 app]# pwd
/root/test-git/app
[root@linux-node2 app]# git branch test01 
[root@linux-node2 app]# git checkout test01
切換到分支 'test01'
[root@linux-node2 app]# touch test01.html
[root@linux-node2 app]# git add .
[root@linux-node2 app]# git commit -m 'test01'
[root@linux-node2 app]# git push origin test01
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 273 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:/home/git/repos/app.git
 * [new branch]      test01 -> test01

# 此時構建項目會自動識別新分支

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章