簡體   English   中英

從 Gitlab 負載獲取分支名稱 - Jenkins

[英]Getting branch name from Gitlab payload - Jenkins

我在 Jenkins 中使用參數化作業,我將 GIT-REPO 作為參數發送,並在 Gitlab 存儲庫 webhook 中定義此參數。 例如,如果我創建了名為“test”的 Jenkins 作業,那么我會在 Gitlab 存儲庫中添加以下鈎子:

http://jenkins-server/job/test/buildWithParameters?GITREPO=git@gitlab.com/test-repo.git&SOMEOTHERPARAMETER=somevalue

現在,我想構建觸發鈎子的分支。 我怎樣才能做到這一點? 當我使用單個參數化作業時,Gitlab Hook 插件可能無法正常工作。

GitLab 使用請求正文中的 JSON 有效負載調用 Webhook URL,該有效負載包含有關導致 Webhook 調用的 GitLab 事件的大量信息。 例如, 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
}

因此,您無需向 Webhook URL 添加任何查詢參數(GIT-REPO 或任何其他參數)。 無論您的 Jenkins 作業是否已參數化, Jenkins GitLab 插件都會在Jenkins 全局變量env 中提供此 webhook 有效負載信息。 可用的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 全局變量參數讀取 Jenkins 作業參數一樣,您可以從Jenkins 全局變量env讀取 webhook 有效負載字段:

params.MY_PARAM_NAME
env.gitlabSourceRepoURL

希望以上信息能幫助您解決問題。

詹金斯項目:

將字符串參數定義為 gitlabTargetBranch,您可以在 SCM 中使用 */$gitlabTargetBranch 更新此變量

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM