Skip to content

Instantly share code, notes, and snippets.

@theatlasroom
Last active August 16, 2022 23:00
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theatlasroom/b02fa8c6c84d99c24197 to your computer and use it in GitHub Desktop.
Save theatlasroom/b02fa8c6c84d99c24197 to your computer and use it in GitHub Desktop.
Basic post-receive hook - node + pm2
#!/bin/sh
# This is for an expressjs node app, it uses npm + bower packages and pm2 to start the app
# [pm2](https://github.com/Unitech/pm2)
# Assumes you've created a [bare git repo](https://git-scm.com/book/en/v2/Git-on-the-Server-Getting-Git-on-a-Server) with on your server with git init --bare
# Adapted from http://javascript.tutorialhorizon.com/2014/08/17/push-to-deploy-a-nodejs-application-using-git-hooks/
PORT=1337
APP_NAME="app-name"
APP_ROOT="/var/www/app"
# Stop the app
echo "Stopping $APP_NAME"
pm2 stop "${APP_NAME}"
# copy the newly pushed code
echo "Checkout the new code"
git --work-tree="$APP_ROOT" --git-dir=/home/git/repos/app.git checkout -f
# install deps
echo "Installing dependencies"
cd "$APP_ROOT"
npm install && bower install
# Restart server
echo "Restarting the app"
PORT="$PORT" pm2 start ./bin/www --name "${APP_NAME}"
echo "Build Succeeded!"
echo "Done. Run 'pm2 ls' on the server to see the process status."
@alielkhateeb
Copy link

The pm2 command should be restart instead of start or you should add -f option.

Cause with pm2 start this error is thrown: [PM2][ERROR] Script already launched, add -f option to force re-execution

Makes sense, but correct me if I'm wrong.

Thank you!

@AnechaS
Copy link

AnechaS commented Feb 20, 2020

great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment