接Jenkins+Gitlab+Ansible自動化部署(一)https://www.cnblogs.com/zd520pyx1314/p/10210727.html 和(二)https://www.cnblogs.com/zd520pyx1314/p/10213549.html
Jenkins是一個開源持續集成工具,提供了軟甲你開發的持續集成服務,支持主流軟件配置管理,配合實現軟件配置管理,持續集成功能。是主流的運維開發平台,兼容所有主流開發環境,插件市場可與海量業內主流開發工具實現集成,Job為配置單位與日志管理,使運維與開發人員能協同工作。豐富的權限管理划分不同Job不同角色;強大的負載均衡功能,保證我們項目的可靠性。
Jenkins的安裝、配置與管理
添加Jenkins yum倉庫
官網地址 https://pkg.jenkins.io/redhat-stable/
安裝
[root@jenkins ~]# wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo [root@jenkins ~]# rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key 安裝Java [root@jenkins ~]# yum install -y java [root@jenkins ~]# java -version openjdk version "1.8.0_191" OpenJDK Runtime Environment (build 1.8.0_191-b12) OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode) 安裝Jenkins [root@jenkins ~]# yum list | grep 'jenkins' jenkins.noarch [root@jenkins ~]# yum install -y jenkins
創建Jenkins系統服務用戶並配置
創建Jenkins系統服務用戶 [root@jenkins ~]# useradd deploy [root@jenkins ~]# cp /etc/sysconfig/jenkins{,.bak} [root@jenkins ~]# vim /etc/sysconfig/jenkins # 大約在29行,改為deploy用戶 29 JENKINS_USER="deploy" # 確定Jenkins端口號8080 56 JENKINS_PORT="8080" 更改目錄權限 [root@jenkins ~]# chown -R deploy:deploy /var/lib/jenkins [root@jenkins ~]# chown -R deploy:deploy /var/log/jenkins/ 啟動Jenkins [root@jenkins ~]# systemctl start jenkins [root@jenkins ~]# lsof -i:8080 # 這里發現端口沒起來,查看日志發現 [root@jenkins ~]# cat /var/log/jenkins/jenkins.log java.io.FileNotFoundException: /var/cache/jenkins/war/META-INF/MANIFEST.MF (Permission denied) # 然后賦予deploy目錄權限 [root@jenkins ~]# chown -R deploy:deploy /var/cache/jenkins/ [root@jenkins ~]# systemctl restart jenkins [root@jenkins ~]# lsof -i:8080 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME java 4086 deploy 163u IPv6 49665 0t0 TCP *:webcache (LISTEN) 啟動成功
登錄jenkins web管理界面
點擊“Start using jenkins”
Jenkins Job構建
Freestyle Job與Pipeline Job區別:
Freestyle Job需要在頁面添加模塊配置項與參數完成配置;每個Job僅能實現一個開發功能;無法將配置代碼化,不利於Job配置遷移與版本控制;邏輯相對簡單,無需額外學習成本。
Pipeline Job匹配持續集成與持續交付的概念;所有模塊、參數配置都可以體現為一個pipeline腳本;可定義多個stage構建一個管道工作集;所有配置代碼化,方便Job配置遷移與版本控制;需要Pipeline腳本語法基礎。
Jenkins Job構建之環境准備(添加Jenkins后台git client user與email)
1.配置Jenkins server本地GItlab DNS
[root@jenkins ~]# vim /etc/hosts # 文件末尾添加如下一條記錄 192.168.244.130 gitlab.example.com
2.安裝git client,curl工具依賴
[root@jenkins ~]# yum install -y git curl
3. 關閉系統git http.sslVerify安全認證
[root@jenkins ~]# git config --system http.sslVerify false [root@jenkins ~]# echo $? 0
4.添加Jenkins后台git client user與email
首先登錄Jenkins web管理頁面
在Git plugin選項中填寫以下信息,點擊保存
接下來添加憑據,點擊“憑據”
點擊“全局憑據”
點擊“添加憑據”
添加完成會提示如下圖所示
接着添加一個Jenkins freestyle job
點擊“New 任務”
填寫描述信息
添加參數
接着點擊添加“文本參數”
添加完成后點擊“save”即可,接着回到Jenkins首頁,點擊剛才創建的“test-freestyle-job”黑色小三角,找到“configure”選項,開始添加git源碼管理
使用root登錄gitlab,復制test-repo倉庫地址
粘貼至下面
接着進行“build 配置”
在以下框內粘貼
#!/bin/sh export PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin" # Print env variable echo "[INFO] Print env variable" echo "Current deployment envrionment is $deploy_env" >> test.properties echo "THe build is $version" >> test.properties echo "[INFO] Done..." # Check test properties echo "[INFO] Check test properties" if [ -s test.properties ] then cat test.properties echo "[INFO] Done..." else echo "test.properties is empty" fi echo "[INFO] Build finished..."
接下來點擊“Build with Parameters”
提示失敗,點擊紅色失敗按鈕,查看日志並解決
可以看出還是之前的git有點問題,回到test-freestyle-job配置項,查看並確認
然后重新構建
可以看到已經成功構建。
接下來演示Jenkins Pipeline Job構建過程
Pipeline基礎架構
1.所有代碼包裹在pipeline{}層內
2.stages{}層用來包含該pipeline所有stage子層
3.stage{}層用來包含具體我們需要編寫任務的steps{}子層
4.steps{}用來添加我們具體需要調用的模塊語句
agent區域
- agent定義pipeline在哪里運行,可以使用any,none,或具體的Jenkins node主機名等;例如:假定我們要特指在node1上執行,可以寫成:agent{node1 {label 'node1'}}。
environment區域
- “變量名稱=變量值”定義我們的環境變量;
- 可以定義全局環境變量,應用所有stage任務
- 可以定義stage環境變量,應用單獨的stage任務
script區域(可選)
- 在steps內定義script{};
- groovy腳本語言;
- 用來進行腳本邏輯運算;
常用steps區域
- echo:打印輸出
- sh:調用Linux系統shell命令
- git url:調用git模塊進行git相關操作
開始構建Jenkins Pipeline Job
首先登錄到Jenkins web 管理頁
點擊“New 任務”
添加描述信息
添加pipeline script
pipeline script腳本內容(用上述復制下來的ID粘貼至credentialsId后)
#!groovy pipeline { agent {node {label 'master'}} environment { PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin" } parameters { choice( choices: 'dev\nprod', description: 'choose deploy environment', name: 'deploy_env' ) string (name: 'version', defaultValue: '1.0.0', description: 'build version') } stages { stage("Checkout test repo") { steps{ sh 'git config --global http.sslVerify false' dir ("${env.WORKSPACE}") { git branch: 'master', credentialsId:"b974bdfd-bb73-4f0a-8a0d-85d867681ed0", url: 'https://root@gitlab.example.com/root/test-repo.git' } } } stage("Print env variable") { steps { dir ("${env.WORKSPACE}") { sh """ echo "[INFO] Print env variable" echo "Current deployment environment is $deploy_env" >> test.properties echo "The build is $version" >> test.properties echo "[INFO] Done..." """ } } } stage("Check test properties") { steps{ dir ("${env.WORKSPACE}") { sh """ echo "[INFO] Check test properties" if [ -s test.properties ] then cat test.properties echo "[INFO] Done..." else echo "test.properties is empty" fi """ echo "[INFO] Build finished..." } } } } }
“保存”之后,點擊“立即構建”
報錯,點擊查看報錯信息
根據錯誤提示:沒有找到對應參數的變量,是因為首次構建pipeline job時,參數沒有被引用到當前pipeline job當中,返回test-pipeline-job主界面,此時的“立即構建”按鈕會變為“Build with Parameters”,點擊“Build with Parameters”
可以看到第二次構建是成功的,點擊#2前的藍色圓球查看輸出信息
Started by user admin Running in Durability level: MAX_SURVIVABILITY [Pipeline] node Running on Jenkins in /var/lib/jenkins/workspace/test-pipeline-job [Pipeline] { [Pipeline] withEnv [Pipeline] { [Pipeline] stage [Pipeline] { (Checkout test repo) [Pipeline] sh + git config --global http.sslVerify false [Pipeline] dir Running in /var/lib/jenkins/workspace/test-pipeline-job [Pipeline] { [Pipeline] git > git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url https://root@gitlab.example.com/root/test-repo.git # timeout=10 Fetching upstream changes from https://root@gitlab.example.com/root/test-repo.git > git --version # timeout=10 using GIT_ASKPASS to set credentials > git fetch --tags --progress https://root@gitlab.example.com/root/test-repo.git +refs/heads/*:refs/remotes/origin/* > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10 Checking out Revision dd39fbeeb70dd5e2d545dfe084c3d540d106d6ef (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f dd39fbeeb70dd5e2d545dfe084c3d540d106d6ef > git branch -a -v --no-abbrev # timeout=10 > git branch -D master # timeout=10 > git checkout -b master dd39fbeeb70dd5e2d545dfe084c3d540d106d6ef Commit message: "Merge branch 'release-1.0.0' into 'master'" > git rev-list --no-walk dd39fbeeb70dd5e2d545dfe084c3d540d106d6ef # timeout=10 [Pipeline] } [Pipeline] // dir [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Print env variable) [Pipeline] dir Running in /var/lib/jenkins/workspace/test-pipeline-job [Pipeline] { [Pipeline] sh + echo '[INFO] Print env variable' [INFO] Print env variable + echo 'Current deployment environment is dev' + echo 'The build is 1.0.0' + echo '[INFO] Done...' [INFO] Done... [Pipeline] } [Pipeline] // dir [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Check test properties) [Pipeline] dir Running in /var/lib/jenkins/workspace/test-pipeline-job [Pipeline] { [Pipeline] sh + echo '[INFO] Check test properties' [INFO] Check test properties + '[' -s test.properties ']' + cat test.properties Current deployment environment is dev The build is 1.0.0 + echo '[INFO] Done...' [INFO] Done... [Pipeline] echo [INFO] Build finished... [Pipeline] } [Pipeline] // dir [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Finished: SUCCESS
可以看到輸出狀態為“SUCCESS”,證明構建成功。