为组中的所有项目添加 GitLab Web 挂钩

Add GitLab Web hook for all projects in group

我希望 GitLab 组中的所有项目都共享 webhook 配置:

<MY_JENKINS_INSTANCE>/git/notifyCommit?url=$CHANGED_REPOSITORY

GitLab webhook documentation 表明它应该是可能的:

If you have a big set of projects in the one group then it will be convenient for you to configure web hooks globally for the whole group. You can add the group level web hooks on the group settings page.

虽然我在我的 gitlab 7.0.0 的组设置页面上看不到这样的东西,但这听起来和我想要的一模一样。我无法确定此功能是否比 changelog.

中的功能更新

该功能是否存在?如何使用?

只有企业版才有可能:

In GitLab Enterprise Edition you can configure web hooks globally for the whole group. You can add the group level web hooks on the group settings page Settings > Web Hooks.

根据 的评论,这里有一个使用 GitLab CE API:

的过程

在 GitLab 中拥有或创建一个用户和一个具有 api 范围的个人访问令牌:

  • 用户(右上角头像)> 设置(菜单)> 访问令牌(边栏)
  • 选中 api 范围(复选框)
  • 点击创建个人访问令牌(按钮)
  • 您的新个人访问令牌(文本字段)
  • 中的值

执行 HTTP 请求以获取所有项目:

GET https://gitlab.example.com/api/v4/projects
Private-Token: <my_personal_token>
Accept: application/json

对于响应中的每个项目:

  • id 即下一个请求中要使用的 URL
  • ssh_url_to_repo的值转换成URL编码的
    • 示例:ssh://git@example.com:1234/group/alpha.git 变为 ssh%3A%2F%2Fgit%40example.com%3A1234%2Fgroup%2Falpha.git

对于每个项目,执行一个 HTTP 请求以创建一个挂钩:

POST https://gitlab.example.com/api/v4/projects/<project_ID>/hooks
Private-Token: <my_personal_token>
Content-Type: application/json

{
  "url": "https://jenkins.example.com/git/notifyCommit?url=<encoded_ssh_url>",
  "enable_ssl_verification": true
}

这应该以您选择的语言编写。

不适合作为持久性解决方案,但这可能对寻求一次性更改的人有用(来自 raketasks documentation):

Add a webhook for projects in a given NAMESPACE
# omnibus-gitlab
sudo gitlab-rake gitlab:web_hook:add URL="http://example.com/hook" NAMESPACE=acme
# source installations
bundle exec rake gitlab:web_hook:add URL="http://example.com/hook" NAMESPACE=acme RAILS_ENV=production