1+ #! /bin/bash
2+ set -e
3+
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]
18+
19+ CACHE_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
21+ PYTHON_IMAGE=" python@sha256:1f92d35b567363820d0f2f37c7ccf2c1543e2d852cea01edb027039e6aef25e6"
22+ PYTHON_IMAGE_FILE=" python-3.9.0.tar"
23+
24+ case " $1 " in
25+ load)
26+ echo " === Loading Docker images from cache ==="
27+ if [ -d " $CACHE_DIR " ] && [ " $( ls -A $CACHE_DIR /* .tar 2> /dev/null) " ]; then
28+ for image in $CACHE_DIR /* .tar; do
29+ echo " Loading $( basename $image ) ..."
30+ docker load -i " $image "
31+ done
32+ echo " Docker images loaded from cache successfully"
33+ else
34+ echo " No cached Docker images found in $CACHE_DIR "
35+ fi
36+ ;;
37+
38+ pull)
39+ echo " === Pre-pulling Python test image ==="
40+ echo " Checking if Python image exists locally..."
41+ if ! docker images -q " $PYTHON_IMAGE " 2> /dev/null | grep -q . ; then
42+ echo " Python image not found locally, pulling from registry..."
43+ docker pull " $PYTHON_IMAGE "
44+ echo " Python image pulled successfully"
45+ else
46+ echo " Python image already exists locally"
47+ fi
48+ ;;
49+
50+ save)
51+ echo " === Saving Docker images to cache ==="
52+ mkdir -p " $CACHE_DIR "
53+
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 "
59+ else
60+ echo " Python image not found locally, skipping save"
61+ fi
62+
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
68+ ;;
69+
70+ * )
71+ echo " Usage: $0 [load|save|pull]"
72+ echo " load - Load Docker images from cache"
73+ echo " save - Save Docker images to cache"
74+ echo " pull - Pull Python image if not already present"
75+ exit 1
76+ ;;
77+ esac
0 commit comments