Skip to content

Instantly share code, notes, and snippets.

@daniellawrence
Created January 21, 2015 08:17
Show Gist options
  • Save daniellawrence/5174d159357136239498 to your computer and use it in GitHub Desktop.
Save daniellawrence/5174d159357136239498 to your computer and use it in GitHub Desktop.
docker_ci
#!/bin/bash -e
CONTAINTER_NAME=${1}
# If we are not provided a name to use, then use the name of
# the current directory
if [[ -z ${CONTAINTER_NAME} ]];then
CONTAINER_NAME=${PWD##*/}
fi
CONTAINER_VERSION=${2}
if [[ -z ${CONTAINER_VERSION} ]];then
CONTAINER_VERSION=$(git describe 2> /dev/null)
fi
if [[ -z ${CONTAINER_VERSION} ]];then
CONTAINER_VERSION="local"
fi
if [[ ! -f Dockerfile ]];then
echo "Missing Dockfile from current directory"
exit 1
fi
BASE_IMAGE=$(cat Dockerfile | awk '$1 ~ /^FROM$/ {print $2}')
# ------------------------------------------------------------------------------
# Make sure we have our base image
# ------------------------------------------------------------------------------
function pull_container() {
if [[ -z $1 ]];then
return
fi
_CONTAINER_NAME=""
_CONTAINER_VERSION=""
_CONTAINER_NAME=$(echo $1 | cut -d: -f1)
_CONTAINER_VERSION=$(echo $1 | cut -d: -f2)
_IMAGE_ID=$(docker images \
| awk "\$1 ~ /$_CONTAINER_NAME/ && \$2 ~ /$_CONTAINER_VERSION/ { print \$3}")
if [[ -z ${_IMAGE_ID} ]];then
docker pull ${_CONTAINER_NAME}:${_CONTAINER_VERSION}
fi
}
# ------------------------------------------------------------------------------
# Build the container and make sure its found in the images
# ------------------------------------------------------------------------------
function build_container() {
_CONTAINER_NAME=${1}
_CONTAINER_VERSION=${2}
docker build -t ${_CONTAINER_NAME}:${_CONTAINER_VERSION} .
_IMAGE_ID=$(docker images \
| awk "\$1 ~ /$_CONTAINER_NAME/ && \$2 ~ /$_CONTAINER_VERSION/ { print \$3}")
}
# ------------------------------------------------------------------------------
# Check if the git repo is dirty, changes without commits.
# We use this to flag the container as a dirty container
# ------------------------------------------------------------------------------
function git_is_dirty() {
if [[ ! -z $(git status --porcelain) ]];then
echo "true"
else
echo "false"
fi
}
# ------------------------------------------------------------------------------
# Run the container in the background, then attach to it to see the stdout of the
# container in the hosts' stdout
# ------------------------------------------------------------------------------
function run_container() {
set -x
_CONTAINER_NAME=${1}
_CONTAINER_VERSION=${2}
echo "Starting ${_CONTAINER_NAME}:${_CONTAINER_VERSION} on $(hostname -f)"
_CONTAINER_ID=$(docker run -d ${_CONTAINER_NAME}:${_CONTAINER_VERSION} | cut -c1-12)
if [[ -z ${_CONTAINER_ID} ]];then
echo "CONTAINER(${_CONTAINER_ID}) failed to start as IMAGE(${_CONTAINER_NAME})"
exit 1
fi
_RUNNING_CONTAINER_ID=$(
docker ps \
| awk "\$2 ~ /$_CONTAINER_NAME:$_CONTAINER_VERSION/ { print \$1}"
)
echo $_RUNNING_CONTAINER_ID
echo $_CONTAINER_ID
if [[ ${_RUNNING_CONTAINER_ID} != ${_CONTAINER_ID} ]];then
echo "---- EXIT ---"
echo "CONTAINER ${_CONTAINER_ID} finished too fast"
echo "---- EXIT --- container logs --- "
docker logs ${_CONTAINER_ID}
echo "---- EXIT ---"
exit 1
fi
docker attach ${_CONTAINER_ID}
_CONTAINER_RC=${?}
if [[ ${_CONTAINER_RC} -ne 0 ]];then
echo "---- EXIT --- "
echo "Container ${_CONTAINER_ID} exited with non-zero RC: ${_CONTIANER_RC}"
echo "---- EXIT --- "
exit 1
fi
return ${_CONTAINER_RC}
}
# ------------------------------------------------------------------------------
# MAIN
# ------------------------------------------------------------------------------
if [[ $(git_is_dirty) == "true" ]];then
CONTAINER_VERSION="${CONTAINER_VERSION}-dirty"
fi
pull_container $BASE_IMAGE
build_container $CONTAINER_NAME $CONTAINER_VERSION
run_container $CONTAINER_NAME $CONTAINER_VERSION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment