基於kubernetes和jenkins的gitops實戰-jenkins與gitlab相關聯
gitlab通知觸發自動構建:
配置要點:
一、前提:gitlab和jenkins同在本地網路時,需要以管理員許可權設定“外發請求”,啟用“允許webhook和服務對本地網路的請求”;
1. 授予jenkins訪問gitlab上倉庫中特定使用者的程式碼許可權;
2. jenkins基於SSH協議獲取程式碼倉庫中的內容,因而事先需要配置jenkins能基於ssh金鑰接入gitlab;
二、Gitlab API訪問授權
1. 配置Gitlab API訪問認證(Access token)及操作許可權;
2. 配置Jenkins啟用/project端點認證,配置他能通過配置的Access Token接入Gitlab API;
三、Jenkins pipeline 專案的通知授權
1. 建立pipeline任務,選擇使用的"GitLab Connect",並選定使用“Build when a change is pushed to GitLab.” 觸發器;
2. 而後為構建觸發器生成Secret token;
3. 配置Gitlab Repository 能通過webhook,基於該Secret token觸發構建
具體配置文件見:https://docs.gitlab.com/ce/integration/jenkins.html
操作:
一、外發請求
生成ssh金鑰,將私鑰放置在jenkins上,將公鑰放在gitlab上,這樣jenkins就可以通過私鑰訪問gitlab了。
[root@master01 ~]# cd .ssh/ [root@master01 .ssh]# ls id_rsa id_rsa.pub known_hosts [root@master01 .ssh]# cat id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC6ZX2JiJhsOh1SH76DXXhyTuIjR3XJ8UF3tVk6/kUHfVyKIvNUr…… [root@master01 .ssh]# ssh-keygen
將公鑰儲存在gitlab的ssh金鑰上,preferences下:
然後要把私鑰資訊建立為jenkins上的credential---全域性----新增憑據-----ssh Username with private key;私鑰與公鑰是一對,在~/.ssh/id_rsa檔案
二、在Gitlab上建立Access Token,可以讓Jenkins向Gitlab傳遞自己構建結果的資訊;
然後授權Jenkins通過Access token能夠連線Gitlab,所以首先需要安裝gitlab外掛,一旦安裝成功,在jenkins上Configure System就會多一個gitlab的配置項
三、配置jenkins的專案可經由Gitlab上的事件觸發,即構建觸發器,讓專案和倉庫建立關聯關係;
該觸發器設定一個token,不能誰都可以完成觸發器構建工作:構建觸發器-----高階
配置gitlab的webhooks,能夠通過該secret token觸發構建,這個配置是專案內的配置,需要在專案下-------設定-----webhooks
如果出現了這個報錯:“Urlis blocked: Requests to the local network are not allowed”請檢查第一步“允許webhook和服務對本地網路的請求”是否儲存好。
測試,儲存完webhooks後下方就有測試專案:
測試成功後,就可以修改pipeline的版本號,和專案裡pom.xml和spring-boot-helloworld\src\main\java\com\neo\controller\下HelloWorldController.java的版本號
然後推送程式碼:
1 huan@DESKTOP-07DR243 MINGW64 ~/Desktop/git/spring-boot-helloworld/gitlab-self/spring-boot-helloworld (master) 2 $ git add * 3 4 huan@DESKTOP-07DR243 MINGW64 ~/Desktop/git/spring-boot-helloworld/gitlab-self/spring-boot-helloworld (master) 5 $ git remote -v 6 origin http://192.168.3.100:30080/root/spring-boot-helloworld.git (fetch) 7 origin http://192.168.3.100:30080/root/spring-boot-helloworld.git (push) 8 9 huan@DESKTOP-07DR243 MINGW64 ~/Desktop/git/spring-boot-helloworld/gitlab-self/spring-boot-helloworld (master) 10 $ git commit -m "update to 0.9.2" 11 [master 8e44f1f] update to 0.9.2 12 2 files changed, 2 insertions(+), 2 deletions(-) 13 14 huan@DESKTOP-07DR243 MINGW64 ~/Desktop/git/spring-boot-helloworld/gitlab-self/spring-boot-helloworld (master) 15 $ git push origin master 16 Enumerating objects: 19, done. 17 Counting objects: 100% (19/19), done. 18 Delta compression using up to 8 threads 19 Compressing objects: 100% (8/8), done. 20 Writing objects: 100% (10/10), 736 bytes | 368.00 KiB/s, done. 21 Total 10 (delta 3), reused 0 (delta 0) 22 To http://192.168.3.100:30080/root/spring-boot-helloworld.git 23 d92b711..8e44f1f master -> master 24 25 huan@DESKTOP-07DR243 MINGW64 ~/Desktop/git/spring-boot-helloworld/gitlab-self/spring-boot-helloworld (master) 26 $
就發現可以自動推送了,現在最後一步,要把jenkinsfile檔案也使用SCM倉庫推送:
改好後,我們修改下3個檔案的版本標籤,推送試下: