Skip to content

Instantly share code, notes, and snippets.

@patrobinson
Created May 22, 2017 02:40
Show Gist options
  • Save patrobinson/587f184b7078b40fb635799422360bad to your computer and use it in GitHub Desktop.
Save patrobinson/587f184b7078b40fb635799422360bad to your computer and use it in GitHub Desktop.
Using docker images from s3 for caching
#!/bin/bash -e
# There's an assumption we're two directories into the repo (scripts/buildkite)
basedir=$(dirname $0)
pushd ${basedir}/../../
image_tag=$1
cache_sha_sum=$2
if [[ -z $image_tag || -z $cache_sha_sum ]]; then
echo "Usage: $0 <image tag> <image id>"
exit 1
fi
mkdir -p tmp
tar_file_name="tmp/${image_tag/\//-}-${cache_sha_sum}.tar"
docker save $image_tag > $tar_file_name
gzip $tar_file_name
popd
#!/bin/bash
# I set this bucket up with a lifecycle policy to delete files after 30 days
# This is actually set at a pipeline level, since I use it in many scripts
BUILDKITE_CACHE_BUCKET="my-s3-bucket-used-for-caching"
# There's an assumption we're two directories into the repo (scripts/buildkite)
basedir=$(dirname $0)
pushd ${basedir}/../../
# We sha the docker image based on it's dependencies.
# This is for an NPM app, change out for your relevant dependancy files
cache_sha_sum=$(shasum <(shasum package.json yarn.lock) |cut -f1 -d' ')
image_tag=$1
if [[ -z $image_tag ]]; then
echo "Usage: $0 <image tag>"
exit 1
fi
s3_location="s3://${BUILDKITE_CACHE_BUCKET}/${BUILDKITE_PIPELINE_SLUG}"
cache_file="${image_tag/\//-}-${cache_sha_sum}.tar.gz"
if [[ $(docker images -q $image_tag |wc -l) -gt 0 ]]; then
echo "A cached image already exists, not updating"
elif [[ $(aws s3 ls "${s3_location}/${cache_file}") ]]; then
aws s3 cp "${s3_location}/${cache_file}" tmp/
docker load < tmp/${cache_file}
rm -f tmp/${cache_file}
else
echo "No cache exists, the container will be built from scratch"
fi
popd
#!/bin/bash -e
# I set this bucket up with a lifecycle policy to delete files after 30 days
# This is actually set at a pipeline level, since I use it in many scripts
BUILDKITE_CACHE_BUCKET="my-s3-bucket-used-for-caching"
# There's an assumption we're two directories into the repo (scripts/buildkite)
basedir=$(dirname $0)
pushd ${basedir}/../../
# We sha the docker image based on it's dependencies.
# This is for an NPM app, change out for your relevant dependancy files
cache_sha_sum=$(shasum <(shasum package.json yarn.lock) |cut -f1 -d' ')
image_tag=$1
if [[ -z $image_tag ]]; then
echo "Usage: $0 <image tag>"
exit 1
fi
cache_file="${image_tag/\//-}-${cache_sha_sum}.tar.gz"
s3_location="s3://${BUILDKITE_CACHE_BUCKET}/${BUILDKITE_PIPELINE_SLUG}"
mkdir -p tmp
if aws s3 ls "${s3_location}/${cache_file}"; then
echo "Up to date docker cache already exists"
else
echo "Updating cache"
./scripts/buildkite/create_docker_cache.sh $image_tag $cache_sha_sum
aws s3 cp tmp/${cache_file} "${s3_location}/${cache_file}"
fi
popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment