Jenkins持續集成工具 - Master-Slave架構以及Pipeline流水線

目錄

一、Master-Slave架構

1.1、開始部署jenkins-slave節點

1.2、slave構建任務

二、Pipeline流水線

2.1、利用pipeline-script方式直接執行流水線

2.2、利用pipeline-script-from-SCM方式執行流水線


一、Master-Slave架構

實際生產環境jenkins主節點一般不直接處理job,而是分發至下屬slave節點構建任務

1.1、開始部署jenkins-slave節點

# slave構建job也需要java支持

[root@linux-node2 ~]# java -version
[root@linux-node2 ~]# mkdir /var/lib/jenkins

# 新增slave節點
Manage Jenkins---Manage Nodes---新建節點
選項解釋參考:https://www.jianshu.com/p/2c5b19f587c2

 

# slave正常啓動
# 網傳節點機器slave進程可能是老版本,目前出現remoting.jar進程

[root@linux-node2 jenkins]# ps -ef | grep jenkins
root       1494   1448  0 14:35 ?        00:00:00 bash -c cd "/var/lib/jenkins" && java  -jar remoting.jar -workDir /var/lib/jenkins -jar-cache /var/lib/jenkins/remoting/jarCache
root       1501   1494  9 14:35 ?        00:00:10 java -jar remoting.jar -workDir /var/lib/jenkins -jar-cache /var/lib/jenkins/remoting/jarCache
root       1623   1299  0 14:37 pts/0    00:00:00 grep --color=auto jenkins

1.2、slave構建任務

# job根據標籤配置slave執行

# 任務在哪構建在哪部署,slave構建成功後同步至相應目錄
# 也可以繼續同步或者共享至其他生產服務器

[root@linux-node2 jenkins]# ll /var/lib/jenkins/workspace/web-01
總用量 12
-rw-r--r-- 1 root root 7 4月  12 14:47 123.html
-rw-r--r-- 1 root root 4 4月  12 14:47 1.html
-rw-r--r-- 1 root root 4 4月  12 14:47 index.html
-rw-r--r-- 1 root root 0 4月  12 14:47 master.html

二、Pipeline流水線

特點:
1、項目發佈可視化,明確階段,方便定位問題
2、一個Jenkinsfile文件管理項目整個生命週期
3、Jenkinsfile文件可以放項目代碼中進行版本管理


2.1、利用pipeline-script方式直接執行流水線

# 創建流水線任務
# 直接寫流水線代碼執行

# 流水線怎麼寫可參考直接生成片段模板功能

2.2、利用pipeline-script-from-SCM方式執行流水線

# 讓整個構建過程通過讀取配置文件的方式開始執行,jenkins頁面job無需太多配置,
# 如果有更新直接修改配置文件重新提交git庫,jenkins直接再次構建即可。
# 本次構建直接讀取git服務器內pipeline版本庫的配置文件

# 創建pipeline版本庫

[git@linux-node3 repos]$ pwd
/home/git/repos
[git@linux-node3 repos]$ mkdir pipeline.git && cd pipeline.git
[git@linux-node3 pipeline.git]$ git --bare init
初始化空的 Git 版本庫於 /home/git/repos/pipeline.git/

# 本地上傳pipeline腳本至版本庫

[root@linux-node2 app]# pwd  # 本地git庫位置
/root/test-git/app
[root@linux-node2 app]# git clone [email protected]:/home/git/repos/pipeline.git  # 首先下載好pipe版本庫
正克隆到 'pipeline'...
warning: 您似乎克隆了一個空版本庫。
[root@linux-node2 app]# cd pipeline  # 準備往pipe版本庫扔東西
[root@linux-node2 pipeline]# vim pipeline-script  # 配置構建全過程
[root@linux-node2 pipeline]# cat pipeline-script  # 注意:配置讀取是pipeline版本庫,不一定實際同步也是pipe庫文件,具體看腳本怎麼配置
node ('web') {   //配置slave標籤,否則默認jenkins服務端執行
   stage('Checkout') {  //pipeline語法片段生成的,用來checkout git庫
      checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'dee660c8-a39d-4ce6-805a-744e33004e14', url: '[email protected]:/home/git/repos/app.git']]])
   }
   stage('Build') {  //模擬下其他過程而已
      // Run the maven build
      echo 'Build...'
   }
   stage('Results') {
      echo 'Results...'
   }
}
[root@linux-node2 pipeline]# git add pipeline-script  # 告訴Git,把文件添加到倉庫
[root@linux-node2 pipeline]# git commit pipeline-script -m 'pipe-test01'  # 告訴Git,把文件提交到倉庫:
[root@linux-node2 pipeline]# git branch  # 告訴Git,當前是哪個分支在搞事情
* master
[root@linux-node2 pipeline]# git push origin master  # 提交到共享庫
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 502 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:/home/git/repos/pipeline.git
 * [new branch]      master -> master


# 流水線job配置裏調用腳本

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