AutoDeploy your projects using Github WebHooks

amitchambial

Amit Chambial

Posted on June 30, 2020

AutoDeploy your projects using Github WebHooks

Hey Everyone,

Many of us has this problem of deploying our github repo to EC2 machines or Digital Ocean's droplets or etc. We can automate this process by running a simple webhook script on our machine.

Lets start !

The script

const secret = "secret-from-github";
const repo = "path-to-repo-on-machine(eg ~/react-project)";
const http = require('http');
const crypto = require('crypto');
const exec = require('child_process').exec;
const child = require('child_process');
http.createServer(function (req, res) {
        let data= []

        req.on('data', function(chunk) {
                data.push(chunk);
        });

        req.on('end', () => {
                let sig = "sha1=" + crypto.createHmac('sha1', secret).update(data.toString()).digest('hex');
                if (req.headers['x-hub-signature'] == sig) {
                        if(JSON.parse(data).ref==='refs/heads/master'){
                                console.log('Deploying commit - ',JSON.parse(data).head_commit.message)
                                exec('cd ' + repo + ' && git pull origin master && npm install && npm run build && pm2 start npm -- start');
                        }
                }

        })

    res.end();
}).listen(8080);
Enter fullscreen mode Exit fullscreen mode
  • This script is first generating a sha signature using sceret and verifiying the request.
  • If request signature matches our generated signature then we our parsing the payload to JSON.
  • In this script we are receiving all the events that are being generated on our Github repo like commit , pull request, merge.
  • I have a develop branch and a master branch.
  • I am triggering the build when an event occurs on a master branch. if(JSON.parse(data).ref==='refs/heads/master')
  • When we have a merge event to our master branch it will first go to your directory on server and run

šŸ‘‰šŸ» git pull origin master

šŸ‘‰šŸ» npm install

šŸ‘‰šŸ» npm run build

šŸ‘‰šŸ» pm2 start [pm2 is process manager for nodejs. you can replace it with npm start also ]

Note: git pull will ask for username and password.The prompt should not come thats our motive here. You need to either use ssh or use

git config credential.helper store

. Check this out here.I am using credential store as i am the only one accessing that server.

Now start the script using node or pm2.

Configuring the Machine.

If you are using nginx , create a proxy pass for it. You just need to open the port 8080 for communication.

  • NGINX is a better option as you can assign subdomain to it and also ssl certificate.

Configuring Repo on Github

  1. Go to Setting šŸ‘‰šŸ» Webhooks.
  2. Webhook page
  3. After adding the webhook , edit it and enforce ssl for better security.

That's it . Your are done šŸŽ‰.

You can find me on other platforms šŸ‘‡

I am using this method in my latest project https://ismy.institute/

We are participating in YC SUS2020 also.

šŸ’– šŸ’Ŗ šŸ™… šŸš©
amitchambial
Amit Chambial

Posted on June 30, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related