Skip to content

Instantly share code, notes, and snippets.

@msveden
Created March 10, 2018 10:13
Show Gist options
  • Save msveden/e304e3033ac58f752d78f8025c7b97f9 to your computer and use it in GitHub Desktop.
Save msveden/e304e3033ac58f752d78f8025c7b97f9 to your computer and use it in GitHub Desktop.
#!/bin/sh
# This script:
# 1. Creates a bare git repo in /opt/[name-of-repo]
# 2. Clones the bare git repo to a "live" location
# 3. Creates a post-receive hook script
#
# Command line usage:
# the-name-of-this-file name-of-repo
REPO=$1
HOOKFILE=/opt/$REPO.git/hooks/post-receive
echo 'hookfile is ' $HOOKFILE
git init --bare /opt/$REPO.git
git clone /opt/$REPO.git /opt/live/$REPO
cat <<EOM >$HOOKFILE
cd /opt/live/$REPO
echo ‘post-receive: git check out…’
git --git-dir=/opt/$REPO.git --work-tree=/opt/live/$REPO checkout master -f
echo ‘post-receive: npm install…’
npm install \
&& echo ‘post-receive: building…’ \
&& npm run build \
&& echo ‘post-receive: → done.’ \
&& (pm2 delete ‘$REPO’ || true) \
&& pm2 start npm --name ‘$REPO’ -- start \
&& echo ‘post-receive: app started successfully with pm2.
EOM
chmod ug+x $HOOKFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment