11#! /bin/bash
22set -e
33
4- # Because the Python image is >3GB the reason the pipeline takes
5- # so damn long is because it's downloading the image every time.
6- # The download takes longer than the actual tests.
7- #
8- # This script is used to cache the image and load it from cache
9- # when the pipeline runs.
10- #
11- # The image is saved as a tar file and loaded from cache.
12- # The image is pulled from the registry if it's not already present.
13- #
14- # It will save on time. It will save on bandwidth and CircleCI credits.
15- # It will save my sanity.
16- #
17- # Usage: ./docker-cache.sh [load|save|pull]
4+ # Script to manage Docker image caching on Windows CircleCI
5+ # Usage: ./cache-python-docker.sh [load|save|pull]
186
197CACHE_DIR=" /c/docker-cache"
20- # This is the image we're caching, Python 3.9. Use the same sha as in test/windows/plugin.spec.ts
218PYTHON_IMAGE=" python@sha256:1f92d35b567363820d0f2f37c7ccf2c1543e2d852cea01edb027039e6aef25e6"
229PYTHON_IMAGE_FILE=" python-3.9.0.tar"
10+ STATE_FILE=" /tmp/.docker-cache-state"
11+
12+ # Function to check if image exists
13+ image_exists () {
14+ docker images -q " $1 " 2> /dev/null | grep -q .
15+ }
2316
2417case " $1 " in
2518 load)
@@ -38,39 +31,49 @@ case "$1" in
3831 pull)
3932 echo " === Pre-pulling Python test image ==="
4033 echo " Checking if Python image exists locally..."
41- if ! docker images -q " $PYTHON_IMAGE " 2> /dev/null | grep -q . ; then
34+
35+ # Track if image was already present
36+ if image_exists " $PYTHON_IMAGE " ; then
37+ echo " Python image already exists locally"
38+ echo " already_present" > " $STATE_FILE "
39+ else
4240 echo " Python image not found locally, pulling from registry..."
4341 docker pull " $PYTHON_IMAGE "
4442 echo " Python image pulled successfully"
45- else
46- echo " Python image already exists locally"
43+ echo " newly_pulled" > " $STATE_FILE "
4744 fi
4845 ;;
4946
5047 save)
5148 echo " === Saving Docker images to cache ==="
52- mkdir -p " $CACHE_DIR "
5349
54- # Save the Python test image
55- if docker images -q " $PYTHON_IMAGE " 2> /dev/null | grep -q . ; then
56- echo " Saving Python image to cache..."
57- docker save " $PYTHON_IMAGE " -o " $CACHE_DIR /$PYTHON_IMAGE_FILE "
58- echo " Python image saved to $CACHE_DIR /$PYTHON_IMAGE_FILE "
50+ # Check if we should save (only if we pulled it this run)
51+ if [ -f " $STATE_FILE " ] && [ " $( cat $STATE_FILE ) " = " newly_pulled" ]; then
52+ mkdir -p " $CACHE_DIR "
53+
54+ if image_exists " $PYTHON_IMAGE " ; then
55+ echo " Saving newly pulled Python image to cache..."
56+ docker save " $PYTHON_IMAGE " -o " $CACHE_DIR /$PYTHON_IMAGE_FILE "
57+ echo " Python image saved to $CACHE_DIR /$PYTHON_IMAGE_FILE "
58+
59+ # Show cache size
60+ ls -lh " $CACHE_DIR /$PYTHON_IMAGE_FILE "
61+ else
62+ echo " ERROR: Python image not found locally despite being pulled"
63+ exit 1
64+ fi
5965 else
60- echo " Python image not found locally , skipping save"
66+ echo " Python image was already cached , skipping save"
6167 fi
6268
63- # List cached files
64- if [ -d " $CACHE_DIR " ] && [ " $( ls -A $CACHE_DIR /* .tar 2> /dev/null) " ]; then
65- echo " Cached Docker images:"
66- ls -lh $CACHE_DIR /* .tar
67- fi
69+ # Clean up state file
70+ rm -f " $STATE_FILE "
6871 ;;
6972
7073 * )
7174 echo " Usage: $0 [load|save|pull]"
7275 echo " load - Load Docker images from cache"
73- echo " save - Save Docker images to cache"
76+ echo " save - Save Docker images to cache (only if newly pulled) "
7477 echo " pull - Pull Python image if not already present"
7578 exit 1
7679 ;;
0 commit comments