Created
June 17, 2020 11:07
-
-
Save jeswinsimon/43b799c45a5adce035f080b07674a7f2 to your computer and use it in GitHub Desktop.
post-receive Git hook for building and serving React application
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="<target-location>" | |
DEPLOY="<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 | |
if [[ $filelist == *"package.json"* || $filelist == *"package-lock.json"* ]]; | |
then | |
echo "Changes detected in Package files..." | |
echo "Reinstalling dependencies..." | |
npm i --only=prod | |
fi | |
echo "Building UI..." | |
npm run build | |
echo "Redeploying UI..." | |
rm -rf $DEPLOY/* | |
cp -r $TARGET/build/* $DEPLOY/ | |
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