gitlab webhook 回调方法使用

5个月前 (12-08) 0 点赞 0 收藏 0 评论 8 已阅读

项目中有个需求,即希望每次 gitlab 项目提交 tag 的时候可以自动调用一下自己服务端接口,从而拿到最新的 tag info 及 tag message,然后保存到数据库。

很久很久以前不知道有钩子(webhook)方法,然后使用了很笨的方法,即每次通过 projectId 循环遍历拿到 tagList,项目多的时候,每个项目又 tag 多的时候,就造成接口访问非常慢,随着数据量越来越大,到后来接口越来越慢,终于忍无可忍了。

今天使用 webhook 钩子方法获取每次更新的 tag 即其 tag message,一下解决了一个困扰已久的问题。

为了以后查看方便,也为了方便他人,特此记录一下。

关于 webhooks 的介绍,最好当然是看 webhooks 官方文档 了。

测试的时候,在 gitlab 网站 创建项目,当然也可以在自己的 gitlab 私服项目,在用户设置页面先设置 personal access token(Secret token),然后在项目或者组织下设置 webhooks url:

这里的服务地址(webhook url )填写的是 webhook.site 这个网站生成的 url :

https://webhook.site/#!/709611b5-6a83-423e-9063-a6354ee94837/20f932f1-2e7a-4a10-8bb1-4ccd3fb17a24/1

本来想使用自己的 Java 后台地址:

http://192.168.1.108:8881/gitlabTagListWebhook

结果报错:

Url is blocked: Requests to the local network are not allowed

说不支持 local network,估计要使用域名形式。

因为账号权限不够,就没有继续倒腾 local network。

好在发现有第三方的 webhooks 服务。最终使用 webhook.site 这个网站。

webhooks url 设置好后,当项目有 push 或者新增 tag 等操作,gitlab 就会回调我们设置的这个 webhook url.

一个 add tag 操作后,服务端接受到的回调参数如下:

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

参考:

webhook.site
GitLab的Webhook配置和开发
webhook之本地网络服务设定


gitlab webhook 回调方法使用

本文收录在
0评论

登录

忘记密码 ?

切换登录

注册