Setup Ubuntu Webhook
What is a webhook?
Webhooks are events that trigger an action. In most cases, they are used for communication between systems. This is the simplest way of getting an alert when something happens in another system.
What we can do using webhook on Ubuntu?
Using webhook we can easily run an HTTP server and create endpoints. If the server receives an appropriate request it can run a shell script for the receiving request.
Installation
The installation is very easy, we are going use webhook package for it.
sudo apt-get install webhook
Now go to your home directory and create a new directory called hooks
, then add a new file called hooks.json
mkdir -p ~/hooks && cd ~/hooks
vi hooks.json
The hooks.json
file is your webhook configuration file, here you can define all of your endpoints and their respective shell script file. Here I am giving an example of how I create an endpoint to receive a post request and validate post data.
[
{
"id": "laravel-deploy",
"execute-command": "/home/sourov/hooks/laravel-deploy.sh",
"command-working-directory": "/home/sourov/sites/github-actions",
"trigger-rule": {
"match": {
"type": "value",
"value": "development",
"parameter": {
"source": "payload",
"name": "ref"
}
}
},
"include-command-output-in-response": true,
"include-command-output-in-response-on-error": true
}
]
You can read the documentation to know about the properties and what they can do.
Run webhook server
webhook
command is available to run the webhook server, Here Im logging to my user account so my home directory is /home/sourov
webhook -hooks /home/sourov/hooks/hooks.json -port 8081 -verbose -hotreload
Here I am running the server on 8081 port and enable hot-reload so that if I change the shell script it can re-read the file.
Here we have one problem. We have to run the server in the background. In this article, I am going to show you how we can use Supervisor to run the server in background.
Install Supervisor
Supervisor is a process monitor tool which allow us to manage background process for the Linux operating system.
sudo apt-get install supervisor
After your installation go to /etc/supervisor/conf.d
directory and create a configuration file.
cd /etc/supervisor/conf.d
sudo vi webhook.conf
Here I have given an example supervisor configuration file to run webhook server, you can copy this. Make sure you replace the path of your home directory and username. Also you have to create the log directory by your own, here my log directory is /home/sourov/hooks/logs
[program:webhook]
command=/usr/bin/webhook -hooks /home/sourov/hooks/hooks.json -port 8081 -verbose -hotreload
process_name=%(program_name)s
autostart=true
autorestart=true
user=sourov
stdout_logfile=/home/sourov/hooks/logs/stdout.log
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=5
stdout_capture_maxbytes=1MB
stdout_events_enabled=false
stderr_logfile=/home/sourov/hooks/logs/stderr.log
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=5
stderr_capture_maxbytes=1MB
stderr_events_enabled=false
Running Supervisor
When you have configured everything now this is the time to start supervisor, to do so you have to run three commands.
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start webhook
And if you need to restart any process you can run the following command.
sudo supervisorctl restart webhook