Gitlab源碼庫里代碼提交后,如何觸發jenkins自動構建?


版本庫里代碼提交后,如何觸發jenkins自動構建?這是一個面試題,感覺自己回答的並不好,因為並沒有用過這個功能,之前公司實際項目用的是svn版本管理,一般都用立刻構建,和定時任務構建(不管代碼是否有提交,都到代碼庫里拉取最新的代碼,然后構建)。並且我也是這樣告訴面試官的,顯然面試官對我很困惑,最終,我告訴他我們的源碼管理工具用的是SVN,然后,可能把面試官也弄懵了,因為面試公司用的是gitlab. 哈哈,面試官自己沒有嘗試過“SVN+觸發構建插件”吧。

經過查詢,網上針對Gitlab源碼管理+jenkins是有解決方案的: Gitlab利用Webhook實現Push代碼后的jenkins自動構建

下面是我的實踐過程:

已經准備好如下環境:

Gitlab內網服務器地址:http://192.168.1.50:8090

jenkins內網服務器地址:http://192.168.1.40:8080

步驟從這里開始:

1. jenkins,分別點擊"系統管理"->"插件管理"->"可選插件",選擇Gitlab和Gitlab Hook Plugin 

 

 

2.192.168.1.50服務器上,需要建立git用戶

useradd git
passwd git

3(這個步驟,請跳過,后面jenkins里,憑據我並沒有選擇免密登陸). 將Jenkins的公鑰key添加到Gitlab上, 這樣就可以實現jenkins訪問gitlab免密碼 

在 Docker:pipeline編寫基本技巧- jenkins配置通過密鑰拉取git源碼管理倉庫的代碼 中,已經有使用公鑰私鑰的經驗了

現在登陸 192.168.1.40 

#192.168.1.40(jenkins)機器上,已經存在公鑰
[root@192 ~]# ls .ssh/ id_rsa id_rsa.pub known_hosts #將192.168.1.40機器的公鑰Copy到192.168.1.50(Gitlab)服務器上
[root@192 ~]# ssh-copy-id git@192.168.1.50 
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.1.50's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.1.50'" and check to make sure that only the key(s) you wanted were added.
[root@
192 ~]# ssh 'git@192.168.1.50'
Last
login: Sat Apr 6 02:01:01 2019 from mei-pc
[root@
192 ~]# exit
logout Connection to
192.168.1.50 closed.
[root@
192 ~]#

 

 

4. Gitlab服務器上,如果沒有項目,可以參考 Github: 從github上拉取別人的源碼,並推送到自己的github倉庫  找一個項目放上去

步驟如下:

[root@192 tomcat-java-demo]# vi .git/config 

[root@192 tomcat-java-demo]# git remote add origin http://192.168.1.50:8090/root/tomcat-java-demo-gitlab.git
fatal: remote origin already exists.

#如果遇到上面這個錯誤提示,就按照接下來的操作
[root@192 tomcat-java-demo]# git remote rm origin
[root@192 tomcat-java-demo]# git remote add origin http://192.168.1.50:8090/root/tomcat-java-demo.git
[root@192 tomcat-java-demo]# git push -u origin master
Username for 'http://192.168.1.50:8090': root
Password for 'http://root@192.168.1.50:8090': 
Counting objects: 229, done.
Compressing objects: 100% (185/185), done.
Writing objects: 100% (229/229), 4.52 MiB | 1.15 MiB/s, done.
Total 229 (delta 25), reused 229 (delta 25)
remote: Resolving deltas: 100% (25/25), done.
remote: 
remote: The private project root/tomcat-java-demo was successfully created.
remote: 
remote: To configure the remote, run:
remote:   git remote add origin http://192.168.1.50:8090/root/tomcat-java-demo.git
remote: 
remote: To view the project, visit:
remote:   http://192.168.1.50:8090/root/tomcat-java-demo
remote: 
To http://192.168.1.50:8090/root/tomcat-java-demo.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

上傳代碼到gitlab成功, 到gitlab管理界面驗證一下

 

4. jenkins上新建項目,這里項目名:testgitlabhook

 

 

設置jenkins拉 gitlab上代碼地址和對應的憑據

 

 

保存

5. 到gitlab管理頁面進行配置

點 Add Webhook后,

出現如下錯誤

解決參考地址:gitlab使用webhook向jenkins發送請求,報錯 Requests to the local network are not allowed   

大致意思 gitlab 10.6 版本以后為了安全,不允許向本地網絡發送webhook請求,如果想向本地網絡發送webhook請求,則需要使用管理員帳號登錄, 做如下修改

現在再回到上一個步驟 

點[Add webhook],不報錯了,在下面出現 標簽 1所示, 

 

按照上圖標簽操作,點2,3 

會出現如下錯誤

這是沒有權限,需要把Jenkins-->Jenkins Manages-->Configure System,找到GitLab配置,去掉勾選。

參考解決地址:https://www.jianshu.com/p/156de44a44c2

 

 再回去點 Test-> push event

出現 Hook executed successfully: HTTP 200 說明配置成功了

 

你會發現,每點一次 Test-> push event,就會觸發一次jenkins立刻構建

或者:

到gitlab管理界面,tomcat-java-demo 項目下,新增一個文件,提交后,也會發現觸發了jenkins的立刻構建。 

到此,gitlab 通過webhook觸發jenkins自動構建流程通了。

 

作者: 梅梅~

出處: https://www.cnblogs.com/keeptesting

關於作者:專注軟件測試,測試運維相關工作,請多多賜教!

本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出, 原文鏈接 歡迎溝通交流加微信聯系。 微信:yangguangkg20140901 暗號:博客園.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM