Skip to content

Commit ae23e5f

Browse files
committed
fix: move the caching to a script
1 parent d0f7cf9 commit ae23e5f

File tree

2 files changed

+80
-26
lines changed

2 files changed

+80
-26
lines changed

.circleci/cache-python-docker.sh

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

.circleci/config.yml

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -212,28 +212,12 @@ jobs:
212212

213213
- run:
214214
name: Load cached Docker images if available
215-
command: |
216-
if [ -d "/c/docker-cache" ] && [ "$(ls -A /c/docker-cache/*.tar 2>/dev/null)" ]; then
217-
for image in /c/docker-cache/*.tar; do
218-
echo "Loading $(basename $image)..."
219-
docker load -i "$image"
220-
done
221-
else
222-
echo "No cached Docker images found"
223-
fi
215+
command: .circleci/cache-python-docker.sh load
224216

225217
# Pre-pull the Python image to cache it
226218
- run:
227219
name: Pre-pull Python test image
228-
command: |
229-
PYTHON_IMAGE="python@sha256:1f92d35b567363820d0f2f37c7ccf2c1543e2d852cea01edb027039e6aef25e6"
230-
echo "Checking if Python image exists locally..."
231-
if ! docker images -q "$PYTHON_IMAGE" 2>/dev/null | grep -q .; then
232-
echo "Pulling Python image for cache..."
233-
docker pull "$PYTHON_IMAGE"
234-
else
235-
echo "Python image already exists locally"
236-
fi
220+
command: .circleci/cache-python-docker.sh pull
237221
- run:
238222
name: Install JUnit coverage reporter
239223
command: npm install --no-save jest-junit
@@ -252,14 +236,7 @@ jobs:
252236
- run:
253237
name: Save Docker images to cache
254238
when: always
255-
command: |
256-
mkdir -p /c/docker-cache
257-
# Save the Python test image
258-
PYTHON_IMAGE="python@sha256:1f92d35b567363820d0f2f37c7ccf2c1543e2d852cea01edb027039e6aef25e6"
259-
if docker images -q "$PYTHON_IMAGE" 2>/dev/null | grep -q .; then
260-
echo "Saving Python image to cache..."
261-
docker save "$PYTHON_IMAGE" -o "/c/docker-cache/python-3.9.0.tar"
262-
fi
239+
command: .circleci/cache-python-docker.sh save
263240

264241
- save_cache:
265242
key: v1-docker-images-{{ checksum "test/windows/plugin.spec.ts" }}

0 commit comments

Comments
 (0)