概述
Gitlab触发jenkins并获取项目post参数 jenkins -- Generic Webhook Trigger插件 此插件是git webhook的高阶应用,安装后会暴露出来一个公共APIGitlab触发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","timestamp": "2020-05-28T15:08:47+08:00","url": "http://gitlab.example.com/h_y/hello/-/commit/a49de07609ad97132c0c42aca35c75694ab80085",{
"ID": "a2e3c8d96b30967da1fa9579096d52c2b3757d2a","timestamp": "2020-05-28T15:07:58+08:00","url": "http://gitlab.example.com/h_y/hello/-/commit/a2e3c8d96b30967da1fa9579096d52c2b3757d2a","removed": [
]
}
],"total_commits_count": 3,"push_options": {
},"repository": {
"name": "hello","visibility_level": 0
}
}
创建完成手动触发一次构建生成插件配置文件
gitlb配置
http://jenkinsserver:8080//generic-webhook-trigger/invoke?token=qazwsx
集成测试
总结
以上是编程之家为你收集整理的Gitlab触发jenkins并获取项目post参数 Gitlab触发jenkins并获取项目post参数全部内容,希望文章能够帮你解决Gitlab触发jenkins并获取项目post参数 Gitlab触发jenkins并获取项目post参数所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。