Last active
August 29, 2015 13:57
-
-
Save csirac2/9613959 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
# `prepare_dir_tarball some_dir` | |
# | |
# Given a directory name in the pwd (with NO trailing slash), create or update | |
# `some_dir.tar.gz` only if the contents of the tarball would actually change. | |
# | |
# Although Dockerfiles allow you to ADD a directory, this isn't cacheable. | |
# Instead, maintain a tarball which allows you to ADD a host directory as a file | |
# which *is* cacheable. I.E.: | |
# | |
# Prior to running your container: | |
# `pushd .; cd path/to; prepare_dir_tarball some_dir; popd` | |
# | |
# Then, inside your Dockerfile | |
# `ADD path/to/some_dir.tar.gz` | |
# | |
# The above is a cacheable, functional equivalent of: | |
# `ADD path/to/some_dir/` | |
prepare_dir_tarball() { | |
SRC_DIR=$1 | |
if [ -z "$1" ]; then | |
echo "ERROR: prepare_dir_tarball(): expected directory name" | |
exit 1 | |
fi | |
SRC_TAR="${SRC_DIR}.tar.gz" | |
if [ -f "$SRC_TAR" ]; then | |
echo "$SRC_TAR exists" | |
TMP_TAR="${SRC_DIR}.tmp.tar.gz" | |
tar -czf "$TMP_TAR" "$SRC_DIR" | |
SRC_SHA=`tar -xf "$SRC_TAR" --to-stdout | shasum` | |
TMP_SHA=`tar -xf "$TMP_TAR" --to-stdout | shasum` | |
if [ "$SRC_SHA" == "$TMP_SHA" ]; then | |
echo "$SRC_TAR has not changed, shasum $SRC_SHA" | |
else | |
echo "$SRC_TAR has changed, shasums:" | |
echo " old: $SRC_SHA" | |
echo " new: $TMP_SHA" | |
mv "$TMP_TAR" "$SRC_TAR" | |
echo "$SRC_TAR replaced" | |
fi | |
else | |
echo "$SRC_TAR does not exist, creating" | |
tar -czf "$SRC_TAR" "$SRC_DIR" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment