Skip to content

Instantly share code, notes, and snippets.

@jeswinsimon
Last active June 16, 2020 07:16
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 jeswinsimon/91457239017cd4c4fdad603f0c442918 to your computer and use it in GitHub Desktop.
Save jeswinsimon/91457239017cd4c4fdad603f0c442918 to your computer and use it in GitHub Desktop.
post-receive hook for deploying a Node PM2 app to server
#!/bin/bash
TARGET="<deployment-location>"
GIT_DIR="<repo-location>"
BRANCH="master"
while read oldrev newrev ref
do
# only checking out the master (or whatever branch you would like to deploy)
if [ "$ref" = "refs/heads/$BRANCH" ];
then
filelist=$(git diff-tree --no-commit-id --name-only -r $newrev)
echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH
cd $TARGET
# Reinstall dependencies only if there are any changes in package/package-lock files.
if [[ $filelist == *"package.json"* || $filelist == *"package-lock.json"* ]];
then
echo "Changes detected in Package files..."
echo "Reinstalling dependencies..."
npm i --only=prod
fi
echo "Redeploying API Server..."
sudo pm2 restart API
else
echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment