全部產品
Search
文件中心

Alibaba Cloud DevOps:Webhooks設定

更新時間:Mar 22, 2025

通過程式碼程式庫Webhook,允許服務端在接收到特殊事件的時候,調用指定的URL觸發期望行為,本文介紹雲效代碼管理的WebHooks 以及對應參數說明。

功能說明

當滿足觸發事件時,代碼管理Codeup會發送一個帶資料的Post請求到Webhook指定的URL。Webhooks 可用於觸發CI構建、執行鏡像備份、觸發部署到生產環境等情境。功能設定如下:

  1. 程式碼程式庫管理員通過左側功能表列單擊設定選擇WebHooks

  2. 單擊建立 WebHook開啟對話方塊。

  3. 填寫事件接收方 URL 地址。

  4. (可選)填寫Secret Token,將在請求的 X-Codeup-Token HTTP頭中發送,用於接收方驗證請求合法性。

  5. 選擇觸發器,包括:推送事件、標籤推送事件、評論事件、合并請求事件。

  6. (可選)填寫描述資訊。

  7. 單擊確定完成建立。

image

事件說明

推送事件

推送事件指用戶端將代碼Push到服務端,有一個例外:Push標籤,不會形成推送事件。

要求標頭

Codeup-Event: Push Hook

請求內容

{
  "object_kind": "push",
  "before": "f2e2d577fab1562a6239b82721fd9827e05fdce6",
  "after": "eb63d0277e64684236ebf8394b919230c4b8a286",
  "ref": "refs/heads/master",
  "user_id": 4,
  "user_name": "Codeup",
  "user_email": "cod***@aliyun.com",
  "project_id": 15,
  "repository": {
    "name": "Diaspora",
    "url": "gi*@codeup.aliyun.com:demo/demo.git",
    "description": "",
    "homepage": "https://codeup.aliyun.com/demo/demo",
    "git_http_url":"https://codeup.aliyun.com/demo/demo.git",
    "git_ssh_url":"gi*@codeup.aliyun.com:demo/demo.git",
    "visibility_level":0
  },
  "commits": [
    {
      "id": "f2e2d577fab1562a6239b82721fd9827e05fdce6",
      "message": "Update readme.",
      "timestamp": "2019-01-01T00:08:00+08:00",
      "url": "https://codeup.aliyun.com/demo/demo/commits/f2e2d577fab1562a6239b82721fd9827e05fdce6",
      "author": {
        "name": "Codeup",
        "email": "cod***@aliyun.com"
      }
    },
    {
      "id": "eb63d0277e64684236ebf8394b919230c4b8a286",
      "message": "fixed readme",
      "timestamp": "2019-01-03T23:36:29+08:00",
      "url": "https://codeup.aliyun.com/demo/demo/commits/eb63d0277e64684236ebf8394b919230c4b8a286",
      "author": {
        "name": "Codeup",
        "email": "cod***@aliyun.com"
      }
    }
  ],
  "total_commits_count": 2

}

標籤推送事件

當建立或者刪除標籤時觸發

要求標頭

Codeup-Event: Tag Push Hook

請求體

{
  "object_kind": "tag_push",
  "ref": "refs/tags/v1.0.0",
  "before": "0000000000000000000000000000000000000000",
  "after": "eb63d0277e64684236ebf8394b919230c4b8a286",
  "user_id": 1,
  "user_name": "Codeup",
  "project_id": 1,
  "repository": {
    "name": "demo",
    "url": "ssh://gi*@codeup.aliyun.com/demo/demo.git",
    "description": "",
    "homepage": "https://codeup.aliyun.com/demo/demo",
    "git_http_url":"https://codeup.aliyun.com/demo/demo.git",
    "git_ssh_url":"gi*@codeup.aliyun.com:demo/demo.git",
    "visibility_level":0
  },
  "commits": [],
  "total_commits_count": 0
}

評論事件

當任何一條評論產生時觸發,無論是在代碼提交還是程式碼檢閱的評論。評論資料會存在object_attributes欄位裡。

提交的評論

要求標頭

Codeup-Event: Note Hook

請求體

