Gitlab Webhook和Jenkins:没有数据收到

时间:2015-09-22 18:06:06

标签: jenkins gitlab

我一直在关注以下链接,以便使用Web钩子将Gitlab与Jenkins集成。以下所有链接均提及接收JSON'或者有效载荷'或者詹金斯方面的令牌,但是当我尝试在Jenkins配置的Shell脚本中回显或打印这些参数时,我看不到任何内容。

在shell脚本中我有这个,但我从未收到任何有效负载:

  回声"构建工作正常!有效载荷是$ payload"

我确实在/var/log/Jenkins/Jenkins.logs上看到了一些JSON,但是我希望在我的“控制台输出”中看到这些消息,以便我可以使用这些消息从Gitlab进来是否触发构建。

这些链接中的大多数提到的选项都不能通过Gitlab获得。有一篇文章提到将Web钩子格式转换为application / json,但Gitlab UI上没有这样的选项。 How to process a github webhook payload in Jenkins? http://chloky.com/github-json-payload-in-jenkins/

詹金斯设置: Jenkins Settings

Gitlab webhook:

  

http://xx.xx.xx.xxx:8080/job/Interim_Build/buildWithParameters?token=TOKEN_NAME

任何帮助都会很棒。感谢。

2 个答案:

答案 0 :(得分:1)

我建议你试试两个解决方案(两个都适合我):

  1. 使用Go https://github.com/akira/githookproxy中编写的优雅代理从Gitlab webhook转换json数据。 它将接受webhook请求,并将其转换为target_url的请求,格式为:

    • payload:JSON正文
    • START:开始提交哈希
    • END:结束提交哈希
    • REFNAME:参考名称
  2. 使用此Jenkins插件模仿jenkins作为Gitlab CI https://github.com/jenkinsci/gitlab-plugin

  3. 对我来说,最好的是第一个,因为它简单而且更透明。

答案 1 :(得分:0)

GitLab和GitHub是两个单独的产品。因此,您所引用的GitHub Webhooks的文档或links不适用于GitLab Webhooks。

GitLab在请求正文中使用JSON负载调用webhook URL,该负载中包含有关导致Webhook调用的GitLab事件的大量信息。例如,GitLab webhook push event payload中包含以下信息:

{
  "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
}

Jenkins GitLab pluginJenkins Global Variable en 中提供此Webhook有效负载信息。 available env variables如下:

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 Global Variable params 中读取Jenkins作业参数一样,您可以从Jenkins Global Variable env

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

希望,以上信息有助于解决您的问题。