Skip to content

Instantly share code, notes, and snippets.

@1mike12
Last active March 8, 2023 18:08
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1mike12/1dc54ded1a399fbd150e5ecc151ea528 to your computer and use it in GitHub Desktop.
Save 1mike12/1dc54ded1a399fbd150e5ecc151ea528 to your computer and use it in GitHub Desktop.
node and react git post-receive hook deployment

server

  1. make a project/ directory and project.git/ directory
  2. cd project.git && git init --bare
  3. cd project.git/hooks && touch post-receive
  4. copy paste post-receive script
  5. make executable chmod +x post-receive

local

git remote add production user@domain.com:/var/www/project.git

git push production

#!/bin/bash
TRAGET="/var/www/project"
SERVER="/var/www/project/server"
CLIENT="/var/www/project/reactcode"
GIT_DIR="/var/repos/project.git"
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
echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
git --work-tree=$TRAGET --git-dir=$GIT_DIR checkout -f
echo "installing react modules"
cd $CLIENT
npm install
echo "building client"
npm run build
cd $SERVER
echo "installing server modules"
npm install
echo "Running knex migration"
npx knex migrate:latest
echo "restarting PM2"
pm2 restart server
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