{
  "object_kind": "note",
  "user": {
    "name": "Codeup",
    "username": "Codeup",
    "avatar_url": ""
  },
  "project_id": 5,
  "repository": {
    "name": "demo",
    "url": "https://codeup.aliyun.com/demo/demo.git",
    "description": "demo.",
    "homepage": "https://codeup.aliyun.com/demo/demo"
  },
  "object_attributes": {
    "id": 1243,
    "note": "This is a commit comment. How does this work?",
    "noteable_type": "Commit",
    "author_id": 1,
    "created_at": "2019-05-17 18:08:09 UTC",
    "updated_at": "2019-05-17 18:08:09 UTC",
    "project_id": 5,
    "attachment":null,
    "line_code": "bec9703f7a456cd2b4ab5fb3220ae016e3e394e3_0_1",
    "commit_id": "cfe32cf61b73a0d5e9f13e774abde7ff789b1660",
    "noteable_id": null,
    "system": false,
    "st_diff": {
      "diff": "--- /dev/null\n+++ b/six\n@@ -0,0 +1 @@\n+Subproject commit 409f37c4f05865e4fb208c771485f211a22c4c2d\n",
      "new_path": "six",
      "old_path": "six",
      "a_mode": "0",
      "b_mode": "160000",
      "new_file": true,
      "renamed_file": false,
      "deleted_file": false
    },
    "url": "https://codeup.aliyun.com/demo/demo/commits/cfe32cf61b73a0d5e9f13e774abde7ff789b1660#note_1243"
  },
  "commit": {
    "id": "cfe32cf61b73a0d5e9f13e774abde7ff789b1660",
    "message": "Add submodule",
    "timestamp": "2019-02-27T10:06:20+02:00",
    "url": "https://codeup.aliyun.com/demo/demo/commits/cfe32cf61b73a0d5e9f13e774abde7ff789b1660",
    "author": {
      "name": "Demo",
      "email": "de**@gmail.com"
    }
  }
}

合并請求的評論

要求標頭

Codeup-Event: Note Hook

請求體

{
  "commit": null,
  "merge_request": {
    "author_aliyun_pk": "297541067872644100",
    "author_id": 19271,
    "biz_id": "4901786402db48aaab6450b8d12480d2",
    "created_at": "2023-07-10T14:25:30+08:00",
    "description": "",
    "is_update_by_push": false,
    "last_commit": {
      "author": {
        "email": "te**@test.com",
        "name": "test"
      },
      "id": "b4f8b799e9698278efb7d23bfae4fff17252eba3",
      "message": "更新 2.txt",
      "timestamp": "2023-06-13T10:09:53+08:00",
      "url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr/commits/b4f8b799e9698278efb7d23bfae4fff17252eba3"
    },
    "local_id": 247,
    "merge_status": "cannot_be_merged",
    "project_id": 609042,
    "source": {
      "http_url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr.git",
      "name": "test_new_crd",
      "namespace": "6369b7bb801ae2503bb96f8b",
      "ssh_url": "gi*@codeup.aliyun.com:6369b7bb801ae2503bb96f8b/test_new_cr.git",
      "visibility_level": 0,
      "web_url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr"
    },
    "source_branch": "test_xxx",
    "source_project_id": 609042,
    "source_type": "BRANCH",
    "state": "opened",
    "target": {
      "http_url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr.git",
      "name": "test_new_crd",
      "namespace": "6369b7bb801ae2503bb96f8b",
      "ssh_url": "gi*@codeup.aliyun.com:6369b7bb801ae2503bb96f8b/test_new_cr.git",
      "visibility_level": 0,
      "web_url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr"
    },
    "target_branch": "master",
    "target_project_id": 609042,
    "title": "更新 2.txt",
    "updated_at": "2023-07-10T14:25:30+08:00",
    "url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr/change/247",
    "work_in_progress": false
  },
  "object_attributes": {
    "author_aliyun_pk": "297541067872644100",
    "author_id": 19271,
    "closed": 0,
    "commit_id": "b4f8b799e9698278efb7d23bfae4fff17252eba3",
    "created_at": "2023-07-10T14:25:39+08:00",
    "file_path": "1.txt",
    "line_code": "9",
    "note": "xaxas",
    "note_biz_id": "051efb1b3aef435a968bca00e7592c32",
    "note_type": "INLINE_COMMENT",
    "noteable_id": "29bd1475cffd4c1cb1f17f604fc4e104",
    "noteable_type": "MergeRequest",
    "project_id": 609042,
    "related_biz_id": "29bd1475cffd4c1cb1f17f604fc4e104",
    "related_patch_set": {
      "branch": "test_xxx",
      "commit_id": "b4f8b799e9698278efb7d23bfae4fff17252eba3",
      "create_time": "2023-07-10T14:25:30+08:00",
      "mr_biz_id": "4901786402db48aaab6450b8d12480d2",
      "patch_set_biz_id": "29bd1475cffd4c1cb1f17f604fc4e104",
      "patch_set_name": "版本1",
      "version_no": 1
    },
    "related_type": "CODE_REVIEW_PATCH_SET",
    "state": "OPENED",
    "system": false,
    "updated_at": "2023-07-10T14:25:39+08:00",
    "url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr/change/247?comment=051efb1b3aef435a968bca00e7592c32"
  },
  "object_kind": "note",
  "repository": {
    "git_http_url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr.git",
    "git_ssh_url": "gi*@codeup-proxy.aliyun.com:6369b7bb801ae2503bb96f8b/test_new_cr.git",
    "homepage": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr",
    "name": "test_new_crd",
    "url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr.git"
  },
  "user": {
    "aliyun_pk": "297541067872644100",
    "avatar_url": "https://tcs-devops.aliyuncs.com/thumbnail/112n76790347a42173fdca7ace673b123a59/w/100/h/100",
    "extern_uid": "6369b7b14f6a9c17e247d43e",
    "name": "test",
    "username": "test"
  },
  "version": "new"
}

