項(xiàng)目中有個(gè)需求,即希望每次 gitlab 項(xiàng)目提交 tag 的時(shí)候可以自動(dòng)調(diào)用一下自己服務(wù)端接口,從而拿到最新的 tag info 及 tag message,然后保存到數(shù)據(jù)庫(kù)。
很久很久以前不知道有鉤子(webhook)方法,然后使用了很笨的方法,即每次通過 projectId 循環(huán)遍歷拿到 tagList,項(xiàng)目多的時(shí)候,每個(gè)項(xiàng)目又 tag 多的時(shí)候,就造成接口訪問非常慢,隨著數(shù)據(jù)量越來(lái)越大,到后來(lái)接口越來(lái)越慢,終于忍無(wú)可忍了。
今天使用 webhook 鉤子方法獲取每次更新的 tag 即其 tag message,一下解決了一個(gè)困擾已久的問題。
為了以后查看方便,也為了方便他人,特此記錄一下。
關(guān)于 webhooks 的介紹,最好當(dāng)然是看 webhooks 官方文檔 了。
測(cè)試的時(shí)候,在 gitlab 網(wǎng)站 創(chuàng)建項(xiàng)目,當(dāng)然也可以在自己的 gitlab 私服項(xiàng)目,在用戶設(shè)置頁(yè)面先設(shè)置 personal access token(Secret token),然后在項(xiàng)目或者組織下設(shè)置 webhooks url:

這里的服務(wù)地址(webhook url )填寫的是 webhook.site 這個(gè)網(wǎng)站生成的 url :
https://webhook.site/#!/709611b5-6a83-423e-9063-a6354ee94837/20f932f1-2e7a-4a10-8bb1-4ccd3fb17a24/1
本來(lái)想使用自己的 Java 后臺(tái)地址:
http://192.168.1.108:8881/gitlabTagListWebhook
結(jié)果報(bào)錯(cuò):
Url is blocked: Requests to the local network are not allowed
說不支持 local network,估計(jì)要使用域名形式。
因?yàn)橘~號(hào)權(quán)限不夠,就沒有繼續(xù)倒騰 local network。
好在發(fā)現(xiàn)有第三方的 webhooks 服務(wù)。最終使用 webhook.site 這個(gè)網(wǎng)站。
webhooks url 設(shè)置好后,當(dāng)項(xiàng)目有 push 或者新增 tag 等操作,gitlab 就會(huì)回調(diào)我們?cè)O(shè)置的這個(gè) webhook url.
一個(gè) add tag 操作后,服務(wù)端接受到的回調(diào)參數(shù)如下:
{
"object_kind": "tag_push",
"event_name": "tag_push",
"before": "0000000000000000000000000000000000000000",
"after": "d8e9ef35379b022edef590becdba7cbf7aaade5b",
"ref": "refs/tags/tagTest",
"checkout_sha": "bcdfbfd57c8f3cd6cd65998464bb71a562d49948",
"message": "tagtest_00001",
"user_id": 11586018,
"user_name": "JoeyChang",
"user_username": "iJoeychang",
"user_email": "",
"user_avatar": "https://gitlab.com/uploads/-/system/user/avatar/11586018/avatar.png",
"project_id": 36166737,
"project": {
"id": 36166737,
"name": "testDemo",
"description": "test demo",
"web_url": "https://gitlab.com/iJoeychang/testdemo",
"avatar_url": null,
"git_ssh_url": "git@gitlab.com:iJoeychang/testdemo.git",
"git_http_url": "https://gitlab.com/iJoeychang/testdemo.git",
"namespace": "JoeyChang",
"visibility_level": 20,
"path_with_namespace": "iJoeychang/testdemo",
"default_branch": "master",
"ci_config_path": "",
"homepage": "https://gitlab.com/iJoeychang/testdemo",
"url": "git@gitlab.com:iJoeychang/testdemo.git",
"ssh_url": "git@gitlab.com:iJoeychang/testdemo.git",
"http_url": "https://gitlab.com/iJoeychang/testdemo.git"
},
"commits": [
{
"id": "bcdfbfd57c8f3cd6cd65998464bb71a562d49948",
"message": "Initial template creation\n",
"title": "Initial template creation",
"timestamp": "2019-03-06T09:52:24+01:00",
"url": "https://gitlab.com/iJoeychang/testdemo/-/commit/bcdfbfd57c8f3cd6cd65998464bb71a562d49948",
"author": {
"name": "GitLab",
"email": "root@localhost"
},
"added": [
".gitignore",
".mvn/wrapper/maven-wrapper.jar",
".mvn/wrapper/maven-wrapper.properties",
"Dockerfile",
"README.md",
"mvnw",
"mvnw.cmd",
"pom.xml",
"src/main/java/com/example/demo/DemoApplication.java",
"src/main/resources/application.properties",
"src/test/java/com/example/demo/DemoApplicationTests.java"
],
"modified": [],
"removed": []
}
],
"total_commits_count": 1,
"push_options": {},
"repository": {
"name": "testDemo",
"url": "git@gitlab.com:iJoeychang/testdemo.git",
"description": "test demo",
"homepage": "https://gitlab.com/iJoeychang/testdemo",
"git_http_url": "https://gitlab.com/iJoeychang/testdemo.git",
"git_ssh_url": "git@gitlab.com:iJoeychang/testdemo.git",
"visibility_level": 20
}
}
參考: