Skip to content

Instantly share code, notes, and snippets.

@jeswinsimon
Created June 17, 2020 11:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeswinsimon/43b799c45a5adce035f080b07674a7f2 to your computer and use it in GitHub Desktop.
Save jeswinsimon/43b799c45a5adce035f080b07674a7f2 to your computer and use it in GitHub Desktop.
post-receive Git hook for building and serving React application
#!/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