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","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参数全部内容。
原文地址:https://www.cnblogs.com/cndevops
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。