Operations 8 min read

Automate Website Deployment with GitHub Webhooks and Shell Scripts

This guide explains how to use GitHub Webhooks together with a shell deployment script, a Node.js listener, and Nginx reverse proxy to achieve fully automated website deployment, covering setup, code examples, and practical considerations.

Aotu Lab
Aotu Lab
Aotu Lab
Automate Website Deployment with GitHub Webhooks and Shell Scripts

This article demonstrates how to leverage GitHub Webhooks for fully automated website deployment, replacing manual clone‑pull‑restart steps with a streamlined workflow that combines a shell script, a Node.js webhook handler, and Nginx reverse‑proxy configuration.

Why Use GitHub Webhooks?

When a repository on GitHub receives events such as push or pull, a webhook can automatically notify a server, triggering custom actions without manual intervention.

The project must reside on GitHub.

Specific events (e.g., push, pull) are subscribed to.

The webhook fires automatically when the event occurs.

Automation Shell Script

The core deployment logic is placed in auto_build.sh. The script resets the working tree, cleans untracked files, pulls the latest changes, checks out the master branch, and ensures correct ownership.

#! /bin/bash

SITE_PATH='/export/Data/aotu.jd.com/index/cnt1992.github.io'
USER='admin'
USERGROUP='admin'

cd $SITE_PATH
git reset --hard origin/master
git clean -f
git pull
git checkout master
chown -R $USER:$USERGROUP $SITE_PATH

Before the first run, the repository must be cloned manually:

git clone <em>github-remote-repo</em> /export/Data/aotu.jd.com/index/cnt1992.github.io

Server‑Side Webhook Handler

A small Node.js service receives the webhook POST request, verifies the signature, and executes the shell script when a push event arrives. The github-webhook-handler library simplifies this process.

var http = require('http');
var spawn = require('child_process').spawn;
var createHandler = require('github-webhook-handler');

// Path and secret must match the GitHub webhook configuration
var handler = createHandler({ path: '/auto_build', secret: '' });

http.createServer(function (req, res) {
  handler(req, res, function (err) {
    res.statusCode = 404;
    res.end('no such location');
  })
}).listen(6666);

handler.on('error', function (err) {
  console.error('Error:', err.message)
});

// Execute the deployment script on push events
handler.on('push', function (event) {
  console.log('Received a push event for %s to %s',
    event.payload.repository.name,
    event.payload.ref);

  runCommand('sh', ['./auto_build.sh'], function(txt) {
    console.log(txt);
  });
});

function runCommand(cmd, args, callback) {
  var child = spawn(cmd, args);
  var response = '';
  child.stdout.on('data', function(buffer) { response += buffer.toString(); });
  child.stdout.on('end', function() { callback(response); });
}

// Optional: ignore issue events
// handler.on('issues', function (event) { ... });

The service is started with pm2 start index.js so it runs continuously in the background.

Nginx Reverse Proxy

To expose the Node.js listener to the public internet, configure Nginx to proxy requests to the internal port 6666.

server {
    listen 80;
    server_name aotu.jd.com;

    location /auto_build {
        proxy_pass http://127.0.0.1:6666;
    }
}

Configuring the GitHub Webhook

In the GitHub repository settings, navigate to Settings → Webhooks & services , add a new webhook, and provide the Payload URL (e.g., http://aotu.jd.com/auto_build) and the secret that matches the server configuration.

GitHub webhook settings
GitHub webhook settings

Verification

After saving the webhook, push a commit to the repository. GitHub will send a POST request to the server; a successful 200 response indicates the webhook is working.

Webhook delivery log
Webhook delivery log
200 response screenshot
200 response screenshot

Conclusion and Limitations

The presented solution automates website deployment using GitHub Webhooks, a shell script, a Node.js listener, and Nginx. While effective, it relies on a public GitHub repository; sensitive projects may not be suitable for this approach without additional security measures or a private Git server.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Node.jsNGINXGitHubshell scriptWebhooks
Aotu Lab
Written by

Aotu Lab

Aotu Lab, founded in October 2015, is a front-end engineering team serving multi-platform products. The articles in this public account are intended to share and discuss technology, reflecting only the personal views of Aotu Lab members and not the official stance of JD.com Technology.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.