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

版本庫裏代碼提交後,如何觸發jenkins自動構建?這是一個面試題,感受本身回答的並很差,由於並無用過這個功能,以前公司實際項目用的是svn版本管理,通常都用馬上構建,和定時任務構建(無論代碼是否有提交,都到代碼庫里拉取最新的代碼,而後構建)。而且我也是這樣告訴面試官的,顯然面試官對我很困惑,最終,我告訴他咱們的源碼管理工具用的是SVN,而後,可能把面試官也弄懵了,由於面試公司用的是gitlab. 哈哈,面試官本身沒有嘗試過「SVN+觸發構建插件」吧。html

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

下面是個人實踐過程:git

已經準備好以下環境:github

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

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

步驟從這裏開始:tomcat

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 暗號:博客園.

相關文章
相關標籤/搜索