Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add GitLab Web hook for all projects in group

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?

like image 593
Oliver Gondža Avatar asked Jun 25 '15 11:06

Oliver Gondža


People also ask

Can GitLab receive Webhooks?

GitLab Webhooks allows using Webhook notifications for all of the major lifecycle events in the software development process. It can trigger Webhooks for Code Push Events, Issue Changes, Comments, Merge Requests, Pipeline Events, Wiki Page Events, Deployment Events, Release Events, etc.

What is a payload URL in Webhooks?

The payload URL is the URL of the server that will receive the webhook POST requests.


3 Answers

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.

like image 190
cweiske Avatar answered Nov 06 '22 07:11

cweiske


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.

like image 43
Hay Avatar answered Nov 06 '22 08:11

Hay


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
like image 42
Larsen Avatar answered Nov 06 '22 07:11

Larsen