jenkins -- Generic Webhook Trigger插件

此插件是git webhook的高阶应用,安装后会暴露出来一个公共API,GWT插件接收到 JSON 或 XML 的 HTTP POST 请求后,根据我们配置的规则决定触发哪个Jenkins项目。
定义需要的变量

定义token,此token将用于gitlab的调用

正则匹配,只能匹配到设定的规则还会触发构建,只有满足 production与master分支,且git项目名满足匹配字母或- 大于1个,且触发者(代码提交者)不能是yh,才能触发构建。

在gitlab项目里添加webhook
https://jenkins.example.com/generic-webhook-trigger/invoke?token=xxxxx

点击触发测试

效果,可以成功触发

gitlab传递到jenkins的数据在gitlab的webhook url的编辑里面 view detail,可自行获取想要的变量值

{
  "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
",
      "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
",
      "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
",
      "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
  }
}
原文地址:https://www.cnblogs.com/huandada/p/13067778.html