Created
October 8, 2022 08:17
-
-
Save Suvink/c8722dca2cc2cd95ef3ec75a7b1b614c to your computer and use it in GitHub Desktop.
Git auto deploy script for Express JS with PM2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"apps": [ | |
{ | |
"name": "Sample App Name", | |
"script": "index.js", | |
"watch": "./", | |
"log_date_format": "YYYY-MM-DD HH:mm Z" | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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