Gitlab触发jenkins并获取项目post参数
jenkins -- Generic Webhook Trigger插件
此插件是git webhook的高阶应用,安装后会暴露出来一个公共API,GWT插件接收到 JSON 或 XML 的 HTTP POST 请求后,根据我们配置的规则决定触发哪个Jenkins项目。
定义需要的变量
此插件有两种配置方式
1.图形界面配置-创建流水线任务在触发器中配置(本文不采用此法)
2.pipeline 脚本中配置-注意此方法需要手动触发一次构建任务生成 Generic Webhook Trigger配置
jenkins配置
1.安装插件
勾选Generic Webhook Trigger后,点击【Install without restart】安装插件。
2.创建Jenkins任务
在Jenkins Dashboard中,点击【新建任务】
输入任务名称,选择流水线类型后,点击确定创建任务。
点击刚才创建好的任务。
点击【配置】。
选择【流水线】。
3.pipeline内容
pipeline {
agent any
riggers{
GenericTrigger(
genericVariables:[
[key:'event_name',value:'$.event_name'], //触发动作 pubat or tag_pubat
[key:'user_email',value:'$.user_email'], //GitLab公共邮箱需要自行配置否则获取不到
[key:'project_name',value:'$.project.name'], //项目名称 DevOps_Test
[key:'git_url',value:'$.project.git_http_url'], //git_url http://xxx.xxx.xxx/devops/DevOps_Test.git
[key:'ref',value:'$.ref'], //分支或tag信息
[key:'group_name',value:'$.project.namespace'], //GITLAB_GROUP
[key:'commits_id',value:'$.commits[0].id'] //gitlab commits id
],
token:"qazwsx", //gitlab webhook触发token 多个任务配置同一个token会一起触发
causeString:'Triggered on $ref',
printContributedVariables:true,
printPostContent:true
)
}
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
}
gitlab传递的post数据是json格式
{
"object_kind": "push",
"event_name": "push",
"before": "a2e3c8d96b30967da1fa9579096d52c2b3757d2a",
"after": "9ff547f1010f40c54aefe693d32c026cfc7d8f4d",
"ref": "refs/heads/master",
"checkout_sha": "9ff547f1010f40c54aefe693d32c026cfc7d8f4d",
"message": null,
"user_id": 324,
"user_name": "h_y",
"user_username": "h_y",
"user_email": "",
"user_avatar": null,
"project_id": 3199,
"project": {
"id": 3199,
"name": "hello",
"description": "",
"web_url": "http://gitlab.example.com/h_y/hello",
"avatar_url": null,
"git_ssh_url": "git@gitlab.example.com:h_y/hello.git",
"git_http_url": "http://gitlab.example.com/h_y/hello.git",
"namespace": "h_y",
"visibility_level": 0,
"path_with_namespace": "h_y/hello",
"default_branch": "master",
"ci_config_path": null,
"homepage": "http://gitlab.example.com/h_y/hello",
"url": "git@gitlab.example.com:h_y/hello.git",
"ssh_url": "git@gitlab.example.com:h_y/hello.git",
"http_url": "http://gitlab.example.com/h_y/hello.git"
},
"commits": [
{
"id": "9ff547f1010f40c54aefe693d32c026cfc7d8f4d",
"message": "type\n",
"title": "type",
"timestamp": "2020-05-28T15:09:37+08:00",
"url": "http://gitlab.example.com/h_y/hello/-/commit/9ff547f1010f40c54aefe693d32c026cfc7d8f4d",
"author": {
"name": "h_y",
"email": "h_y@example.com"
},
"added": [
],
"modified": [
"Jenkinsfile"
],
"removed": [
]
},
{
"id": "a49de07609ad97132c0c42aca35c75694ab80085",
"message": "type\n",
"title": "type",
"timestamp": "2020-05-28T15:08:47+08:00",
"url": "http://gitlab.example.com/h_y/hello/-/commit/a49de07609ad97132c0c42aca35c75694ab80085",
"author": {
"name": "h_y",
"email": "h_y@example.com"
},
"added": [
],
"modified": [
"Jenkinsfile"
],
"removed": [
]
},
{
"id": "a2e3c8d96b30967da1fa9579096d52c2b3757d2a",
"message": "type\n",
"title": "type",
"timestamp": "2020-05-28T15:07:58+08:00",
"url": "http://gitlab.example.com/h_y/hello/-/commit/a2e3c8d96b30967da1fa9579096d52c2b3757d2a",
"author": {
"name": "h_y",
"email": "h_y@example.com"
},
"added": [
],
"modified": [
"Jenkinsfile"
],
"removed": [
]
}
],
"total_commits_count": 3,
"push_options": {
},
"repository": {
"name": "hello",
"url": "git@gitlab.example.com:h_y/hello.git",
"description": "",
"homepage": "http://gitlab.example.com/h_y/hello",
"git_http_url": "http://gitlab.example.com/h_y/hello.git",
"git_ssh_url": "git@gitlab.example.com:h_y/hello.git",
"visibility_level": 0
}
}
创建完成手动触发一次构建生成插件配置文件
gitlb配置
http://jenkinsserver:8080//generic-webhook-trigger/invoke?token=qazwsx
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?