Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #2227 from gabrtv/pre-receive
Browse files Browse the repository at this point in the history
ref(builder): switch to pre-receive hook
  • Loading branch information
Gabriel Monroy committed Oct 23, 2014
2 parents 464f102 + a27a0b3 commit 3dacdcc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 29 deletions.
19 changes: 4 additions & 15 deletions builder/templates/builder
Expand Up @@ -11,13 +11,6 @@ get_app_name() {
echo $1 | awk -F"." '{print $1}'
}

get_git_sha() {
repo=$1
branch=$2
branch_file="${repo}/${branch}"
cat $branch_file
}

indent() {
echo " $@"
}
Expand All @@ -35,7 +28,7 @@ puts-warn() {
}

usage() {
echo "Usage: $0 <user> <repo> <branch>"
echo "Usage: $0 <user> <repo> <sha>"
}

if [ $# -ne $ARGS ]; then
Expand All @@ -45,7 +38,8 @@ fi

USER=$1
REPO=$2
BRANCH=$3
GIT_SHA=$3
SHORT_SHA=${GIT_SHA:0:8}
APP_NAME=$(get_app_name $REPO)

cd $(dirname $0) # ensure we are in the root dir
Expand All @@ -56,10 +50,6 @@ REPO_DIR="${ROOT_DIR}/${REPO}"
BUILD_DIR="${REPO_DIR}/build"
CACHE_DIR="${REPO_DIR}/cache"

# get git sha of branch
GIT_SHA=$(get_git_sha $REPO_DIR $BRANCH)
SHORT_SHA=${GIT_SHA:0:8}

# define image names
IMAGE_NAME="$APP_NAME:git-$SHORT_SHA"
TMP_IMAGE="{{ .deis_registry_host }}:{{ .deis_registry_port }}/$IMAGE_NAME"
Expand All @@ -70,8 +60,7 @@ mkdir -p $BUILD_DIR $CACHE_DIR
TMP_DIR=$(mktemp -d --tmpdir=$BUILD_DIR)

cd $REPO_DIR
# extract git branch
git archive $BRANCH | tar -xmC $TMP_DIR
git archive $GIT_SHA | tar -xmC $TMP_DIR

# switch to app context
cd $TMP_DIR
Expand Down
17 changes: 3 additions & 14 deletions builder/templates/gitreceive
Expand Up @@ -35,37 +35,26 @@ case "$1" in
cat | $SELF pre-receive
EOF
chmod +x $PRERECEIVE_HOOK
POSTRECEIVE_HOOK="$REPO_PATH/hooks/post-receive"
# inject a post-receive hook
cat > $POSTRECEIVE_HOOK <<EOF
#!/bin/bash
cat | $SELF post-receive
EOF
chmod +x $POSTRECEIVE_HOOK
# call the original git-shell
git-shell -c "$SSH_ORIGINAL_COMMAND"
;;

pre-receive)
while read oldrev newrev refname
do
# check for authorization on this repo
$GITHOME/receiver "$RECEIVE_REPO" "$newrev" "$RECEIVE_USER" "$RECEIVE_FINGERPRINT"
rc=$?
if [[ $rc != 0 ]] ; then
echo " ERROR: failed on rev $newrev - push denied"
exit $rc
fi
done
;;

post-receive)
while read oldrev newrev refname
do
# builder assumes that we are running this script from $GITHOME
cd $GITHOME
# if we're processing a receive-pack on an existing repo, run a build
if [[ $SSH_ORIGINAL_COMMAND == git-receive-pack* ]]; then
# SECURITY: git user runs the builder as root (for docker access)
sudo $GITHOME/builder $RECEIVE_USER $RECEIVE_REPO $refname 2>&1 | strip_remote_prefix
sudo $GITHOME/builder $RECEIVE_USER $RECEIVE_REPO $newrev 2>&1 | strip_remote_prefix
fi
done
;;
Expand Down

0 comments on commit 3dacdcc

Please sign in to comment.