Create a gist now

Instantly share code, notes, and snippets.

Embed
Git post-receive rsync to remote server
#!/bin/bash
REPO=NAME_HERE
# Dir paths on remote server
# These are associated with branches within a git project
LIVE_BRANCH="master"
LIVE="git@host:/var/www/live/"
STAGE_BRANCH="develop"
STAGE="git@host:/var/www/stage/"
if ! [ -t 0 ]; then
read -a ref
fi
# Get branch name from ref head
IFS='/' read -ra REF <<< "${ref[2]}"
branch="${REF[2]}"
# Make tmp dir for extracting files and cleaning up .git (we dont want them on the live site)
tmpdir="/tmp/$REPO/$branch"
mkdir -p $tmpdir
# Assuming git is installed at /home/git/...
git --work-tree=$tmpdir --git-dir="/home/git/repositories/$REPO.git" checkout -f $branch
# If pushing to LIVE_BRANCH, deploy on LIVE
if [ "$LIVE_BRANCH" == "$branch" ]; then
rsync -vzre ssh --delete "$tmpdir/" $LIVE
fi
# If pushing to STAGE_BRANCH, deploy on STAGE
#if [ "$STAGE_BRANCH" == "$branch" ]; then
#rsync -vzre ssh --delete "$tmpdir/" $STAGE
#fi
rm -rf "/tmp/$REPO"
@geekforbrains

This comment has been minimized.

Show comment
Hide comment
@geekforbrains

geekforbrains May 18, 2012

Note that the above requires that you have authorized keys already configured so that one server can rsync to the other via ssh without additional credentials.

Owner

geekforbrains commented May 18, 2012

Note that the above requires that you have authorized keys already configured so that one server can rsync to the other via ssh without additional credentials.

@temgo

This comment has been minimized.

Show comment
Hide comment
@temgo

temgo Feb 4, 2014

Work great for one remote server but can we have another example with multiples remote servers?

For example: deploy from s1 (master) to others servers ss1, ss2, ss3, ss4 etc...

Regards

temgo commented Feb 4, 2014

Work great for one remote server but can we have another example with multiples remote servers?

For example: deploy from s1 (master) to others servers ss1, ss2, ss3, ss4 etc...

Regards

@shankie-san

This comment has been minimized.

Show comment
Hide comment
@shankie-san

shankie-san Aug 28, 2014

Just a quick note to say thank you – this is amazing and saved us much frustration!

Just a quick note to say thank you – this is amazing and saved us much frustration!

@sanderheijselaar

This comment has been minimized.

Show comment
Hide comment
@sanderheijselaar

sanderheijselaar Sep 24, 2014

Thanks. This helped me alot to setup my own deployment script.

Thanks. This helped me alot to setup my own deployment script.

@cliftonc0613

This comment has been minimized.

Show comment
Hide comment
@cliftonc0613

cliftonc0613 Jul 6, 2015

I tried to use the post-receive and it is giving me an error message. It says the last line rm -rf "/tmp/$REPO" unexpected end of file. Im confused where to put the tmp folder. Thanks! below is the error Im getting.

remote: hooks/post-receive: line 69: unexpected EOF while looking for matching `"'
remote: hooks/post-receive: line 70: syntax error: unexpected end of file
To ssh://ctwdsdem@ctwdsdemos.com:/~/public_html/git/ctwdstarter.git

I tried to use the post-receive and it is giving me an error message. It says the last line rm -rf "/tmp/$REPO" unexpected end of file. Im confused where to put the tmp folder. Thanks! below is the error Im getting.

remote: hooks/post-receive: line 69: unexpected EOF while looking for matching `"'
remote: hooks/post-receive: line 70: syntax error: unexpected end of file
To ssh://ctwdsdem@ctwdsdemos.com:/~/public_html/git/ctwdstarter.git

@omidontop

This comment has been minimized.

Show comment
Hide comment
@omidontop

omidontop Jan 7, 2016

That was awesome dude. Thanks for sharing! I modified your script a bit and used it together with GitLab to automatically deploy my web site after every push.

That was awesome dude. Thanks for sharing! I modified your script a bit and used it together with GitLab to automatically deploy my web site after every push.

@rafael-neri

This comment has been minimized.

Show comment
Hide comment
@rafael-neri

rafael-neri Apr 28, 2016

You can use sshpass. You do not need to generate the keys.

Example (change line 29):
sshpass -p "password" rsync -vzre ssh --delete "$tmpdir/" $LIVE

Install sshpass on Ubuntu:
# sudo apt-get install sshpass -y

You can use sshpass. You do not need to generate the keys.

Example (change line 29):
sshpass -p "password" rsync -vzre ssh --delete "$tmpdir/" $LIVE

Install sshpass on Ubuntu:
# sudo apt-get install sshpass -y

@jayralencar

This comment has been minimized.

Show comment
Hide comment
@jayralencar

jayralencar Oct 5, 2016

Hi, can I use it in shared hosting?

Hi, can I use it in shared hosting?

@marcelkorpel

This comment has been minimized.

Show comment
Hide comment
@marcelkorpel

marcelkorpel Mar 13, 2017

@jayralencar Yes, you can, as long as your shared host has Git installed (and you can SSH into your account, of course). I've even seen a page where the user compiled and installed Git in their home directory, but your hosting provider might not like it.

@jayralencar Yes, you can, as long as your shared host has Git installed (and you can SSH into your account, of course). I've even seen a page where the user compiled and installed Git in their home directory, but your hosting provider might not like it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment