简体   繁体   English

从 Jenkins 中的 gitlab webhook 读取 json 负载

[英]Read json payload from gitlab webhook in Jenkins

I followed this tutorial to setup a Jenkins job to run whenever a push is made to the gitlab repository.我按照本教程设置了一个 Jenkins 作业,以便在推送到 gitlab 存储库时运行。 I tested the webhook and I can see that the job is triggered.我测试了 webhook,我可以看到作业被触发。 However, I don't see anything in the payload.但是,我在有效负载中没有看到任何内容。

Just wondering, if anyone has ever tried to read the payload received from gitlab webhook?只是想知道,是否有人曾尝试读取从 gitlab webhook 收到的有效负载?

Jenkins Gitlab Plugin sends these POST parameters to Jenkins whenever any event occurs in the Gitlab repo.每当 Gitlab 存储库中发生任何事件时, Jenkins Gitlab 插件都会将这些 POST 参数发送给 Jenkins。

You can add env in the Jenkins console to get what all Gitlab parameters are exported to the Jenkins environment.您可以在 Jenkins 控制台中添加env以获取所有 Gitlab 参数导出到 Jenkins 环境的内容。 Then you can print or use the required variables.然后您可以打印或使用所需的变量。

eg例如

echo $gitlabSourceRepoURL
echo $gitlabAfter
echo $gitlabTargetBranch
echo $gitlabSourceRepoHttpUrl
echo $gitlabMergeRequestLastCommit
echo $gitlabSourceRepoSshUrl
echo $gitlabSourceRepoHomepage
echo $gitlabBranch
echo $gitlabSourceBranch
echo $gitlabUserEmail
echo $gitlabBefore
echo $gitlabSourceRepoName
echo $gitlabSourceNamespace
echo $gitlabUserName

The tutorial you have mentioned talks about GitHub webhooks.您提到的教程讨论了 GitHub webhooks。 GitLab and GitHub are two separate products. GitLab 和 GitHub 是两个独立的产品。 So, the documentation or links for GitHub webhooks will not apply to GitLab webhooks.因此,GitHub webhooks 的文档或链接不适用于 GitLab webhooks。

GitLab invokes the webhook URL with a JSON payload in the request body that carries a lot of information about the GitLab event that led to the webhook invocation. GitLab 使用请求正文中的 JSON 有效负载调用 Webhook URL,该有效负载包含有关导致 Webhook 调用的 GitLab 事件的大量信息。 For example, the GitLab webhook push event payload carries the following information in it:例如, GitLab webhook 推送事件负载中携带以下信息:

{
  "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": "john@example.com",
  "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":"git@example.com: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":"git@example.com:mike/diaspora.git",
    "ssh_url":"git@example.com:mike/diaspora.git",
    "http_url":"http://example.com/mike/diaspora.git"
  },
  "repository":{
    "name": "Diaspora",
    "url": "git@example.com:mike/diaspora.git",
    "description": "",
    "homepage": "http://example.com/mike/diaspora",
    "git_http_url":"http://example.com/mike/diaspora.git",
    "git_ssh_url":"git@example.com: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": "jordi@softcatala.org"
      },
      "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
}

The Jenkins GitLab plugin makes this webhook payload information available in the Jenkins Global Variable env . Jenkins GitLab 插件使这个 webhook 有效负载信息在Jenkins 全局变量env 中可用。 The available env variables are as follows:可用的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

Just as you would read Jenkins job parameters from Jenkins Global Variable params in your job pipeline script, you could read webhook payload fields from Jenkins Global Variable env :就像您从作业管道脚本中的Jenkins 全局变量参数读取 Jenkins 作业参数一样,您可以从Jenkins 全局变量env读取 webhook 有效负载字段:

echo "My Jenkins job parameter is ${params.MY_PARAM_NAME}"
echo "One of Jenkins job webhook payload field is ${env.gitlabTargetBranch}"

Hope, the above information helps solve your problem.希望以上信息能帮助您解决问题。

Yes, I did it.我做到了。 And it works for some scenarios.它适用于某些场景。

If you use /gitlab/buildnow, you can have access to payload objects.如果您使用 /gitlab/buildnow,则可以访问有效负载对象。 All of them.他们都。 But you have to name them under "this build is parametrized".但是您必须将它们命名为“此构建已参数化”。 Then you can access them by name, like ${AUTHOR_NAME}.然后您可以按名称访问它们,例如 ${AUTHOR_NAME}。

Doc: https://github.com/elvanja/jenkins-gitlab-hook-plugin#parameterized-projects文档: https : //github.com/elvanja/jenkins-gitlab-hook-plugin#parameterized-projects

But please note that if you use /gitlab/notifycommit, it will not work, since there is a gap (the poll) between triggering jenkins, and actually starting the job.但请注意,如果您使用 /gitlab/notifycommit,它将不起作用,因为在触发 jenkins 和实际开始工作之间存在差距(轮询)。 All payload data in this situation is empty.这种情况下的所有有效载荷数据都是空的。

But be carefull to use /gitlab/buildnow, because you cannot control if you want or not to build, like when Maven commit back some files, and build is not supposed to be triggered.但是要小心使用/gitlab/buildnow,因为您无法控制是否要构建,例如Maven何时提交回一些文件,并且不应触发构建。

What I did was to write a little tool in Python to receive all gitlab notification, and this tool talks back to GitLab and Jenkins, to fire (or not) jobs, and collect back statuses.我所做的是用 Python 编写一个小工具来接收所有 gitlab 通知,这个工具会与 GitLab 和 Jenkins 对话,以触发(或不触发)作业,并收集回状态。

My start point: How do I receive Github Webhooks in Python (last answer, not the choosen one).我的起点: 我如何在 Python 中接收 Github Webhooks (最后一个答案,而不是选择的答案)。

I started developing it 2 days ago.我两天前开始开发它。 It's done, but I am still validating it.它已经完成,但我仍在验证它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM