Skip to content

Instantly share code, notes, and snippets.

@eedwards-sk
Created April 24, 2019 16:29
Show Gist options
  • Save eedwards-sk/4ab13f1e3c50546345af53413e4c5121 to your computer and use it in GitHub Desktop.
Save eedwards-sk/4ab13f1e3c50546345af53413e4c5121 to your computer and use it in GitHub Desktop.
concourse-docker-compose with sidecar image caching
- task: build
image: concourse-docker-compose
privileged: true
input_mapping:
images: app-build
config:
platform: linux
inputs:
- name: source
- name: ecr-credentials
outputs:
- name: app-image
params:
IMAGE_REPO: <sanitized>
APP_IMAGE_NAME: spice/app-builds:latest
BUST_CACHE: false
caches:
- path: images
run:
path: /bin/bash
args:
- -c
- |
set -e
# get workspace dir
WORKSPACE_DIR="$(pwd)"
readonly WORKSPACE_DIR
# set builder name
APP_IMAGE_BUILDER_NAME="spice/app-builder"
APP_IMAGE_BUILDER_TAG="latest"
# export fully qualified image names
export APP_IMAGE="${IMAGE_REPO}/${APP_IMAGE_NAME}"
export APP_IMAGE_BUILDER="${APP_IMAGE_BUILDER_NAME}:${APP_IMAGE_BUILDER_TAG}"
# bust cache
if [[ ${BUST_CACHE} == 'true' ]]
then
rm -fv images/*
fi
# source docker helper lib
source /usr/local/lib/concourse-docker-compose/docker-v1.bash
# start docker engine
echo "starting docker engine"
start_docker
# # list cache
# ls -la images/
# load any cached images
for image in images/*.tar
do
if ! [[ -e ${image} ]]
then
break
fi
echo "loading ${image} from cache"
docker load --input ${image}
done
# login to ecr
cat ecr-credentials/token | docker login --username AWS --password-stdin "${IMAGE_REPO}"
# pull existing images
docker_pull "${APP_IMAGE}" || true
# # list images
# docker images
# enter source dir
pushd source || exit 64
# build images
./ci/scripts/build
# run unit tests
POSTGRES_IMAGE_VERSION="$(cat .postgres-version)"
export POSTGRES_IMAGE="postgres:${POSTGRES_IMAGE_VERSION}"
./ci/scripts/ut
# run integration tests
REDIS_IMAGE_VERSION="$(cat .redis-version)"
export REDIS_IMAGE="redis:${REDIS_IMAGE_VERSION}"
NGINX_IMAGE_VERSION="latest"
export NGINX_IMAGE="nginx:${NGINX_IMAGE_VERSION}"
./ci/scripts/it up && ./ci/scripts/it down
# leave source dir
popd || exit 64
# push images
docker push "${APP_IMAGE}"
# # list images
# docker images
# check for previously cached builder image
cached_builder_image_id=
if [[ -e "images/builder.image-id" ]]
then
cached_builder_image_id=$(cat "images/builder.image-id")
fi
current_builder_image_id=$(image_from_tag "${APP_IMAGE_BUILDER_NAME}" "${APP_IMAGE_BUILDER_TAG}")
if [[ "${cached_builder_image_id}" != "${current_builder_image_id}" ]]
then
echo "saving ${APP_IMAGE_BUILDER} to cache"
docker save --output "images/builder.tar" "${APP_IMAGE_BUILDER}"
echo "${current_builder_image_id}" > "images/builder.image-id"
else
echo "${APP_IMAGE_BUILDER} cache is up-to-date"
fi
# check for previously cached postgres image
cached_postgres_image_id=
if [[ -e "images/postgres.image-id" ]]
then
cached_postgres_image_id=$(cat "images/postgres.image-id")
fi
current_postgres_image_id=$(image_from_tag "postgres" "${POSTGRES_IMAGE_VERSION}")
if [[ "${cached_postgres_image_id}" != "${current_postgres_image_id}" ]]
then
echo "saving postgres:${POSTGRES_IMAGE_VERSION} to cache"
docker save --output "images/postgres.tar" "postgres:${POSTGRES_IMAGE_VERSION}"
echo "${current_postgres_image_id}" > "images/postgres.image-id"
else
echo "postgres:${POSTGRES_IMAGE_VERSION} cache is up-to-date"
fi
# check for previously cached redis image
cached_redis_image_id=
if [[ -e "images/redis.image-id" ]]
then
cached_redis_image_id=$(cat "images/redis.image-id")
fi
current_redis_image_id=$(image_from_tag "redis" "${REDIS_IMAGE_VERSION}")
if [[ "${cached_redis_image_id}" != "${current_redis_image_id}" ]]
then
echo "saving redis:${REDIS_IMAGE_VERSION} to cache"
docker save --output "images/redis.tar" "redis:${REDIS_IMAGE_VERSION}"
echo "${current_redis_image_id}" > "images/redis.image-id"
else
echo "redis:${REDIS_IMAGE_VERSION} cache is up-to-date"
fi
# check for previously cached nginx image
cached_nginx_image_id=
if [[ -e "images/nginx.image-id" ]]
then
cached_nginx_image_id=$(cat "images/nginx.image-id")
fi
current_nginx_image_id=$(image_from_tag "nginx" "${NGINX_IMAGE_VERSION}")
if [[ "${cached_nginx_image_id}" != "${current_nginx_image_id}" ]]
then
echo "saving nginx:${NGINX_IMAGE_VERSION} to cache"
docker save --output "images/nginx.tar" "nginx:${NGINX_IMAGE_VERSION}"
echo "${current_nginx_image_id}" > "images/nginx.image-id"
else
echo "nginx:${NGINX_IMAGE_VERSION} cache is up-to-date"
fi
# stop docker engine
echo "shutting down docker engine"
stop_docker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment