Automated Deployment with Webhooks: A Simplified Solution
Background
As a developer, manually deploying a server after each code push can be a tedious and time-consuming process. In this article, we will explore a more efficient approach using webhooks, a technology that enables real-time notifications and automated deployments.
Understanding Webhooks
A webhook is a web callback or HTTP push API that provides real-time information to applications or other services. Unlike traditional APIs, webhooks do not require polling, making them more efficient and scalable. They are sometimes referred to as “reverse APIs” because they provide an API rule that needs to be designed to use them.
When a webhook is triggered, it sends a notification to the application, typically in the form of a POST request. This allows the application to react to changes in real-time, without having to periodically invoke APIs.
Implementing Webhooks with GitHub
GitHub offers a webhook feature that allows developers to set up automated deployments. Here’s how it works:
- The GitHub webhook service monitors the repository’s state.
- When a local administrator modifies the code and pushes it to the GitHub repository, the webhook service detects the push event.
- The webhook service triggers a script on the VPS (Virtual Private Server) to deploy the code to the specified location (e.g.,
wwwroot).
Building a Webhook Service
To build a webhook service, you’ll need to:
- Download the script:
webhook.jsis a Node.js script that manages multiple projects simultaneously. - Configure the script: Modify the
var handlerconfiguration to match the project name and GitHub project name. - Set up the webhook service: Create a systemd service file (
webhook.service) to manage the webhook service.
Systemd Service File
Here’s an example of a webhook.service file:
[Unit]
Description = Github Webhook
After = network.target
[Service]
Type = simple
User = nginx
Group = nginx
WorkingDirectory = /home/nginx/workspace/gitbook/scripts/
Environment = NODE_PORT = 10080
ExecStart = /home/nginx/.nvm/versions/node/v10.15.3/bin/node /home/nginx/workspace/gitbook/scripts/webhook.js
Restart = on-failure
[Install]
WantedBy = multi-user.target
Script Code
Here’s an example of a script (webhook.js) that deploys the code to the specified location:
#!/bin/bash
WEB_PATH = '/home/nginx/workspace/gitbook/books/'
WEB_USER = 'nginx'
WEB_USERGROUP = 'nginx'
t1 = `date + '%Y-%m-%d %H:%M:%S'`
echo "$t1 Start deployment" >> /home/nginx/workspace/gitbook/scripts/info.log
cd $WEB_PATH
pwdecho "pulling source code ..." git reset --hard origin/master git clean -f git pull
sleep 2
echo "building ..." gitbook build
t2 = `date + '%Y-%m-%d %H:%M:%S'`
if [ $? -eq 0 ]; then
echo "$t2 Finished." >> /home/nginx/workspace/gitbook/scripts/info.log
else
echo "$t2 Error!" >> /home/nginx/workspace/gitbook/scripts/info.log
fi
Configuring GitHub Webhook
To set up the GitHub webhook, follow these steps:
- Go to the project settings.
- Add a webhook.
- Set the Payload URL to the script’s URL (e.g.,
http://domainname:7777/devops_docs). - Set the Content type to
application/jsonorapplication/x-www-form-urlencoded. - Set the Secret to a consistent value with the
webhook.jsscript. - Select the push event to be monitored.
Testing the Automated Deployment
After configuring the GitHub webhook, test the automated deployment by pushing code to the repository. The script should trigger the deployment, and you should see a detailed log of the deployment process.
Conclusion
In this article, we explored the concept of webhooks and how they can be used to automate deployments. We implemented a webhook service using GitHub and a Node.js script, and demonstrated how to configure the webhook service and script. With webhooks, developers can streamline their deployment process and focus on more important tasks.