Skip to content

Commit 92bed91

Browse files
check size for the universal image (devcontainers#979)
1 parent 3934e0f commit 92bed91

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

.github/actions/smoke-test/action.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ inputs:
44
description: 'Image to test'
55
required: true
66
default: 'base-debian'
7+
threshold:
8+
description: 'Threshold (in GB) to validate that the image size does not excced this limit'
9+
required: false
10+
default: 14
711

812
runs:
913
using: composite
@@ -24,4 +28,4 @@ runs:
2428
- name: Test image
2529
id: test_image
2630
shell: bash
27-
run: ${{ github.action_path }}/test.sh ${{ inputs.image }}
31+
run: ${{ github.action_path }}/test.sh ${{ inputs.image }} ${{ inputs.threshold }}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/bash
2+
# Function to handle errors
3+
handle_error() {
4+
local exit_code=$?
5+
local line_number=$1
6+
local command=$2
7+
echo "Error occurred at line $line_number with exit code $exit_code in command $command"
8+
exit $exit_code
9+
}
10+
trap 'handle_error $LINENO ${BASH_COMMAND%% *}' ERR
11+
echo "This is line $LINENO"
12+
13+
convert_gb_to_bytes() {
14+
local gb="$1"
15+
local bytes
16+
bytes=$(echo "scale=0; $gb * 1024^3" | bc)
17+
printf "%.0f\n" "$bytes"
18+
}
19+
20+
# Check if bc is installed
21+
install_bc() {
22+
if ! command -v bc &> /dev/null; then
23+
echo "bc is not installed. Installing..."
24+
# Install bc using apt-get (for Debian-based systems)
25+
sudo apt-get update
26+
sudo apt-get install -y bc
27+
fi
28+
}
29+
30+
check_image_size() {
31+
IMAGE="$1"
32+
THRESHOLD_IN_GB="$2"
33+
34+
# call install_bc
35+
install_bc
36+
37+
CONTAINER_ID=$(docker ps -q --filter "label=test-container=$IMAGE")
38+
# Find the image ID of the container
39+
IMAGE_ID=$(docker inspect --format='{{.Image}}' "$CONTAINER_ID")
40+
# Find the size of the image
41+
IMAGE_SIZE=$(docker image inspect --format='{{.Size}}' "$IMAGE_ID")
42+
# Output the size
43+
echo "Size of the image $IMAGE_ID: $IMAGE_SIZE bytes"
44+
threshold=$(convert_gb_to_bytes "$THRESHOLD_IN_GB")
45+
# Retrieve the Docker image size
46+
echo -e "\nThreshold is $threshold bytes ie $THRESHOLD_IN_GB GB"
47+
# Remove the 'MB' from the size string and convert to an integer
48+
image_size=${IMAGE_SIZE%bytes}
49+
image_size=${image_size//.}
50+
# Check if the image size is above the threshold
51+
echo -e "\n🧪 Checking image size of $IMAGE :"
52+
if [ -n $image_size ] && [ $image_size -gt $threshold ]; then
53+
echo -e "\nImage size exceeds the threshold of $THRESHOLD_IN_GB gb"
54+
echo -e "\n❌ Image size check failed."
55+
else
56+
echo -e "\n✅ Passed!"
57+
fi
58+
}

.github/actions/smoke-test/test.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#/bin/bash
22
IMAGE="$1"
3+
THRESHOLD_IN_GB="$2"
4+
5+
source $(pwd)/.github/actions/smoke-test/check-image-size.sh
36

47
export DOCKER_BUILDKIT=1
58
set -e
@@ -11,6 +14,11 @@ devcontainer exec --workspace-folder $(pwd)/src/$IMAGE --id-label ${id_label} /
1114

1215
echo "(*) Docker image details..."
1316
docker images
17+
# Checking size of universal image
18+
19+
if [ $IMAGE == "universal" ]; then
20+
check_image_size $IMAGE $THRESHOLD_IN_GB
21+
fi
1422

1523
# Clean up
1624
docker rm -f $(docker container ls -f "label=${id_label}" -q)

.github/workflows/smoke-universal.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ jobs:
2626
uses: ./.github/actions/smoke-test
2727
with:
2828
image: universal
29+
threshold: 14

0 commit comments

Comments
 (0)