This small node.js server allow to execute commands on a machine (deployment scripts for example) when a GitLab webhook is triggered (push, issue events, etc..).
The server use a basic configuration file which defines which command should be executed on specific event of a specific repository. The setup time is around 15 minutes.
Node.js (v6 or greater)
- Download the content of the current repository
- Execute the following command in the downloaded folder :
npm install
- Create the file "config.json" and fill it with your configuration (see the server configuration part below)
- If needed, open the port set in the configuration in your firewall in order to allow http request on the server
- Execute the following command in the downloaded folder :
node server.js
(you can also use a process manager like pm2) - Add the URL of the current server in your gitlab configuration (see the gitlab configuration part below)
Example of a configuration file :
{
"port": 9000,
"logfile": "/home/user/gitlab-hook-server.log",
"hooks": [
{
"repository": "my-gitlab-repository",
"event": "push",
"token": "my-gitlab-token",
"command": "sh /home/user/deploy.sh 'firstParam' '${after}' '${repository.name}' --user='${user_name}'"
}
]
}
- port: The port on which the server is gonna be started
- logfile: The file in which the logs will be written. Can be a relative path (from the server folder) or an absolute path. (optional. if null, the logs will be displayed in the console)
- hooks: List of hooks that you want to bind
- repository: Name of the repository on which the command must be bound (optional. if null, the command will be executed on events of all repositories.
- event: Name of the event on which the command will be executed (see the possible values on the gitlab documentation) (optional. if null, the command will be executed on all events)
- token: The gitlab token define in the gitlab configuration (see gitlab configuration part below) (optional)
- command: The command to execute when the hook is triggered.
${tokens}
will be replaced by values from the request body sent by Gitlab.
- Open the repository that you want to bind in the gitlab website
- Go to "Settings", and then "Integrations"
- In the URL field, type the address of the hook server :
http://<server-name>:<port>/event
- Set a token if needed
- Select the event that you want to trigger
- Click on "Add webhook"
- Once the operation done, click on the "Test" button to check if the binding is working