0

I'm trying to run git pull, npm run build, and pm2 reload myApp any time something is pushed to my github repo.

I created a server which is connected to github webhooks, and the server runs the following code:

const pullFromMaster = exec(`cd ${path} git pull origin master`, function (err, stdout, stderr) {
    console.log(stdout);
});
pullFromMaster.stdout.pipe(process.stdout);

pullFromMaster.on('end', () => {
    console.log('Finished Pulling from Github')

    const buildNewCode = exec(`${path} npm run build`, function (err, stdout, stderr) {
        console.log(stdout)
    });

    buildNewCode.stdout.pipe(process.stdout);
    buildNewCode.on('end', () => {
        console.log('finished building');
        exec(`pm2 reload ${PM2_IDENTIFIER}`, function (err, stdout, stderr) {
            console.log('finished reloading server');
            console.log(stdout)
        });
    })
})

The problem is the buildNewCode process, doesn't wait for pullFromMaster, and the pm2 reload doesn't wait for buildNewCode. How can I make one wait for the next?

Jessica
  • 9,379
  • 14
  • 65
  • 136

0 Answers0