Last active
August 29, 2015 14:19
-
-
Save ashaffer/68f13055e7697d6968c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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