Gitlab+Jenkins學習之路(十一)之Jenkins自動觸發構建和發佈

思路圖:

Gitlab+Jenkins學習之路(十一)之Jenkins自動觸發構建和發佈

  • 一、下載gitlab plugin

jenkins-->系統管理-->管理插件-->下載並安裝gitlab plugin

  • 二、配置gitlab認證

路徑:Jenkins-->Credentials-->System-->Global credentials(unrestricted)-->Add Credentials

1、Kind選擇Gitlab API token

2、其中API token填寫gitlab中有庫權限的賬號

3、ID填寫用戶賬號

Gitlab+Jenkins學習之路(十一)之Jenkins自動觸發構建和發佈

gitlab生成Api token,將生成的token填入上面的證書內。

Gitlab+Jenkins學習之路(十一)之Jenkins自動觸發構建和發佈

Gitlab+Jenkins學習之路(十一)之Jenkins自動觸發構建和發佈


  • 三、選擇連接

路徑:系統管理-->系統設置

1、填寫連接名

2、填寫gitlab訪問URL

3、選擇gitlab認證

3、測試連接

Gitlab+Jenkins學習之路(十一)之Jenkins自動觸發構建和發佈

  • 四、配置任務在job配置中選擇gitlab connection:gitlab

Gitlab+Jenkins學習之路(十一)之Jenkins自動觸發構建和發佈

  • 五、任務配置

在job配置界面

1、勾選 Build when a change is pushed to GitLab. GitLab CI Service URL: http://192.168.56.12:8080/project/php-deploy

2、選擇push events 時間觸發構建

3、選擇分支過濾(此處可以根據不同的需求來使用過濾功能)

4、secret token需要填入gitlab項目中的webhook

Gitlab+Jenkins學習之路(十一)之Jenkins自動觸發構建和發佈

在gitlab中找到項目-->setting-->Integrations配置

填寫在上圖生成的鏈接:

URL:http://192.168.56.12:8080/project/php-deploy

Secret Token:3f199086a22c54957579966e34ad120a

點擊Add webhook

Gitlab+Jenkins學習之路(十一)之Jenkins自動觸發構建和發佈

測試是否生效:點擊test-->選擇push event會跳轉到Hook excuted successfully:HTTP 200

Gitlab+Jenkins學習之路(十一)之Jenkins自動觸發構建和發佈

Gitlab+Jenkins學習之路(十一)之Jenkins自動觸發構建和發佈

  • 六、構建發佈PHP項目

在linux-node1上安裝nginx,初始同步了線上代碼,寫入一個index.html。如圖:

<code>[root@linux-node1 ~]# ll /data/www/php-deploy/
total 12
-rw-r--r-- 1 root root 28 Dec 23 15:45 index.html
-rw-r--r-- 1 root root 12 Dec 21 16:11 new.html
-rw-r--r-- 1 root root 19 Dec 21 16:11 readme
[root@linux-node1 ~]# cat /data/www/php-deploy/index.html

welcome to beijing

/<code>
Gitlab+Jenkins學習之路(十一)之Jenkins自動觸發構建和發佈

模擬程序員在linux-node2上進行修改代碼,並提交到gitlab的master分支上

<code>[root@linux-node2 app1]# pwd
/root/php/app1
[root@linux-node2 app1]# echo "Welcome to use Jenkins and Gitlab" > index.html
[root@linux-node2 app1]# git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
#modified: index.html
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@linux-node2 app1]# git add .
[root@linux-node2 app1]# git commit -m "jenkins + gitlab"
[master 7313bdd] jenkins + gitlab
1 file changed, 1 insertion(+), 1 deletion(-)
[root@linux-node2 app1]# git push origin master
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 334 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:java/app1.git
dd37af6..7313bdd master -> master/<file>/<file>/<code>
Gitlab+Jenkins學習之路(十一)之Jenkins自動觸發構建和發佈

Gitlab+Jenkins學習之路(十一)之Jenkins自動觸發構建和發佈

到此,自動觸發和發佈就完成了!這只是一個簡單的構建發佈,作為內部測試使用還是可以的!生產使用,有待優化!


分享到:


相關文章: