We all might be known for services like Heroku, Netlify, and Vercel. You can deploy your web application from your GitHub repository to all of them. Those services do one thing very well, automate the deployment upon changes on GitHub repository, such as push events. Despite the fact that it comes with an automated deployment feature, those providers charge higher than traditional cloud services like GCP, AWS, or Azure Cloud. In a production environment, you might want to leverage the cloud for the sake of cost efficiency as all of them charge pay-as-you-go. Also, you will have the capability to scale your application. Almost every cloud provider has the function of automating the deployment process so that developers can maintain CI/CD. For instance, Cloud Build of GCP. But It also charges a bit higher and its pricing is based on build minutes. However, if you want to use a compute engine or a EC2 instance for deploying your application to have more control over the server and application then you might continue reading the post.
Prerequisites:
- CPU: 1 or more
- RAM: 1 GB or more
- Operating System: Debian/Ubuntu
Post Contents
Install webhook
sudo apt update && sudo apt install webhook
Create directories and hooks
mkdir -p ~/webhooks/github_repository
nano ~/webhooks/hooks.json
Paste the following into the hooks.json.
[{
"id": "github_repository",
"execute-command": "/root/webhooks/github_repository/deploy.sh",
"command-working-directory": "/root/github_repository/",
"response-message": "Executing deploy script...",
"trigger-rule": {
"match": {
"type": "payload-hash-sha1",
"secret": "PASSWORD",
"parameter": {
"source": "header",
"name": "X-Hub-Signature"
}
}
}
}]
Change the github_repository to your actual GitHub repository name. Save and exit.
Create deploy script
nano ~/webhooks/github_repository/deploy.sh
Paste the following:
#!/bin/bash
git fetch --all
git checkout --force "origin/master"
Create a systemd unit to run webhook in the background.
sudo nano /lib/systemd/system/gitwebhook.service
Paste the following:
[Unit]
Description=Automatically deploy upon push to github repository
After=syslog.target network.target
[Service]
Type=simple
User=YOUR_USERNAME_HERE
WorkingDirectory=/root/project
ExecStart=/usr/bin/webhook -hooks /root/webhooks/hooks.json -ip "0.0.0.0"
Restart=always
[Install]
WantedBy=multi-user.target
Enable the gitwebhook.service
sudo systemctl daemon-reload
sudo systemctl start gitwebhook
sudo systemctl enable gitwebhook
Generate ssh-keys and append public key to your github repository.
Do not use a passphrase while generating ssh-keys. Keep it blank.
Copy the public key using:
cat ~/.ssh/id_rsa.pub
Copy the ssh keys and add the key to the github repository as deploy key.
Create a webhook in github
From your github repository, go to Settings > Webhooks
Click on Add webhook
URL: http://127.0.0.1:9000/hooks/github_repository
Content type: application/json
Secret: Password from the hooks.json file
Note: You need to change 127.0.0.1 to your external IP address of the server and github_repository to your actual github repository name.
Setting up github repository
Clone the repository:
cd /root/
git clone https://github.com/altrgeek/webhook-ci-cd.git
Change the remote origin to SSH
git remote set-url origin [email protected]:altrgeek/webhook-ci-cd.git
verify the origin URL changed to SSH using git remote -v
Test the delivery
Push anything to your github repository, you’ll see a success response in your webhook delivery history: