Skip to content

Instantly share code, notes, and snippets.

@vipulwairagade
Last active February 23, 2023 12:40
Show Gist options
  • Save vipulwairagade/26a18b22720091a31834bc0e871a52ba to your computer and use it in GitHub Desktop.
Save vipulwairagade/26a18b22720091a31834bc0e871a52ba to your computer and use it in GitHub Desktop.
post-receive hook for node.js app on vps
#!/bin/bash
set -eu
PROJECT_REPO="/home/ubuntu/<project-folder-name>/<project-name>/deploy.git"
WORKING_DIR="/home/ubuntu/<project-folder-name>/<project-name>/workingdir"
while read oldrev newrev ref
do
echo "Ref $ref received. Deploying $newrev ..."
git --work-tree=${WORKING_DIR} --git-dir=${PROJECT_REPO} checkout -f $newrev
done
#
# NodeJS hook additions
#
NODE_APP_NAME="<project-name>"
cd ${WORKING_DIR}; npm ci --ignore-scripts; npm run migrate:up;
if [[ "$(pm2 id "${NODE_APP_NAME}")" = "[]" ]]; then
pm2 start npm --name "${NODE_APP_NAME}" -- start
else
pm2 restart "${NODE_APP_NAME}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment