Skip to content

Instantly share code, notes, and snippets.

@ashaffer
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashaffer/68f13055e7697d6968c2 to your computer and use it in GitHub Desktop.
Save ashaffer/68f13055e7697d6968c2 to your computer and use it in GitHub Desktop.
#
# Simple post-receive hook that builds a docker image and pushes it somewhere. In order for it to work
# you need to define your docker registry credentials in a .dockerrc file in your home directory:
#
# export DOCKER_USER=<user>
# export DOCKER_PASS=<password>
# export DOCKER_EMAIL=<email>
# export DOCKER_REGISTRY=<registry, e.g. tutum.co>
#
#!/bin/bash
# Source our registry credentials and login
source ~/.dockerrc
sudo docker login -u $DOCKER_USER -p $DOCKER_PASS -e $DOCKER_EMAIL $DOCKER_REGISTRY
# Setup global vars
project_name=$(basename $(pwd))
push_dest=$DOCKER_REGISTRY/$DOCKER_USER/$project_name
while read oldrev newrev refname
do
# Setup branch-local vars
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
tag=$push_dest:$branch
# Create a temporary directory and checkout into it
checkout=$(mktemp -d)
GIT_WORK_TREE=$checkout git checkout -f $branch
# Build and push
echo "--> Building $project_name to $tag"
# Wrap these commands in braces so that the cleanup
# still happens even if the build/push fails
{
sudo docker build -t $tag $checkout
sudo docker push $push_dest
}
# Cleanup after ourselves
rm -rf $checkout
sudo docker rmi $tag
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment