Skip to content

Instantly share code, notes, and snippets.

@zamicol
Last active July 22, 2022 21:47
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save zamicol/f160d05dd22c9eb25031 to your computer and use it in GitHub Desktop.
Save zamicol/f160d05dd22c9eb25031 to your computer and use it in GitHub Desktop.
Git push to prod
#!/bin/sh
# Use git to push to deploy (aka git push to prod)
# Use 'git push remote master' to push to deploy
# This script only deploys on pushes to master.
#
# HOWTO:
# On your server to deploy, create a bare git directory
# (somewhere like /var/git/<gitProject>)
# Your depoly directory (like /var/www) should be somewhere other than your git repo.
#
# git --bare init
#
# Put this script into <gitProject>/hooks/post-receive on the deploy server
# Make sure this script has execute permissions (chmod +x post-receive)
# Then add this remote repo as a remote in git.
#
# git remote add deployServer ssh://example.com/var/git/<gitProject>
#
# Then use ssh to push to production.
#
# git push deployServer master
#
# You can create a group (like "git"), make it owner of this file
# Then add every user that needs to push to the that group.
# See http://security.stackexchange.com/a/72951/17878 for more background.
echo 'start repo to prod'
PROJPATH="PushDirectoryLocationHere"
GITPATH="LocationOfYourBareGitDirectoryHere"
OWNER="OwnerOfTheProject example: www:www"
function push(){
git --work-tree=$PROJPATH --git-dir=$GITPATH checkout -f master
find $PROJPATH/* -type d -exec chmod 775 {} \;
find $PROJPATH/* -type f -exec chmod 664 {} \;
chown -R $OWNER $PROJPATH/*
echo 'push complete'
}
#If executed from shell, push latest master commit regardless.
#This allows you to re-push by entering "./post-receive" from the command line.
if [ -t 0 ]; then
push
else
#Determine the branch, only push to prod on master.
while read oldrev newrev refname
do
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
if [ "master" == "$branch" ]; then
echo 'Master branch. Pushing to prod'
push
else
echo 'Not on master. Not pushing to prod'
fi
done
fi
echo 'End repo to prod'
@borgogelli
Copy link

What is the purpose of this script ? Should I put the script on my private git server or on the hosting server (where I pull the code with the git client) ? What is not clear to me, it's your definition of production server.
Regards.

@zamicol
Copy link
Author

zamicol commented Nov 10, 2016

Hi @borgogelli. I've updated the gist with a better description. Let me know if this answers your question.

Copy link

ghost commented Sep 28, 2017

Does this still work? I know now that the file is named ‘push-update’ instead of ‘push-receive’

@zamicol
Copy link
Author

zamicol commented Jul 22, 2022

Yes, it should be working.

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