Node JSPowerShell

Auto Deploy Code to servers using GitHub WebHooks and Node JS Microservice

Just to help my team to move code to servers easily I made this solution. Yes, like many organization we do have different environments like TEST,  DEVELOPMENT, STAGE and PRODUCTION and each use different config files and credentials.  So, simply I used webhooks by following below instructions.

  1. Go to your GitHub repo settings.
  2. Choose Hooks from left Pane.
  3. Fill in the Payload URL.
  4. Select Content Type as application/json.
  5. Fill in the Secret Key which is your Personal Access Token.

In my scenario, the solution (code) uses different configurations for each environment and consume different credentials. So, I have created three branches and three Hooks like shown below

  1. Production (http://ProdServer:443/WebHooks/GitHub)
  2. Stage (http://StageServer:443/WebHooks/GitHub)
  3. Development (http://Development:443/WebHooks/GitHub)

Under The Hood, webhooks payload URL will send events when a certain event occurs. We need to create a route with POST method to receive the events from GitHub. Here is the route which calls the scripts to enact the code pull to servers.

var express = require('express');
var spawn = require('child_process').spawn, child;
var app = express();

app.get("/", function (request, response) {
    response.send("Hello World!")
})

app.post("/webhooks/github", function (request, response) {
    spawn('powershell.exe' , ["C:\\Projects\\autoDeploy\\scripts\\deployScript.ps1"])
})

app.listen(443)
console.log("Your application is running on http://localhost:443")

Whenever collaborators push the code to repo its gets pushed to the respective server accordingly. So, I need to create three routes in three servers. The magic happens through the deployment script stored in C:\\Projects\\autoDeploy\\scripts\\deployScript.ps1!

# Choose one per environment
git pull origin production
git pull origin development
git pull origin stage

In next blog I will detail it by adding workflows through servicenow RFC. My sincere thanks to Gaurav Arora for giving me an idea to get started in WebHooks.

One thought on “Auto Deploy Code to servers using GitHub WebHooks and Node JS Microservice

Leave a Reply

Your email address will not be published. Required fields are marked *