Skip to content

Instantly share code, notes, and snippets.

@Suvink
Created October 8, 2022 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Suvink/c8722dca2cc2cd95ef3ec75a7b1b614c to your computer and use it in GitHub Desktop.
Save Suvink/c8722dca2cc2cd95ef3ec75a7b1b614c to your computer and use it in GitHub Desktop.
Git auto deploy script for Express JS with PM2
{
"apps": [
{
"name": "Sample App Name",
"script": "index.js",
"watch": "./",
"log_date_format": "YYYY-MM-DD HH:mm Z"
}
]
}
const express = require("express");
const router = express.Router();
const cp = require('child_process');
const gitDir = "/root/apps/app_name";
//Git pull middleware
const doGitPull = (req, res, next) => {
const pull = cp.spawn('git', ['pull'], { cwd: gitDir })
pull.stdout.on('data', d => console.log(d.toString()))
pull.stderr.on('data', e => console.error(e.toString()))
pull.on('error', e => (
console.error(e.toString()),
res.status(500).json({ message: e.toString() })))
pull.on('exit', code => {
if (code == 0) next()
else res.sendStatus(500).json({
message: code.toString(),
})
})
}
router.post("/pull", doGitPull, (req, res) => {
res.status(200).json({
message: "Deployed successfully!",
});
});
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment