jenkins獲取GitLab的hook數據並處理


jenkins獲取GitLab的hook數據並處理

jenkins安裝Generic Webhook Trigger Plugin插件

{% asset_img Generic_Webhook_Trgger.png Generic\ Webhook\ Trgger具體配置 %}

創建任務,在構建觸發器中勾選Generic Webhook Trigger, 在展開的頁面中會有一個URL, 這個URL是配置Webhook使用的,一般形式是http://JENKINS_URL/generic-webhook-trigger/invoke, JENKINS_URL是初始配置jenkins時會有的,一般是http://IP:8080的形式。

如果之前有配置過GitLab webhook, 請去除這個,不然Generic Webhook Trigger不會生效。

如果Git倉庫的驗證使用的是用戶名和密碼,那么就不需要配置Token.

復制這個URL,將JENKINS_URL替換為自己的真實數據,在GitLab的對應的項目的設置-->集成中輸入,沒有安全令牌,勾選自己需要的觸發器事件,SSL證書驗證可以開啟,也可以不開啟,推薦開啟。

任務設置界面還有Post content parameters, Header parameters, Request parameters, 分別是請求的post數據header頭請求頭

一般我們只需要post的數據里的內容就夠了。

圖片的3,4分別是變量名(3)值(4)。值是直接從postjson格式的數據中取出來的,注意,值的獲取方式是JSONPath,並且里面有,如果json數據有多層,那么就多點幾次,比如$.project.name等,可以配置多個變量組。

我們還可以Generic Webhook Trigger配置Optional filter,設置一個觸發事件,比如只有當你推送的分支是develop的時候,才去構建。

Optional filterExpression框中輸入^(refs/heads/develop)$, 在下面的Text框中輸入步驟3的變量名,這里寫$ref

shell中使用變量名的方式也是直接使用,例如echo $ref, 注意沒有

配置完成后,保存,可以點擊剛剛配置的GitLabwebhook頁面的Test對應的事件測試。

GitLabpushhook數據:

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM