Gitlab 到 Jenkins Webhook(带参数)

问题描述 投票:0回答:3

当推送到特定分支时,我正在尝试使用 Gitlab webhook 在我的 jenkins 上构建一个作业。 我遇到的唯一问题是詹金斯作业是参数化的。参数为分支名称。 是否有一个选项可以将分支名称(每次都是不同的分支)添加到 webhook,以便它会自动插入到 jenkins 作业参数中的参数中?

谢谢

jenkins parameters jenkins-plugins gitlab webhooks
3个回答
16
投票

GitLab 使用请求正文中的 JSON 负载来调用 Webhook URL,该负载包含有关导致 Webhook 调用的 GitLab 事件的大量信息。 GitLab push 事件 的 JSON 负载确实包含分支名称信息 - 请参阅 GitLab webhook 推送事件负载中的“ref”字段:

{
  "object_kind": "push",
  "before": "95790bf891e76fee5e1747ab589903a6a1f80f22",
  "after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
  "ref": "refs/heads/master",
  "checkout_sha": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
  "user_id": 4,
  "user_name": "John Smith",
  "user_username": "jsmith",
  "user_email": "[email protected]",
  "user_avatar": "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80",
  "project_id": 15,
  "project":{
    "id": 15,
    "name":"Diaspora",
    "description":"",
    "web_url":"http://example.com/mike/diaspora",
    "avatar_url":null,
    "git_ssh_url":"[email protected]:mike/diaspora.git",
    "git_http_url":"http://example.com/mike/diaspora.git",
    "namespace":"Mike",
    "visibility_level":0,
    "path_with_namespace":"mike/diaspora",
    "default_branch":"master",
    "homepage":"http://example.com/mike/diaspora",
    "url":"[email protected]:mike/diaspora.git",
    "ssh_url":"[email protected]:mike/diaspora.git",
    "http_url":"http://example.com/mike/diaspora.git"
  },
  "repository":{
    "name": "Diaspora",
    "url": "[email protected]:mike/diaspora.git",
    "description": "",
    "homepage": "http://example.com/mike/diaspora",
    "git_http_url":"http://example.com/mike/diaspora.git",
    "git_ssh_url":"[email protected]:mike/diaspora.git",
    "visibility_level":0
  },
  "commits": [
    {
      "id": "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
      "message": "Update Catalan translation to e38cb41.",
      "timestamp": "2011-12-12T14:27:31+02:00",
      "url": "http://example.com/mike/diaspora/commit/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
      "author": {
        "name": "Jordi Mallach",
        "email": "[email protected]"
      },
      "added": ["CHANGELOG"],
      "modified": ["app/controller/application.rb"],
      "removed": []
    },
    {
      "id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
      "message": "fixed readme",
      "timestamp": "2012-01-03T23:36:29+02:00",
      "url": "http://example.com/mike/diaspora/commit/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
      "author": {
        "name": "GitLab dev user",
        "email": "gitlabdev@dv6700.(none)"
      },
      "added": ["CHANGELOG"],
      "modified": ["app/controller/application.rb"],
      "removed": []
    }
  ],
  "total_commits_count": 4
}

您不能将 webhook 负载中的任何字段自动绑定到 Jenkins 作业参数。但是,仍然有一种方法可以访问 Jenkins 作业中的 webhook 有效负载信息。 Jenkins GitLab 插件 使此 Webhook 有效负载信息在 Jenkins 全局变量 env 中可用。 可用的 env 变量如下,它们确实包含分支信息:

gitlabBranch
gitlabSourceBranch
gitlabActionType
gitlabUserName
gitlabUserEmail
gitlabSourceRepoHomepage
gitlabSourceRepoName
gitlabSourceNamespace
gitlabSourceRepoURL
gitlabSourceRepoSshUrl
gitlabSourceRepoHttpUrl
gitlabMergeRequestTitle
gitlabMergeRequestDescription
gitlabMergeRequestId
gitlabMergeRequestIid
gitlabMergeRequestState
gitlabMergedByUser
gitlabMergeRequestAssignee
gitlabMergeRequestLastCommit
gitlabMergeRequestTargetProjectId
gitlabTargetBranch
gitlabTargetRepoName
gitlabTargetNamespace
gitlabTargetRepoSshUrl
gitlabTargetRepoHttpUrl
gitlabBefore
gitlabAfter
gitlabTriggerPhrase

就像您从作业管道脚本中的 Jenkins 全局变量 params 读取 Jenkins 作业参数一样,您可以从 Jenkins 全局变量 env:

读取 webhook 负载字段
params.MY_PARAM_NAME
env.gitlabBranch

希望以上信息可以帮助解决您的问题。


0
投票

您可以使用 Generic Webhook Trigger 插件 来做到这一点。在 GitLab 违规评论 页面中还有一个将该插件与 GitLab 一起使用的示例。

源分支使用以下 JSONPath 进行解析:

$.object_attributes.source_branch

0
投票

谁能告诉我如何配置从 gitlab webhook 获取推送的文件名我想获取修改后的信息

© www.soinside.com 2019 - 2024. All rights reserved.