合并請求事件

在建立新的合并請求,或已有合并請求狀態發生更新,或源分支有新的提交時將觸發,事件包括:

  • 建立合并請求。

  • 更新合并請求。

  • 通過合并請求。

  • 合并合并請求。

  • 關閉合并請求。

  • 重新開啟合并請求。

合并請求的事件

要求標頭

Codeup-Event: Merge Request Hook

請求體

{
  "object_attributes": {
    "action": "open",
    "author_aliyun_pk": "297541067872644100",
    "author_id": 19271,
    "biz_id": "4901786402db48aaab6450b8d12480d2",
    "created_at": "2023-07-10T14:25:30+08:00",
    "description": "",
    "is_update_by_push": false,
    "last_commit": {
      "author": {
        "email": "te**@test.com",
        "name": "test"
      },
      "id": "b4f8b799e9698278efb7d23bfae4fff17252eba3",
      "message": "更新 2.txt",
      "timestamp": "2023-06-13T10:09:53+08:00",
      "url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr/commits/b4f8b799e9698278efb7d23bfae4fff17252eba3"
    },
    "local_id": 247,
    "merge_status": "unchecked",
    "project_id": 609042,
    "source": {
      "http_url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr.git",
      "name": "test_new_crd",
      "namespace": "6369b7bb801ae2503bb96f8b",
      "ssh_url": "gi*@codeup.aliyun.com:6369b7bb801ae2503bb96f8b/test_new_cr.git",
      "visibility_level": 0,
      "web_url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr"
    },
    "source_branch": "test_xxx",
    "source_project_id": 609042,
    "source_type": "BRANCH",
    "state": "opened",
    "target": {
      "http_url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr.git",
      "name": "test_new_crd",
      "namespace": "6369b7bb801ae2503bb96f8b",
      "ssh_url": "gi*@codeup.aliyun.com:6369b7bb801ae2503bb96f8b/test_new_cr.git",
      "visibility_level": 0,
      "web_url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr"
    },
    "target_branch": "master",
    "target_project_id": 609042,
    "title": "更新 2.txt",
    "updated_at": "2023-07-10T14:25:30+08:00",
    "url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr/change/247",
    "work_in_progress": false
  },
  "object_kind": "merge_request",
  "repository": {
    "git_http_url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr.git",
    "git_ssh_url": "gi*@codeup.aliyun.com:6369b7bb801ae2503bb96f8b/test_new_cr.git",
    "homepage": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr",
    "name": "test_new_crd",
    "url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr.git"
  },
  "user": {
    "aliyun_pk": "297541067872644100",
    "avatar_url": "https://tcs-devops.aliyuncs.com/thumbnail/112n76790347a42173fdca7ace673b123a59/w/100/h/100",
    "extern_uid": "6369b7b14f6a9c17e247d43e",
    "name": "test",
    "username": "test"
  },
  "version": "new"
}

Webhook 接收端例子

如果出於測試的目的,想查看Webhook的執行效果,可以使用簡單的Echo指令碼。

把下面的指令碼儲存為print_http_body.rb

server = WEBrick::HTTPServer.new(:Port => ARGV.first)
server.mount_proc '/' do |req, res|
  puts req.body
end

trap 'INT' do
  server.shutdown
end
server.start

選個未被使用的連接埠(比如8000),並啟動指令碼:ruby print_http_body.rb 8000然後在Codeup上配置Webhook的URL為http://my.host:8000/

在Codeup上單擊Test Hook,可以在命令列裡看到如下內容:

{"before":"f2e2d577fab1562a6239b82721fd9827e05fdce6","after":"eb63d0277e64684236ebf8394b919230c4b8a286"}
aliyun.com - - [14/May/2019:11:11:11 EDT] "POST / HTTP/1.1" 200 0
- -> /

使用 HTTP Basic Authentication

當觸發支援 HTTP Basic Authentication 的伺服器時, 允許在 URL中添加使用者名稱和密碼進行訪問,Webhook形如:http://userName:password@my.host:8080/project/test-job