Skip to content

Instantly share code, notes, and snippets.

@santeriv
Last active November 8, 2017 10:25
Show Gist options
  • Save santeriv/a1a90d3c48079d8a272f487e5606c4a1 to your computer and use it in GitHub Desktop.
Save santeriv/a1a90d3c48079d8a272f487e5606c4a1 to your computer and use it in GitHub Desktop.
Deployment Hook example locally

Idea from http://krisjordan.com/essays/setting-up-push-to-deploy-with-git

These are the steps for this setup:

cd
mkdir push-to-deploy
cd push-to-deploy
mkdir {development,remote}
cd remote
git init --bare
cd ..
cd development
git init
echo "Hello, world." >> file.txt
git add file.txt
git commit -m 'First commit.'
git remote add production ../remote
git push production master:master
git push production master:production

Create 'live' repo by cloning it from 'remote'

cd ..
git clone -b production ./remote/ live

Next create the following script and copy it in as bare-repo's hooks/post-receive see below file to copy-paste the content

cd ./remote/hooks
touch post-receive
chmod +x post-receive
nano post-receive

Open new terminal aside and let it watch live repo files to see if they change

watch 'ls -al ~/push-to-deploy/live'

Create a commit with new file to production branch where you actively develop

cd ../../development
touch newfile.txt
git add newfile.txt
git commit -m 'Added new stuff'
git push production master:production

You should see two things. First, you should see "remote: Changes pushed to live". Second, in new terminal with watch you should see that 'live'-folder was updated.

#!/bin/bash
while read oldrev newrev ref
do
branch=`echo $ref | cut -d/ -f3`
if [ "master" == "$branch" ]; then
echo 'Changes pushed master.'
fi
if [ "production" == "$branch" ]; then
pushd ~/push-to-deploy/live
unset GIT_DIR
git pull
popd
echo 'Changes pushed to live'
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment