思路圖:php
jenkins-->系統管理-->管理插件-->下載並安裝gitlab pluginhtml
路徑:Jenkins-->Credentials-->System-->Global credentials(unrestricted)-->Add Credentialsjava
一、Kind選擇Gitlab API tokennode
二、其中API token填寫gitlab中有庫權限的帳號linux
三、ID填寫用戶帳號nginx
gitlab生成Api token,將生成的token填入上面的證書內。git
路徑:系統管理-->系統設置程序員
一、填寫鏈接名web
二、填寫gitlab訪問URLapp
三、選擇gitlab認證
三、測試鏈接
在job配置界面
一、勾選 Build when a change is pushed to GitLab. GitLab CI Service URL: http://192.168.56.12:8080/project/php-deploy
二、選擇push events 時間觸發構建
三、選擇分支過濾(此處能夠根據不一樣的需求來使用過濾功能)
四、secret token須要填入gitlab項目中的webhook
在gitlab中找到項目-->setting-->Integrations配置
填寫在上圖生成的連接:
URL:http://192.168.56.12:8080/project/php-deploy
Secret Token:3f199086a22c54957579966e34ad120a
點擊Add webhook
測試是否生效:點擊test-->選擇push event會跳轉到Hook excuted successfully:HTTP 200
在linux-node1上安裝nginx,初始同步了線上代碼,寫入一個index.html。如圖:
[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 <h1>welcome to beijing</h1>
模擬程序員在linux-node2上進行修改代碼,並提交到gitlab的master分支上
[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 git@192.168.56.11:java/app1.git dd37af6..7313bdd master -> master
到此,自動觸發和發佈就完成了!這只是一個簡單的構建發佈,做爲內部測試使用仍是能夠的!生產使用,有待優化!