自动化部署之jenkins自动触发构建和发布

本文介绍如何通过Jenkins配合GitLab CI实现自动化构建与部署流程,包括安装GitLab插件、配置GitLab认证、设置Webhook以及构建发布PHP项目等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路图:

image.png

一、下载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填写用户账号

image.png

gitlab生成Api token,将生成的token填入上面的证书内。

image.png

image.png


三、选择连接

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

1、填写连接名

2、填写gitlab访问URL

3、选择gitlab认证

3、测试连接

image.png

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

image.png

五、任务配置

在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

image.png

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

填写在上图生成的链接:

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

Secret Token:3f199086a22c54957579966e34ad120a

点击Add webhook

image.png

测试是否生效:点击test-->选择push event会跳转到Hook excuted successfully:HTTP 200

image.png

image.png

六、构建发布PHP项目

在linux-node1上安装nginx,初始同步了线上代码,写入一个index.html。如图:

1
2
3
4
5
6
7
[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 >

image.png

模拟程序员在linux-node2上进行修改代码,并提交到gitlab的master分支上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[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
  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

image.png

image.png

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




本文转自 IT_外卖小哥  51CTO博客,原文链接:http://blog.51cto.com/jinlong/2053863
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值