Skip to content

Instantly share code, notes, and snippets.

@eedwards-sk
Created August 13, 2019 21:06
Show Gist options
  • Save eedwards-sk/8233edabd164c4fbc02e889c7cfb175b to your computer and use it in GitHub Desktop.
Save eedwards-sk/8233edabd164c4fbc02e889c7cfb175b to your computer and use it in GitHub Desktop.
---
platform: linux
image_resource:
type: docker-image
source:
repository: snapkitchen/concourse-docker-compose
tag: 18.06
params:
IMAGE_REPO: foo.io
APP_IMAGE_NAME: foo/app
APP_IMAGE_TAG: latest
ECR_TOKEN:
GIT_COMMIT_SHA:
GIT_COMMIT_MESSAGE:
inputs:
- name: app-source
- name: ecr-credentials
optional: true
- name: git-commit-sha
optional: true
- name: git-commit-message
optional: true
outputs:
- name: app-build
caches:
- path: images
- path: app-build
run:
path: /bin/bash
args:
- -c
- |
set -e
# get workspace dir
WORKSPACE_DIR="$(pwd)"
readonly WORKSPACE_DIR
# get commit sha
if [[ -z "${GIT_COMMIT_SHA}" ]]
then
GIT_COMMIT_SHA="$(cat git-commit-sha/value)"
fi
# get commit message
if [[ -z "${GIT_COMMIT_MESSAGE}" ]]
then
GIT_COMMIT_MESSAGE="$(cat git-commit-message/value)"
fi
# write commit sha to release file
echo "${GIT_COMMIT_SHA}" > app-source/app/RELEASE_VERSION
# set builder name
APP_IMAGE_BUILDER_NAME="${APP_IMAGE_NAME}-builder"
APP_IMAGE_BUILDER_TAG="latest"
# export fully qualified image names
export APP_IMAGE="${IMAGE_REPO}/${APP_IMAGE_NAME}:${APP_IMAGE_TAG}"
export APP_IMAGE_BUILDER="${APP_IMAGE_BUILDER_NAME}:${APP_IMAGE_BUILDER_TAG}"
# source docker helper lib
source /usr/local/lib/concourse-docker-compose/docker-v1.bash
# start docker engine
echo "starting docker engine"
start_docker
# load any cached ancillary images
for image in images/*.tar
do
if ! [[ -e ${image} ]]
then
break
fi
echo "loading ${image} from cache"
docker load --input ${image}
done
# pulling app image before loading the cached image
# because the cached image will often be newer
# on repeat builds of the same pr
# login to ecr
if [[ -z "${ECR_TOKEN}" ]]
then
ECR_TOKEN="$(cat ecr-credentials/token)"
fi
echo "${ECR_TOKEN}" | docker login --username AWS --password-stdin "${IMAGE_REPO}"
# pull latest image
docker_pull "${APP_IMAGE}" || true
# load cached app image
if [[ -e app-build/image ]]
then
echo "loading app-build/image from cache"
docker load --input app-build/image
fi
# enter app-source dir
pushd app-source || exit 64
# build images
ci/scripts/build
# get dependency versions
POSTGRES_IMAGE_VERSION="$(cat .postgres-version)"
export POSTGRES_IMAGE="postgres:${POSTGRES_IMAGE_VERSION}"
NGINX_IMAGE_VERSION="latest"
export NGINX_IMAGE="nginx:${NGINX_IMAGE_VERSION}"
# check for [skip test(s)] in the commit message
if (echo "${GIT_COMMIT_MESSAGE:-}" | grep -i '\[SKIP[-_ ]TEST[S]\{0,1\}\]')
then
echo ">> skipping test"
else
# run test
ci/scripts/test
fi
# check for [skip systest(s)] in the commit message
if (echo "${GIT_COMMIT_MESSAGE:-}" | grep -i '\[SKIP[-_ ]SYSTEST[S]\{0,1\}\]')
then
echo ">> skipping systest"
else
# run system test
ci/scripts/systest
fi
# leave app-source dir
popd || exit 64
# check for previously cached app image
cached_app_image_id=
if [[ -e "app-build/image.image-id" ]]
then
cached_app_image_id=$(cat "app-build/image.image-id")
fi
current_app_image_id=$(image_from_tag "${IMAGE_REPO}/${APP_IMAGE_NAME}" "${APP_IMAGE_TAG}")
echo "comparing ${APP_IMAGE} ${cached_app_image_id} to cached ${current_app_image_id}"
if [[ "${cached_app_image_id}" != "${current_app_image_id}" ]]
then
echo "saving ${APP_IMAGE} to cache"
docker save --output "app-build/image" "${APP_IMAGE}"
echo "${current_app_image_id}" > "images/app.image-id"
else
echo "${APP_IMAGE} cache is up-to-date"
fi
# 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 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