Skip to content

Instantly share code, notes, and snippets.

@Adophilus
Last active September 29, 2022 11:45
Show Gist options
  • Save Adophilus/333c3f7a2866b91eb981534766d9a4a5 to your computer and use it in GitHub Desktop.
Save Adophilus/333c3f7a2866b91eb981534766d9a4a5 to your computer and use it in GitHub Desktop.
A git hook for processing npm projects uploaded to live server using git
#! /usr/bin/env bash
source "~/.bashrc"
PROJECT_NAME="changenow-store"
DEPLOYMENT_PATH="~/tools/express-server/apps/$PROJECT_NAME"
BRANCH="main"
function initNodeApp () {
local folderExisted=$1
local WORKDIR="$PWD"
cd "$DEPLOYMENT_PATH"
pnpm install
if [[ "$folderExisted" == false ]]
then
pnpm setup
pnpm migrations
else
pnpm migrations
fi
cd ../../
pnpm restart
cd "$WORKDIR"
}
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..."
# rm -rf "$DEPLOYMENT_PATH"
folderExisted=true
if [ ! -d "$DEPLOYMENT_PATH" ]
then
mkdir "$DEPLOYMENT_PATH"
folderExisted=false
fi
git --work-tree="$DEPLOYMENT_PATH" --git-dir="$PWD" checkout -f main
if [ -f "$DEPLOYMENT_PATH/package.json" ]
then
initNodeApp $folderExisted
fi
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