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


集成测试

Gitlab触发jenkins并获取项目post参数的更多相关文章
- 持续集成之④:GitLab触发jenkins构建项目
持续集成之④:GitLab触发jenkins构建项目 一:目的为在公司的测试环境当中一旦开发向gitlab仓库提交成功代码,gitlab通知jenkins进行构建项目.代码质量测试然后部署至测试环境, ...
- Jenkins教程(八)实现 GitLab 触发 Jenkins 自动按模块发布前端
楔子 上篇文章解决了提交/合并请求自动触发的需求,但所有前端模块都在同一个代码仓库里,如何获取变更文件路径确定要发布哪个模块呢?本文将带你解决这个问题. 思路 分别解决 3 个问题: 获取变更的文件列 ...
- Jenkins 实现Gitlab事件自动触发Jenkins构建及钉钉消息推送
实现Gitlab事件自动触发Jenkins构建及钉钉消息推送 实践环境 GitLab Community Edition 12.6.4 Jenkins 2.284 Post build task 1. ...
- gitlab与jenkins结合构建持续集成
Jenkins是java编写,需要安装JDK,这里采用 yum 安装,对版本有需求的,可以到 oracle 官网下载 JDK. yum install -y java-1.8.0-openjdk 一. ...
- jenkins发布普通项目、配置自动上线自动部署
1.以root用户运行jenkins是不专业的 刚开始用jenkins时用jenkins这个普通用户运行程序,始终无法连接到gitlab,报错如下: 先是把修改jenkins上的git路径,将git修 ...
- KubeSphere CI/CD+GitLab+Harbor将Spring Boot项目部署至Kubernetes
上一篇文章分享了如何在 KubeSphere 对公共的代码仓库 GitHub 和镜像仓库 DockerHub 创建流水线,本文将继续使用 KubeSphere,基于 Harbor 和 GitLab 创 ...
- Gitlab自动触发Jenkins构建项目
Gitlab自动触发Jenkins构建项目 一.前提 Gitlab已安装配置好. Jenkins已安装Gitlab plugin. 二.配置jenkins中Job 1.勾选触发器下的gitlab触发器 ...
- 如何创建一个项目,让gitlab自动触发jenkins进行构建
前进是:你已经配置好jenkins+gitlab自动化布置了,这里只是常规构建新的项目时,需要做的配置,记录下来,以免忘了又着急 参考这篇博客: https://www.jianshu.com/p/e ...
- 【linux】【jenkins】自动化运维四 整合gitlab、docker发布java项目
jenkins发布java项目 过程参考发布vue项目.https://www.cnblogs.com/jxd283465/p/11543431.html 大同小异. vue建立的是Freestyle ...
随机推荐
- 百万级数据mysql查询优化
一.limit越往后越慢的原因 当我们使用limit来对数据进行分页操作的时,会发现:查看前几页的时候,发现速度非常快,比如 limit 200,25,瞬间就出来了.但是越往后,速度就越慢,特别是百万 ...
- 在写脚本时,在一开始(Shebang 之后)就加上这一句,或者它的缩略版: set -xeuo pipefail
编写可靠 bash 脚本的一些技巧 腾讯技术工程 已认证的官方帐号 1,254 人赞同了该文章 写过很多 bash 脚本的人都知道,bash 的坑不是一般的多. 其实 bash 本身并不是一个 ...
- 记一次MySQL(5.7版本)数据库的主从同步和备份
我遇到的问题 我先后在BAT三大云服务器商购买了学生机,配置如下 百度云2核/4G 阿里云1核/2G 腾讯云1核/2G 我的解决方案 由于我不知道百度云的续费规则,导致买了2核/4G的服务器之后以为像 ...
- Lombok 插件安装和使用
引言 以前的 Java 项目中,充斥着太多毫无技术含量的代码,比如类属性的 getter/setter/toString 方法,还有就是异常处理.I/O 流的关闭操作等.这些样板代码虽然可以通过 ID ...
- Linux中级之keepalived配置
hacmp: ibm的高可用集群软件,并且是商业的(收费),一般用于非x86架构机器当中 AIX,Unix 去IOE:ibm,oracle,emckeepalived: 一款高可用集群软件,利用vrr ...
- Python中字符串转换列表
前言:在使用Python完成工作中一些任务的时候,常常要对数据类型进行转换,比如字符串转列表,列表转字符串,字符串转元组等等,下面分享一下常用的字符串转列表 字符串转列表 第一种字符串转列表: t_s ...
- 西门子 S7-300 以太网模块连接 WINCC方案
北京华科远创科技有限研发的远创智控ETH-YC模块,型号有MPI-ETH-YC01和PPI-ETH-YC01,适用于西门子S7-200/S7-300/S7-400.SMART S7-200.西门子数控 ...
- FreeBSD 13.0 正式版发布,已经可以下载
请访问原文链接:https://sysin.org/article/freebsd-13/,查看最新版.原创作品,转载请保留出处. 根据 FreeBSD 13.0 官方发布计划,4 月 9 日 13. ...
- 『动善时』JMeter基础 — 35、JMeter接口关联【JSON提取器】详解
目录 1.JSON提取器介绍 2.JSON提取器界面详解 3.JSON提取器的使用 (1)测试计划内包含的元件 (2)HTTP Cookie管理器内容 (3)用户登陆请求界面内容 (4)JSON提取器 ...
- Java常用集合笔记
最近事情比较少,闲暇之余温习巩固一下Java的一些基础知识,并做一些笔记, Java常用集合, 主要参考的这篇文章:Java常用集合 ArrayList/Vertor 1. ArrayList 的主要 ...