A simple Express.js server that listens for GitHub webhooks and automatically pulls the latest changes and restarts the application using PM2.
- Receives GitHub webhook events for repository pushes (commits)
- Automatically runs
pm2 pullto fetch the latest changes - Restarts the specified PM2 instance using the instance number
- Supports multiple PM2 instances through query parameters
-
Install dependencies:
npm install express body-parser -
Make sure your PM2 applications are set up with Git integration:
pm2 start app.js --name="app" --watch --source-map-support --time -- [args]Or in your ecosystem.config.js:
module.exports = { apps: [{ name: "app", script: "app.js", watch: true, source_map_support: true, time: true, args: "[args]", // Add your repository URL for pm2 pull to work repo: "https://github.com/username/repo.git", // Branch to pull from branch: "main" }] }
-
Start the webhook server:
node index.jsOr with PM2:
pm2 start index.js --name="webhook-server"
- Go to your GitHub repository
- Navigate to Settings > Webhooks
- Click "Add webhook"
- Set the Payload URL to your server's address with the PM2 instance number:
- For instance 0 (default):
http://your-server-address:3000/webhook - For instance 1:
http://your-server-address:3000/webhook?number=1 - For instance 2:
http://your-server-address:3000/webhook?number=2 - And so on for other instances
- For instance 0 (default):
- Set Content type to
application/json - Select "Just the push event" (or choose which events you want to trigger the webhook)
- Ensure "Active" is checked
- Click "Add webhook"
-
To update and restart PM2 instance 0 (default):
POST http://your-server-address:3000/webhook -
To update and restart PM2 instance 1:
POST http://your-server-address:3000/webhook?number=1 -
To update and restart PM2 instance 2:
POST http://your-server-address:3000/webhook?number=2
If the webhook is not working:
- Check the server logs for error messages
- Ensure your server is accessible from the internet
- Verify that the correct PM2 instance number is specified in the webhook URL
- Make sure your PM2 applications have the Git repository configured correctly
- Check GitHub's webhook delivery logs for any failed attempts
This guide walks through setting up an Nginx reverse proxy server listening on port 3003 and forwarding to a service on port 3004.
- Ubuntu/Debian server
- Root or sudo access
- Nginx installed (
sudo apt install nginx)
Create a new configuration file in the Nginx sites directory:
sudo nano /etc/nginx/sites-available/reverse-proxyPaste the following configuration:
server {
listen 0.0.0.0:3000;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
# Optional: Increase timeout if needed
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
send_timeout 90;
}
}Save and exit the editor (Ctrl+X, then Y, then Enter in nano).
Create a symbolic link to enable the site:
sudo ln -s /etc/nginx/sites-available/reverse-proxy /etc/nginx/sites-enabled/Test the configuration for syntax errors:
sudo nginx -tIf the test passes, reload Nginx:
sudo systemctl reload nginxEnsure port 3000 is open in the firewall:
sudo ufw allow 3000/tcp
sudo ufw statusIf using a different firewall or cloud provider security group, make sure to open port 3003 there as well.
From the server, test local access:
curl localhost:3000From another machine, test remote access:
curl http://YOUR_SERVER_IP:3000If you can access the service locally but not remotely:
-
Check firewall settings:
sudo ufw status
-
Verify the server is binding to all interfaces (the
0.0.0.0:3000in the config) -
Check Nginx error logs:
sudo tail -f /var/log/nginx/error.log
-
Ensure your service on port 3000 is running:
curl localhost:3000
-
Check if your cloud provider (AWS, Digital Ocean, etc.) has additional firewall settings that might be blocking the port.
- Replace
http://localhost:3000with the actual service you want to proxy to - Adjust timeout values as needed for your application
- For production environments, consider adding rate limiting and additional security headers