Add GitLab Web hook for all projects in group

10.6k Views Asked by At

I would like all my projects in a GitLab group to have shared configuration for a webhook:

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

GitLab webhook documentation suggests it should be possible:

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.

That sound exactly like what I am after though I see no such thing on group settings page in my gitlab 7.0.0. I was not able to find out if this feature is not newer than that in the changelog.

Does the feature exist? How do I use it?

3

There are 3 best solutions below

3
On BEST ANSWER

That's possible in the enterprise version only:

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.

0
On

Following up on @VertigoRay's comments, here's a procedure to do it using GitLab CE API:

Have, or create an user in GitLab and a personal access token with api scope:

  • User (top right avatar) > Settings (menu) > Access tokens (sidebar)
  • Check api scope (checkbox)
  • Click on create personal access token (button)
  • <my_personal_token> is the value in Your New Personal Access Token (text field)

Perform an HTTP request to get all projects:

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

For each project in the response:

  • id which is the <project_ID> to be used in the next request URL
  • Convert the value of ssh_url_to_repo so that it becomes URL encoded <encoded_ssh_url>
    • Example: ssh://[email protected]:1234/group/alpha.git becomes ssh%3A%2F%2Fgit%40example.com%3A1234%2Fgroup%2Falpha.git

For each project, perform an HTTP request to create a hook:

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
}

This should be scripted in the langage of your choice.

0
On

Not suitable as a persistent solution, but this might be useful for someone looking for a one-time change (from the 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