From 92bed916706bf92cc9fc9d7492e69c626d74ec6e Mon Sep 17 00:00:00 2001 From: Prathamesh Zarkar <159782310+prathameshzarkar9@users.noreply.github.com> Date: Mon, 11 Mar 2024 22:58:54 +0530 Subject: [PATCH] check size for the universal image (#979) --- .github/actions/smoke-test/action.yaml | 6 +- .../actions/smoke-test/check-image-size.sh | 58 +++++++++++++++++++ .github/actions/smoke-test/test.sh | 8 +++ .github/workflows/smoke-universal.yaml | 1 + 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100755 .github/actions/smoke-test/check-image-size.sh diff --git a/.github/actions/smoke-test/action.yaml b/.github/actions/smoke-test/action.yaml index c90c78025..fa9208eac 100644 --- a/.github/actions/smoke-test/action.yaml +++ b/.github/actions/smoke-test/action.yaml @@ -4,6 +4,10 @@ inputs: description: 'Image to test' required: true default: 'base-debian' + threshold: + description: 'Threshold (in GB) to validate that the image size does not excced this limit' + required: false + default: 14 runs: using: composite @@ -24,4 +28,4 @@ runs: - name: Test image id: test_image shell: bash - run: ${{ github.action_path }}/test.sh ${{ inputs.image }} + run: ${{ github.action_path }}/test.sh ${{ inputs.image }} ${{ inputs.threshold }} diff --git a/.github/actions/smoke-test/check-image-size.sh b/.github/actions/smoke-test/check-image-size.sh new file mode 100755 index 000000000..e1cad85d2 --- /dev/null +++ b/.github/actions/smoke-test/check-image-size.sh @@ -0,0 +1,58 @@ +#!/bin/bash +# Function to handle errors +handle_error() { + local exit_code=$? + local line_number=$1 + local command=$2 + echo "Error occurred at line $line_number with exit code $exit_code in command $command" + exit $exit_code +} +trap 'handle_error $LINENO ${BASH_COMMAND%% *}' ERR +echo "This is line $LINENO" + +convert_gb_to_bytes() { + local gb="$1" + local bytes + bytes=$(echo "scale=0; $gb * 1024^3" | bc) + printf "%.0f\n" "$bytes" +} + +# Check if bc is installed +install_bc() { + if ! command -v bc &> /dev/null; then + echo "bc is not installed. Installing..." + # Install bc using apt-get (for Debian-based systems) + sudo apt-get update + sudo apt-get install -y bc + fi +} + +check_image_size() { + IMAGE="$1" + THRESHOLD_IN_GB="$2" + + # call install_bc + install_bc + + CONTAINER_ID=$(docker ps -q --filter "label=test-container=$IMAGE") + # Find the image ID of the container + IMAGE_ID=$(docker inspect --format='{{.Image}}' "$CONTAINER_ID") + # Find the size of the image + IMAGE_SIZE=$(docker image inspect --format='{{.Size}}' "$IMAGE_ID") + # Output the size + echo "Size of the image $IMAGE_ID: $IMAGE_SIZE bytes" + threshold=$(convert_gb_to_bytes "$THRESHOLD_IN_GB") + # Retrieve the Docker image size + echo -e "\nThreshold is $threshold bytes ie $THRESHOLD_IN_GB GB" + # Remove the 'MB' from the size string and convert to an integer + image_size=${IMAGE_SIZE%bytes} + image_size=${image_size//.} + # Check if the image size is above the threshold + echo -e "\n🧪 Checking image size of $IMAGE :" + if [ -n $image_size ] && [ $image_size -gt $threshold ]; then + echo -e "\nImage size exceeds the threshold of $THRESHOLD_IN_GB gb" + echo -e "\n❌ Image size check failed." + else + echo -e "\n✅ Passed!" + fi +} diff --git a/.github/actions/smoke-test/test.sh b/.github/actions/smoke-test/test.sh index 1e9ba452a..6a9c9a674 100755 --- a/.github/actions/smoke-test/test.sh +++ b/.github/actions/smoke-test/test.sh @@ -1,5 +1,8 @@ #/bin/bash IMAGE="$1" +THRESHOLD_IN_GB="$2" + +source $(pwd)/.github/actions/smoke-test/check-image-size.sh export DOCKER_BUILDKIT=1 set -e @@ -11,6 +14,11 @@ devcontainer exec --workspace-folder $(pwd)/src/$IMAGE --id-label ${id_label} / echo "(*) Docker image details..." docker images +# Checking size of universal image + +if [ $IMAGE == "universal" ]; then + check_image_size $IMAGE $THRESHOLD_IN_GB +fi # Clean up docker rm -f $(docker container ls -f "label=${id_label}" -q) diff --git a/.github/workflows/smoke-universal.yaml b/.github/workflows/smoke-universal.yaml index 838b04b82..108853972 100644 --- a/.github/workflows/smoke-universal.yaml +++ b/.github/workflows/smoke-universal.yaml @@ -26,3 +26,4 @@ jobs: uses: ./.github/actions/smoke-test with: image: universal + threshold: 14