Skip to content

Instantly share code, notes, and snippets.

@richardkmichael
Created January 5, 2015 23:22
Show Gist options
  • Save richardkmichael/9302b92cd955283d6397 to your computer and use it in GitHub Desktop.
Save richardkmichael/9302b92cd955283d6397 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Post-update hook, it receives a list of updated refs on STDIN ($@); including tags.
# -e = exit if any command exits non-zero ; -u exit if undefined variable name ; -o pipefail
set -euo pipefail
GIT_DIR="$( readlink -nf ${PWD} )"
REPO_NAME="$( basename ${GIT_DIR} .git )"
DEPLOY_LOG_DIR="${GIT_DIR}/../deploy-logs"
DEPLOY_LOG="${DEPLOY_LOGS}/${REPO_NAME}.log"
# Ideally, read these from an in-repo config file.
GIT_WORK_TREE="${HOME}/apps/staging/example.com"
DEPLOY_REF="staging"
function log() {
local message="${1}"
echo "$( date ) | ${message}" >> ${DEPLOY_LOG}
}
[[ -d "${GIT_WORK_TREE}" ]] || { log "Missing work-tree: ${GIT_WORK_TREE}" ; exit 1 ; }
log "Repo pushed: $( dirname ${GIT_DIR} )/$( basename ${GIT_DIR} .git )"
# This loop will update the work-tree with the last found ref that matches the $DEPLOY_REF.
for ref in "$@" ; do
log "Updated ref: ${ref}"
# Match refs/heads/foo OR refs/tags/foo
if [[ ${ref} =~ ${DEPLOY_REF}$ ]] ; then
log "Deploying ref: ${ref}"
git --git-dir=${GIT_DIR} --work-tree=${GIT_WORK_TREE} checkout -f ${DEPLOY_REF}
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment