Skip to content

Gitlab:Webhook

This is an easy to use nodeJS based web hook for GitLab.

Category

How to install

npm install gitlabhook -g

How to use

/etc/gitlabhook/gitlabhook-server.js:

#!/usr/bin/env node

var util =  require('util');

var PORT = 8080;

var logger = {
  info:  console.log,
  error: console.log
};

var glh = {
  port: PORT,
  host: '0.0.0.0',
  logger: logger,
  configFile: 'gitlabhook.conf',
  configPathes: ['/etc/gitlabhook/', '/usr/local/etc/gitlabhook/', '.']
};

// With an additional callback function the "gitlabhook.conf" will be ignored.
var server = require('gitlabhook')(glh);
server.listen();
if (server.server) logger.info('webhook server listen (%d)\n', PORT);

/etc/gitlabhook/gitlabhook.conf:

{
  "tasks": {
    "*": [
      "echo 'GitLab Server %s' >> /tmp/gitlabhook.tmp",
      "echo 'Repository: %r' >> /tmp/gitlabhook.tmp",
      "echo 'Event: %k' >> /tmp/gitlabhook.tmp",
      "echo 'User: %u' >> /tmp/gitlabhook.tmp",
      "echo 'Branch: %b' >> /tmp/gitlabhook.tmp",
      "echo 'Git Url: %g' >> /tmp/gitlabhook.tmp",
      "echo 'Last Commit: %i' >> /tmp/gitlabhook.tmp",
      "echo '\tMessage: %m' >> /tmp/gitlabhook.tmp",
      "echo '\tTime: %t' >> /tmp/gitlabhook.tmp",
      "echo $(date) >> /tmp/gitlabhook.tmp",
      "echo '\n' >> /tmp/gitlabhook.tmp"
    ]
  },
  "keep":false
}

Place-holders

The place holders are:

  • %s: GitLab server's IP address
  • %r: name of the repository (e.g. myRepo)
  • %k: kind of event (e.g. tag_push)
  • %u: owner of the repository (user name)
  • %b: branch reference (e.g. refs/heads/master)
  • %g: ssh-based cloning url on the GitLab server (e.g. [email protected]:rolf.niepraschk/myRepo.git)
  • %h: http-based cloning url on the GitLab server (e.g. http://gitlab.host/rolf.niepraschk/myRepo.git)
  • %i: id of the last commit
  • %t: timestamp of the last commit
  • %m: message of the last commit

Log message example

got event on telebot:refs/heads/release from 10.0.0.39


{ object_kind: 'push',
  event_name: 'push',
  before: '0000000000000000000000000000000000000000',
  after: '34e51aba78c51d85dfc8c223379a6cf76a8ecb7e',
  ref: 'refs/heads/release',
  checkout_sha: '34e51aba78c51d85dfc8c223379a6cf76a8ecb7e',
  message: null,
  user_id: 1,
  user_name: 'yourname',
  user_email: '[email protected]',
  user_avatar: 'https://secure.gravatar.com/avatar/f623695a1c380b4a12d1274d0ec8d006?s=80&d=identicon',
  project_id: 10,
  project:
   { name: 'telebot',
     description: 'TELEGRAM BOT PROJECT',
     web_url: 'https://admin.server-project.com/yourname/telebot',
     avatar_url: null,
     git_ssh_url: '[email protected]:yourname/telebot.git',
     git_http_url: 'https://admin.server-project.com/yourname/telebot.git',
     namespace: 'yourname',
     visibility_level: 0,
     path_with_namespace: 'yourname/telebot',
     default_branch: 'master',
     homepage: 'https://admin.server-project.com/yourname/telebot',
     url: '[email protected]:yourname/telebot.git',
     ssh_url: '[email protected]:yourname/telebot.git',
     http_url: 'https://admin.server-project.com/yourname/telebot.git' },
  commits: [ [length]: 0 ],
  total_commits_count: 0,
  repository:
   { name: 'telebot',
     url: '[email protected]:yourname/telebot.git',
     description: 'TELEGRAM BOT PROJECT',
     homepage: 'https://admin.server-project.com/yourname/telebot',
     git_http_url: 'https://admin.server-project.com/yourname/telebot.git',
     git_ssh_url: '[email protected]:yourname/telebot.git',
     visibility_level: 0 } }


cmds: [ 'echo \'GitLab Server 10.0.0.39\' > /tmp/gitlabhook.tmp\necho \'Repository: telebot\' > /tmp/gitlabhook.tmp\necho \'Event: push\' > /tmp/gitlabhook.tmp\necho \'User: yourname\' > /tmp/gitlabhook.tmp\necho \'Branch: refs/heads/release\' > /tmp/gitlabhook.tmp\necho \'Git Url: [email protected]:yourname/telebot.git\' > /tmp/gitlabhook.tmp\necho \'Last Commit: \' > /tmp/gitlabhook.tmp\necho \'\tMessage: \' > /tmp/gitlabhook.tmp\necho \'\tTime: \' > /tmp/gitlabhook.tmp\necho $(date) >> /tmp/gitlabhook.tmp\necho \'\n\' >> /tmp/gitlabhook.tmp\n' ]

Tempdir: /tmp/gitlabhook.116521-7722-1i0rtwd
File created: /tmp/gitlabhook.116521-7722-1i0rtwd/task-000
Executed: /bin/sh /tmp/gitlabhook.116521-7722-1i0rtwd/task-000
Remove working directory: /tmp/gitlabhook.116521-7722-1i0rtwd

github vs gitlab

전달되는 JSON 데이터는 전체적으로 비슷하지만 HEADER 등은 다르므로 주의해야 한다.

Local Download

node-gitlab-hook f82e6d4 on 2016/02/19
Github: rolfn/node-gitlab-hook
Node-gitlab-hook-f82e6d4.zip

See also

Favorite site