github.com/misfo/deis@v1.0.1-0.20141111224634-e0eee0392b8a/builder/templates/gitreceive (about) 1 #!/bin/bash 2 set -eo pipefail 3 4 # Places cursor at start of line, so that subsequent text replaces existing text. For example; 5 # "remote: Updated branch 'master' of 'repo'. Deploying to dev." becomes 6 # "Updated branch 'master' of 'repo'. Deploying to dev." 7 strip_remote_prefix() { 8 sed -u "s/^/"$'\e[1G'"/" 9 } 10 11 GITUSER=${GITUSER:-git} 12 GITHOME="/home/$GITUSER" 13 SELF=`which $0` 14 15 case "$1" in 16 17 # called by sshd on each `git push` 18 run) 19 export RECEIVE_USER=$2 20 export RECEIVE_FINGERPRINT=$3 21 # ssh provides the original requested command in $SSH_ORIGINAL_COMMAND 22 SSH_ORIGINAL_COMMAND="$(echo $SSH_ORIGINAL_COMMAND | sed 's/\///g' )" # remove any '/'s 23 export RECEIVE_REPO="$(echo $SSH_ORIGINAL_COMMAND | awk '{print $2}' | sed -e 's/'\''//g')" 24 REPO_PATH="$GITHOME/$RECEIVE_REPO" 25 if [ ! -d $REPO_PATH ]; then 26 mkdir -p $REPO_PATH 27 cd $REPO_PATH 28 git init --bare > /dev/null 29 fi 30 cd $GITHOME 31 PRERECEIVE_HOOK="$REPO_PATH/hooks/pre-receive" 32 # inject a pre-receive hook 33 cat > $PRERECEIVE_HOOK <<EOF 34 #!/bin/bash 35 cat | $SELF pre-receive 36 EOF 37 chmod +x $PRERECEIVE_HOOK 38 # call the original git-shell 39 git-shell -c "$SSH_ORIGINAL_COMMAND" 40 ;; 41 42 pre-receive) 43 while read oldrev newrev refname 44 do 45 # check for authorization on this repo 46 $GITHOME/receiver "$RECEIVE_REPO" "$newrev" "$RECEIVE_USER" "$RECEIVE_FINGERPRINT" 47 rc=$? 48 if [[ $rc != 0 ]] ; then 49 echo " ERROR: failed on rev $newrev - push denied" 50 exit $rc 51 fi 52 # builder assumes that we are running this script from $GITHOME 53 cd $GITHOME 54 # if we're processing a receive-pack on an existing repo, run a build 55 if [[ $SSH_ORIGINAL_COMMAND == git-receive-pack* ]]; then 56 # SECURITY: git user runs the builder as root (for docker access) 57 sudo $GITHOME/builder $RECEIVE_USER $RECEIVE_REPO $newrev 2>&1 | strip_remote_prefix 58 fi 59 done 60 ;; 61 62 *) 63 echo "Usage: gitreceive <command> [options]" 64 ;; 65 esac