Last active
August 16, 2022 23:00
-
-
Save theatlasroom/b02fa8c6c84d99c24197 to your computer and use it in GitHub Desktop.
Basic post-receive hook - node + 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
#!/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." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The pm2 command should be
restart
instead ofstart
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!