#!/bin/sh ###################################################################### # # Put this file in; # /usr/share/gitolite/hooks/deployweb # DIR_WWW=/srv/www/ DEPLOY_BRANCH=master TARGET_USER=www for DP_FILE in /srv/gitolite/deploy/* do if [ ! -f "$DP_FILE" ]; then # Nothing to do ;) #echo "Deploy: invalid DP_FILE" exit 1; fi # Get project name PROJECT=$(basename "$DP_FILE") echo "Deploy: PROJECT=${PROJECT}" # Get git repository path and verify if exists DIR_GIT=$(head -n 1 $DP_FILE) if [ ! -d "$DIR_GIT" ]; then echo "Deploy: invalid DIR_GIT: ${DIR_GIT}" exit 2; fi echo "Deploy: DIR_GIT=${DIR_GIT}" # Get directory to deploy and verify if exists GIT_WORK_TREE=${DIR_WWW}${PROJECT}/ if [ ! -d "$GIT_WORK_TREE" ]; then echo "Deploy: invalid GIT_WORK_TREE: ${GIT_WORK_TREE}" echo "Deploy: creating directory: $GIT_WORK_TREE}" mkdir -p $GIT_WORK_TREE fi echo "Deploy: GIT_WORK_TREE={$GIT_WORK_TREE}" # Deploy (checkout) echo "Deploy: starting git checkout" git --git-dir=$DIR_GIT \ --work-tree=$GIT_WORK_TREE \ checkout -f $DEPLOY_BRANCH # Fix ownership and permissions echo "Deploy: fixing permissions" echo "Deploy: setting owner: chown -R ${TARGET_USER}" chown -R ${TARGET_USER}:${TARGET_USER} $GIT_WORK_TREE echo "Deploy: setting directory permissions: chmod 755" find $GIT_WORK_TREE -type d -print0 | xargs -0 chmod 755 echo "Deploy: setting file permissions: chmod 644" find $GIT_WORK_TREE -type f -print0 | xargs -0 chmod 644 # Call project script if [ -f "${GIT_WORK_TREE}/deploy.sh" ]; then echo "Deploy: calling ${GIT_WORK_TREE}deploy.sh" cd ${GIT_WORK_TREE} sudo -u ${TARGET_USER} sh ${GIT_WORK_TREE}deploy.sh fi # Done with project echo "Deploy: removing deploy file="$DP_FILE rm $DP_FILE exit 0; done