一、下载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生成Api token,将生成的token填入上面的证书内。

技术分享图片

技术分享图片


三、选择连接

路径:系统管理-->系统设置

1、填写连接名

2、填写gitlab访问URL

3、选择gitlab认证

3、测试连接

技术分享图片

四、配置任务在job配置中选择gitlab connection:gitlab

技术分享图片

五、任务配置

在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中找到项目-->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

技术分享图片

技术分享图片

六、构建发布PHP项目

在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

技术分享图片

技术分享图片

到此,自动触发和发布就完成了!这只是一个简单的构建发布,作为内部测试使用还是可以的!生产使用,有待优化!

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