Skip to content

Commit 0a4c0fb

Browse files
authored
Jetpack CLI: check and update monorepo docker image automatically once a day (#46460)
1 parent 178ac55 commit 0a4c0fb

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

tools/docker/bin/monorepo

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,40 @@ source "$MONOREPO_ROOT/.github/versions.sh"
1717
# Export variables needed by docker-compose
1818
export HOST_CWD="$MONOREPO_ROOT"
1919

20-
# Build the image if it doesn't exist
20+
# Check if we should pull the latest image
21+
CACHE_FILE="$MONOREPO_ROOT/tools/docker/data/.monorepo-image-check"
22+
SHOULD_PULL=0
23+
24+
# Always pull if image doesn't exist
2125
if ! docker image inspect jetpack-monorepo:latest >/dev/null 2>&1; then
22-
if [ "${BUILD_LOCAL:-}" = "1" ]; then
26+
SHOULD_PULL=1
27+
# Force pull if FORCE_PULL environment variable is set
28+
elif [[ "${FORCE_PULL:-}" = "1" ]]; then
29+
SHOULD_PULL=1
30+
echo "Forcing image update due to FORCE_PULL environment variable..."
31+
# Pull if cache file doesn't exist or is older than 24 hours (86400 seconds)
32+
elif [[ ! -f "$CACHE_FILE" ]]; then
33+
SHOULD_PULL=1
34+
else
35+
# Check if file is older than 24 hours
36+
if [[ "$(uname)" = "Darwin" ]]; then
37+
# macOS date command
38+
FILE_TIME=$(stat -f %m "$CACHE_FILE" 2>/dev/null || echo 0)
39+
else
40+
# Linux date command
41+
FILE_TIME=$(stat -c %Y "$CACHE_FILE" 2>/dev/null || echo 0)
42+
fi
43+
CURRENT_TIME=$(date +%s)
44+
TIME_DIFF=$((CURRENT_TIME - FILE_TIME))
45+
46+
if [[ "$TIME_DIFF" -gt 86400 ]]; then
47+
SHOULD_PULL=1
48+
fi
49+
fi
50+
51+
# Pull or build the image if needed
52+
if [[ "$SHOULD_PULL" = "1" ]]; then
53+
if [[ "${BUILD_LOCAL:-}" = "1" ]]; then
2354
echo "Building monorepo image locally..."
2455
docker build \
2556
--build-arg PHP_VERSION="$PHP_VERSION" \
@@ -30,10 +61,14 @@ if ! docker image inspect jetpack-monorepo:latest >/dev/null 2>&1; then
3061
-f "$MONOREPO_ROOT/tools/docker/Dockerfile.monorepo" \
3162
"$MONOREPO_ROOT/tools/docker"
3263
else
33-
echo "Pulling monorepo image..."
64+
echo "Checking for latest monorepo image..."
3465
docker pull automattic/jetpack-monorepo:latest
3566
docker tag automattic/jetpack-monorepo:latest jetpack-monorepo:latest
3667
fi
68+
69+
# Update the timestamp file
70+
mkdir -p "$(dirname "$CACHE_FILE")"
71+
touch "$CACHE_FILE"
3772
fi
3873

3974
# Run the command in the container

0 commit comments

Comments
 (0)