Webhook を使用すると、特定のイベント (コードのプッシュ、コードレビューなど) が発生したときに、サーバーが指定された URL に POST リクエストを送信できます。このメカニズムは、CI/CD ワークフローのトリガー、イメージのバックアップ、または本番環境へのデプロイの開始に使用できます。
イベントの説明
プッシュイベント
クライアントがサーバーにコードをアップロードすると、プッシュイベントがトリガーされます。
リクエストヘッダー:
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*@cod***.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.", // 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", // 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", // サブプロジェクトのコミット 409f37c4f05865e4fb208c771485f211a22c4c2d
"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リクエスト本文
{
"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": "Aut reprehenderit ut est.", // これはプレースホルダーテキストです。
"homepage": "https://codeup.aliyun.com/demo/demo"
},
"object_attributes": {
"id": 1244,
"note": "This MR needs work.", // この MR は作業が必要です。
"noteable_type": "MergeRequest",
"author_id": 1,
"created_at": "2019-05-17 18:21:36 UTC",
"updated_at": "2019-05-17 18:21:36 UTC",
"project_id": 5,
"attachment": null,
"line_code": null,
"commit_id": "",
"noteable_id": 7,
"system": false,
"st_diff": null,
"url": "https://codeup.aliyun.com/demo/demo/merge_requests/1#note_1244"
},
"merge_request": {
"id": 7,
"target_branch": "markdown",
"source_branch": "master",
"source_project_id": 5,
"author_id": 8,
"assignee_id": 28,
"title": "Tempora et eos debitis quae laborum et.", // プレースホルダーテキスト
"created_at": "2019-03-01 20:12:53 UTC",
"updated_at": "2019-03-21 18:27:27 UTC",
"milestone_id": 11,
"state": "opened",
"merge_status": "cannot_be_merged",
"target_project_id": 5,
"iid": 1,
"description": "Et voluptas corrupti assumenda temporibus. Architecto cum animi eveniet amet asperiores. Vitae numquam voluptate est natus sit et ad id.", // プレースホルダーテキスト
"position": 0,
"locked_at": null,
"source": {
"name": "demo",
"ssh_url": "gi*@codeup.aliyun.com:demo/demo.git",
"http_url": "https://codeup.aliyun.com/demo/demo.git",
"web_url": "https://codeup.aliyun.com/demo/demo",
"namespace": "demo",
"visibility_level": 10
},
"target": {
"name": "demo",
"ssh_url": "gi*@codeup.aliyun.com:demo/demo.git",
"http_url": "https://codeup.aliyun.com/demo/demo.git",
"web_url": "https://codeup.aliyun.com/demo/demo",
"namespace": "demo",
"visibility_level": 10
},
"last_commit": {
"id": "562e173be03b8ff2efb05345d12df18815438a4b",
"message": "Merge branch 'another-branch' into 'master'\n\nCheck in this test\n", // ブランチ 'another-branch' を 'master' にマージ\n\nこのテストをチェックイン
"timestamp": "2019-04-08T21: 00:25-07:00",
"url": "https://codeup.aliyun.com/demo/demo/commits/562e173be03b8ff2efb05345d12df18815438a4b",
"url": "https://codeup.aliyun.com/demo/demo/commits/562e173be03b8ff2efb05345d12df18815438a4b",
"author": {
"name": "Codeup",
"email": "cod***@aliyun.com"
}
},
"work_in_progress": false
}
}
コードレビューイベント
新しいコードレビューリクエストが作成されたとき、既存のコードレビューのステータスが更新されたとき、またはソースブランチに新しいコミットがあるときに、コードレビューイベントがトリガーされます。
リクエストヘッダー:
Codeup-Event: Merge Request Hookリクエスト本文:
{
"object_kind": "merge_request",
"user": {
"name": "Codeup",
"username": "codeup",
"avatar_url": ""
},
"object_attributes": {
"id": 99,
"target_branch": "master",
"source_branch": "ms-viewport",
"source_project_id": 14,
"author_id": 51,
"assignee_id": 6,
"title": "MS-Viewport",
"created_at": "2019-09-03T17:23:00Z",
"updated_at": "2019-09-03T17:23:00Z",
"st_commits": null,
"st_diffs": null,
"milestone_id": null,
"state": "opened",
"merge_status": "unchecked",
"target_project_id": 14,
"iid": 1,
"description": "",
"source": {
"name": "demo",
"ssh_url": "gi*@codeup.aliyun.com:demo/demo.git",
"http_url": "https://codeup.aliyun.com/demo/demo.git",
"web_url": "https://codeup.aliyun.com/demo/demo",
"namespace": "demo",
"visibility_level": 10
},
"target": {
"name": "demo",
"ssh_url": "gi*@codeup.aliyun.com:demo/demo.git",
"http_url": "https://codeup.aliyun.com/demo/demo.git",
"web_url": "https://codeup.aliyun.com/demo/demo",
"namespace": "demo",
"visibility_level": 10
},
"last_commit": {
"id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"message": "fixed readme", // readme を修正しました
"timestamp": "2019-02-03T23:36:29+02:00",
"url": "https://codeup.teambition.com/demo/demo/commits/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"author": {
"name": "Codeup",
"email": "cod***@aliyun.com"
}
},
"work_in_progress": false,
"url": "https://codeup.aliyun.com/demo/demo/merge_requests/1",
"action": "open"
}
}Webhook エンドポイントの例
Webhook の動作をテストして確認する場合は、次のような単純な Echo スクリプトを使用します。
require 'webrick'
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スクリプトを print_http_body.rb として保存し、未使用のポート (たとえば 8000) を選択して、ruby print_http_body.rb 8000 コマンドを実行してスクリプトを起動します。Codeup で Webhook の URL http://my.host:8000/ を指定し、[テストフック] をクリックします。次のようになります。
{"before":"f2e2d577fab1562a6239b82721fd9827e05fdce6","after":"eb63d0277e64684236ebf8394b919230c4b8a286"}
teambition.com - - [14/May/2019:11:11:11 EDT] "POST / HTTP/1.1" 200 0
- -> /ユーザー名とパスワードを含む Webhook URL を使用することで、HTTP ベーシック認証をサポートするサーバーにアクセスできます。URL は http://userName:password@my.host:8080/project/test-job の形式です。