Github webhook failing

System Information
  • Strapi Version: 4
  • Operating System: Ubuntu 22.04
  • Database: Postgres
  • Node Version: 16.17.0
  • NPM Version: 8.15.0
  • Yarn Version: -

Im following these guides: Digital Ocean and Strapi

I can see the updates are delivered from GitHub, they have a 200. But I’m not seeing the updates pulled to the server.

Im using a digital ocean droplet running Ubuntu 22.04.

If I run the webhook.js file directly with node and then push an update from GitHub it asks me for my GitHub credentials. I’m not sure if this is why it’s failing when running as a service. Do you need to send the credentials if you have a secret set in the webhook?

Response:

root: node webhook.js
Username for 'https://github.com':

webhook.js:

var secret = '12345';
var repo = '~/project';

const http = require('http');
const crypto = require('crypto');
const exec = require('child_process').exec;

const PM2_CMD = 'cd ~ && pm2 startOrRestart ecosystem.config.js';

http
  .createServer(function(req, res) {
    req.on('data', function(chunk) {
      let sig =
        'sha1=' +
        crypto
          .createHmac('sha1', secret)
          .update(chunk.toString())
          .digest('hex');

      if (req.headers['x-hub-signature'] == sig) {
        exec(`cd ${repo} && git pull && ${PM2_CMD}`, (error, stdout, stderr) => {
          if (error) {
            console.error(`exec error: ${error}`);
            return;
          }
          console.log(`stdout: ${stdout}`);
          console.log(`stderr: ${stderr}`);
        });
      }
    });

    res.end();
  })
  .listen(8080);

I encountered the same problem–response 200 but no further action as scripted–too but not asking for GitHub credentials. I invested a couple of hours and figured out that I didn’t set the secret in my github webhook. I made a little bit of workarounds and it works well. I hope my solution helps you:

  1. Set ‘Secret’ with your GitHub personal access token starting with ‘ghp_…’ in your repository’s webhook.

  2. Insert the personal access token in the webhook.js

The below is the code that I implemented in webhooks.js:

var secret = 'ghp_YOUR_PERSONAL_TOKEN; // Your secret key from Settings in GitHub
var repo = '/path/to/your/strapi/project'; // path to the root of your Strapi project on server

const http = require('http');
const crypto = require('crypto');
const exec = require('child_process').exec;

const PM2_CMD = 'sudo pm2 startOrRestart ecosystem.config.js';

http
  .createServer(function(req, res) {
    req.on('data', function(chunk) {
      let sig =
        'sha256=' +
        crypto
          .createHmac('sha256', secret)
          .update(chunk.toString())
          .digest('hex');

      if (req.headers['x-hub-signature-256'] == sig) {
        exec(`cd ${repo} && git pull && ${PM2_CMD}`, (error, stdout, stderr) => {
	  console.log('A Webhook received.')
	  if (error) {
            console.error(`exec error: ${error}`);
            return;
          }
          console.log(`stdout: ${stdout}`);
          console.log(`stderr: ${stderr}`);
        });
      }
    });

    res.end();
  })
  .listen(8565);  //port changed to 8565

I made a minor change in the code such as changing sha1 to sha256, port change and readability.