diff --git a/.github/actions/smoke-test/action.yaml b/.github/actions/smoke-test/action.yaml index c90c78025..6b9366cb8 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 check the image size 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..46edb706f --- /dev/null +++ b/.github/actions/smoke-test/check-image-size.sh @@ -0,0 +1,75 @@ +#!/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" +INSTALL_FLAG=1 + +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() { + echo "in install_bc function.." + 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 + else + INSTALL_FLAG=1 + fi +} + +# Uninstall bc if it was installed temporarily +uninstall_bc() { + echo "in uninstall_bc function..." + if [[ $(dpkg-query -l bc | grep -c '^ii ') -eq 1 && "$INSTALL_FLAG" -eq 0 ]]; then + echo "Removing bc..." + sudo apt-get purge -y bc + fi +} + +check_image_size() { + IMAGE="$1" + THRESHOLD_IN_GB="$2" + INSTALL_FLAG=0 + + # 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=$threshold" + # 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 + + # call uninstall_bc + uninstall_bc +} diff --git a/.github/actions/smoke-test/test.sh b/.github/actions/smoke-test/test.sh index 1e9ba452a..f9508bf82 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 @@ -14,3 +17,9 @@ docker images # Clean up docker rm -f $(docker container ls -f "label=${id_label}" -q) + +# Checking size of universal image + +if [ $IMAGE == "universal" ]; then + check_image_size $IMAGE $THRESHOLD_IN_GB +fi \ No newline at end of file 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