Jenkins 项目构建之 常用的构建触发器 与gitlab 配置webhook

Jenkins 项目构建之 常用的构建触发器 与gitlab 配置webhook

一:Jenkins 项目构建常用的构建触发器


1.1 触发远程构建

Trigger builds remotely .

通过URL加上相关指令,实现构建。优点在于我们可以使用命令或者代码去执行,去触发构建.


进入项目的配置页面

image-20220214204500640


在构建触发器部分(Build Triggers)选择触发远程构建(Trigger builds remotely (e.g., from scripts))

输入自定义的token ,然后复制给出的URL

image-20220214204834889


修改index.jsp 文件,并提交到gitlab(选做,只是为了看结果)

image-20220214205951125


使用Jenkins的地址+复制的URL+自定义token,触发远程构建

image-20220214210321010


在项目页面,可以看到自动构建了一个新工程

image-20220214210644706


访问tomcat的8080端口,可以看到我们修改的首页内容

image-20220214210529410



1.2 其他工程构建后触发

Build after other projects are build

某些环境的构建,相互之间彼此依赖,所以,有些环境必须先安装。对于大型项目而言,会有多个工程需要构建,此时,就可以编排工程的构建顺序,我们可以安排环境工程先构建。


创建一个pre_project的前置工程

image-20220214211239612

image-20220214211318864


配置项目web_demo_pipeline, 在前置项目pre_project构建后,自动触发构建

image-20220214211442490

image-20220214211552918


修改index.jsp文件,构建前置项目

image-20220214211756375


image-20220214212516067

image-20220214212200282

image-20220214212322983


前置工程pre_project构建成功后,会自动触发web_demo_pipeline 的构建

image-20220214212419686

image-20220214212744594

image-20220214212444949



1.3 定时构建

Build periodically

定时字符串从左往右分别为: 分 时 日 月 周


一些定时表达式的例子:

  • 每30分钟构建一次:H代表形参 H/30 * * * * 10:02 10:32
  • 每2个小时构建一次: H H/2 * * *
  • 每天的8点,12点,22点,一天构建3次: (多个时间点中间用逗号隔开) 0 8,12,22 * * *
  • 每天中午12点定时构建一次 H 12 * * *
  • 每天下午18点定时构建一次 H 18 * * *
  • 在每个小时的前半个小时内的每10分钟 H(0-29)/10 * * * *
  • 每两小时一次,每个工作日上午9点到下午5点(也许是上午10:38,下午12:38,下午2:38,下午
  • 4:38) H H(9-16)/2 * * 1-5

修改项目配置,选择定时构建

image-20220214213101317

image-20220214213624257



1.4轮询SCM

Poll SCM

轮询SCM,是指定时扫描本地代码仓库的代码是否有变更,如果代码有变更就触发项目构建。

Jenkins会定时扫描本地整个项目的代码,增大系统的开销


修改项目的配置,选择轮询SCM

image-20220214214143531


修改代码,提交gitlab

image-20220214213841446


扫描到代码仓库变化,自动构建

image-20220214214541765

image-20220214214608057



二: gitlab 配置webhook 钩子服务


2.1 gitlab 开启 webhook 功能

使用root 账户登录gitlab后台, 点击扳手img模样的Admin Area --->Settings --->Network--->Outbound requests

进入后,再勾选"Allow requests to the local network from web hooks and services"

image-20220214222312808

image-20220214222342135

image-20220214222507654

image-20220214222650237



2.2 jenkins 下载插件,配置构建触发器

Jenkins在下载插件页面,搜索 gitlab,下载 GitLab 和 Generic Webhook Trigger Plugin 插件

image-20220214222841016


Jenkins 配置项目的构建触发器为Build when a change is pushed to GitLab. 并复制后面的URL

image-20220214223326991


2.3 gitlab 在项目中添加webhook

Projects-->Explore projects--->选择项目--->Settings--->Integrations

image-20220214223949282

image-20220214224013797

image-20220214224402782



2.4 进行测试

当前添加webhook页面--->点击Test--->Push evects

image-20220214224640202


2.5 webhook报错解决

如果报错 Hook executed successfully but returned HTTP 403 . 则 webhook服务成功,但是因为权限不够,没有Jenkins的访问权限

image-20220214224900258


在Jenkins页面,Manage Jenkins --> Confifure System--->

image-20220214231821797

去除 Enable authentication for '/project' end-point 的勾选,不用认证,再将下方的Gitlab connections 删除

image-20220214231913466

image-20220214231928168

再回到之前的测试页面,再次进行测试

image-20220214232333077



2.6 进行webhook 构建

更改开发环境代码,提交

image-20220214232447247


Jenkins自动构建并发布

image-20220214232957251

image-20220214233006213

posted @ 2022-02-14 23:38  知己一语  阅读(1441)  评论(0编辑  收藏  举报