diff --git a/bin/restore_cache b/bin/restore_cache index f88fe687..c09314d9 100755 --- a/bin/restore_cache +++ b/bin/restore_cache @@ -2,6 +2,11 @@ CACHE_KEY=$1 +bytes_to_mb() { + local bytes=$1 + printf "%.2f" "$(awk "BEGIN {print $bytes / (1024 * 1024)}")" +} + S3_BUCKET_NAME=${CACHE_BUCKET_NAME-} if [ -z "$S3_BUCKET_NAME" ]; then if [ -z "$BUILDKITE_PLUGIN_A8C_CI_TOOLKIT_BUCKET" ]; then @@ -16,6 +21,7 @@ fi echo "Using $S3_BUCKET_NAME as cache bucket" if aws s3api head-object --bucket "$S3_BUCKET_NAME" --key "$CACHE_KEY" > /dev/null 2>&1; then + SECONDS=0 echo "Restoring cache entry $CACHE_KEY" echo " Downloading" @@ -29,11 +35,17 @@ if aws s3api head-object --bucket "$S3_BUCKET_NAME" --key "$CACHE_KEY" > /dev/nu aws s3 cp "s3://$S3_BUCKET_NAME/$CACHE_KEY" "$CACHE_KEY" --quiet fi + CACHE_SIZE=$(wc -c < "$CACHE_KEY") echo " Decompressing" tar -xf "$CACHE_KEY" echo " Cleaning Up" rm "$CACHE_KEY" + + duration=$SECONDS + echo "Cache entry successfully restored" + echo " Duration: $((duration / 60)) minutes and $((duration % 60)) seconds" + echo " Cache Size: $(bytes_to_mb "$CACHE_SIZE") MB" else echo "No cache entry found for '$CACHE_KEY'" fi diff --git a/bin/restore_gradle_dependency_cache b/bin/restore_gradle_dependency_cache index d5a3f746..27e7bd10 100755 --- a/bin/restore_gradle_dependency_cache +++ b/bin/restore_gradle_dependency_cache @@ -1,15 +1,25 @@ #!/bin/bash -eu # The key is shared with `bin/save_gradle_dependency_cache` -GRADLE_DEPENDENCY_CACHE_KEY="GRADLE_DEPENDENCY_CACHE" +GRADLE_DEPENDENCY_CACHE_KEY="${BUILDKITE_PIPELINE_SLUG}_GRADLE_DEPENDENCY_CACHE_V2" echo "Restoring Gradle dependency cache..." -DEP_CACHE_BASE_FOLDER=$(dirname "$GRADLE_RO_DEP_CACHE") +# The directory is shared with `bin/save_gradle_dependency_cache` +GRADLE_DEP_CACHE="$GRADLE_HOME/dependency-cache" + +DEP_CACHE_BASE_FOLDER=$(dirname "$GRADLE_DEP_CACHE") # `save_cache` & `restore_cache` scripts only work if they are called from the same directory pushd "$DEP_CACHE_BASE_FOLDER" restore_cache "$GRADLE_DEPENDENCY_CACHE_KEY" + +if [ -d "$DEP_CACHE_FOLDER_NAME/modules-2/" ]; then + echo "Placing Gradle dependency cache..." + mv "$DEP_CACHE_FOLDER_NAME/modules-2/"* caches/modules-2/ + rm -r "$DEP_CACHE_FOLDER_NAME" +fi + popd echo "---" diff --git a/bin/save_cache b/bin/save_cache index cb9b4a4b..045dc338 100755 --- a/bin/save_cache +++ b/bin/save_cache @@ -3,6 +3,11 @@ CACHE_FILE=$1 CACHE_KEY=$2 +bytes_to_mb() { + local bytes=$1 + printf "%.2f" "$(awk "BEGIN {print $bytes / (1024 * 1024)}")" +} + if [ -z "$CACHE_FILE" ]; then echo "You must pass the file or directory you want to be cached" exit 1 @@ -45,6 +50,7 @@ if [[ "$SHOULD_FORCE" == '--force' ]]; then fi if ! aws s3api head-object --bucket "$S3_BUCKET_NAME" --key "$CACHE_KEY" > /dev/null 2>&1; then + SECONDS=0 echo "No existing cache entry for $CACHE_KEY – storing in cache" echo " Compressing" @@ -59,6 +65,7 @@ if ! aws s3api head-object --bucket "$S3_BUCKET_NAME" --key "$CACHE_KEY" > /dev/ else tar -czf "$CACHE_KEY" "$CACHE_FILE" fi + CACHE_SIZE=$(wc -c < "$CACHE_KEY") echo " Uploading" # If the bucket has transfer acceleration enabled, use it! @@ -73,6 +80,11 @@ if ! aws s3api head-object --bucket "$S3_BUCKET_NAME" --key "$CACHE_KEY" > /dev/ echo " Cleaning Up" rm "$CACHE_KEY" + + duration=$SECONDS + echo "Cache entry successfully saved" + echo " Duration: $((duration / 60)) minutes and $((duration % 60)) seconds" + echo " Cache Size: $(bytes_to_mb "$CACHE_SIZE") MB" else echo "This file is already cached – skipping upload" fi diff --git a/bin/save_gradle_dependency_cache b/bin/save_gradle_dependency_cache index f0a08fff..e0c2546c 100755 --- a/bin/save_gradle_dependency_cache +++ b/bin/save_gradle_dependency_cache @@ -1,20 +1,23 @@ #!/bin/bash -eu # The key is shared with `bin/restore_gradle_dependency_cache` -GRADLE_DEPENDENCY_CACHE_KEY="GRADLE_DEPENDENCY_CACHE" +GRADLE_DEPENDENCY_CACHE_KEY="${BUILDKITE_PIPELINE_SLUG}_GRADLE_DEPENDENCY_CACHE_V2" echo "Saving Gradle dependency cache..." -mkdir -p "$GRADLE_RO_DEP_CACHE" +# The directory is shared with `bin/restore_gradle_dependency_cache` +GRADLE_DEP_CACHE="$GRADLE_HOME/dependency-cache" + +mkdir -p "$GRADLE_DEP_CACHE" # https://docs.gradle.org/current/userguide/dependency_resolution.html#sub:cache_copy # Gradle suggests removing the `*.lock` files and the `gc.properties` file before saving cache -cp -r ~/.gradle/caches/modules-2 "$GRADLE_RO_DEP_CACHE" \ - && find "$GRADLE_RO_DEP_CACHE" -name "*.lock" -type f -delete \ - && find "$GRADLE_RO_DEP_CACHE" -name "gc.properties" -type f -delete +cp -r ~/.gradle/caches/modules-2 "$GRADLE_DEP_CACHE" \ + && find "$GRADLE_DEP_CACHE" -name "*.lock" -type f -delete \ + && find "$GRADLE_DEP_CACHE" -name "gc.properties" -type f -delete -DEP_CACHE_BASE_FOLDER=$(dirname "$GRADLE_RO_DEP_CACHE") -DEP_CACHE_FOLDER_NAME=$(basename "$GRADLE_RO_DEP_CACHE") +DEP_CACHE_BASE_FOLDER=$(dirname "$GRADLE_DEP_CACHE") +DEP_CACHE_FOLDER_NAME=$(basename "$GRADLE_DEP_CACHE") # `save_cache` & `restore_cache` scripts only work if they are called from the same directory pushd "$DEP_CACHE_BASE_FOLDER"