Last active
June 16, 2020 07:16
-
-
Save jeswinsimon/91457239017cd4c4fdad603f0c442918 to your computer and use it in GitHub Desktop.
post-receive hook for deploying a Node PM2 app to server
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/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