diff --git a/dev-tools/build-packages/build-packages.sh b/dev-tools/build-packages/build-packages.sh index 15ff7dddcd56..75f16b28735f 100755 --- a/dev-tools/build-packages/build-packages.sh +++ b/dev-tools/build-packages/build-packages.sh @@ -81,6 +81,8 @@ get_packages(){ cd .. } +source ../utils/retry-operation.sh + build_tar() { log log "Building base package..." @@ -92,9 +94,12 @@ build_tar() { container_name="dashboard-base-builder" cp ./base-builder.sh ${dockerfile_path} cp ./plugins ${dockerfile_path} - docker build -t ${container_name} ${dockerfile_path} || return 1 - docker run -t --rm -v ${tmp_dir}/:/tmp:Z -v ${output_dir}/:/output:Z\ - ${container_name} ${version} ${revision} ${architecture} ${verbose}|| return 1 + retry_operation "build base package" 5 15 \ + "docker build -t ${container_name} ${dockerfile_path} && \ + docker run -t --rm \ + -v ${tmp_dir}/:/tmp:Z \ + -v ${output_dir}/:/output:Z \ + ${container_name} ${version} ${revision} ${architecture} ${verbose}" || exit 1 cd .. } @@ -106,10 +111,13 @@ build_rpm() { cp -r ${package_config_dir} ${tmp_dir} cp ./rpm-builder.sh ${dockerfile_path} cp ./wazuh-dashboard.spec ${dockerfile_path} - docker build -t ${container_name} ${dockerfile_path} || return 1 - docker run -t --rm -v ${tmp_dir}/:/tmp:Z -v ${output_dir}/:/output:Z\ - ${container_name} ${version} ${revision} ${architecture}\ - ${commit_sha} ${production} ${verbose}|| return 1 + retry_operation "build rpm package" 5 15 \ + "docker build -t ${container_name} ${dockerfile_path} && \ + docker run -t --rm \ + -v ${tmp_dir}/:/tmp:Z \ + -v ${output_dir}/:/output:Z \ + ${container_name} ${version} ${revision} ${architecture} \ + ${commit_sha} ${production} ${verbose}" || exit 1 cd ../ } @@ -123,9 +131,13 @@ build_deb() { cp ./deb-builder.sh ${dockerfile_path} cp -r ./debian ${dockerfile_path} docker build -t ${container_name} ${dockerfile_path} || return 1 - docker run -t --rm -v ${tmp_dir}/:/tmp:Z -v ${output_dir}/:/output:Z \ - ${container_name} ${version} ${revision} ${architecture}\ - ${commit_sha} ${production} ${verbose}|| return 1 + retry_operation "build deb package" 5 15 \ + "docker build -t ${container_name} ${dockerfile_path} && \ + docker run -t --rm \ + -v ${tmp_dir}/:/tmp:Z \ + -v ${output_dir}/:/output:Z \ + ${container_name} ${version} ${revision} ${architecture} \ + ${commit_sha} ${production} ${verbose}" || exit 1 cd .. } diff --git a/dev-tools/test-packages/test-packages.sh b/dev-tools/test-packages/test-packages.sh index 5f21bc955e00..c40dcbf49e8f 100644 --- a/dev-tools/test-packages/test-packages.sh +++ b/dev-tools/test-packages/test-packages.sh @@ -107,17 +107,21 @@ check_metadata_rpm() { echo "metadata package is correct: $metadataPackage" } +source ../utils/retry-operation.sh + # Run test test() { if [[ $PACKAGE == *".deb" ]]; then - docker build --build-arg PACKAGE=$PACKAGE -t $CONTAINER_NAME ./deb/ - docker run -it --rm -d --name $CONTAINER_NAME $CONTAINER_NAME - check_metadata_deb + retry_operation "test deb package" 5 15 \ + "docker build --build-arg PACKAGE=$PACKAGE -t $CONTAINER_NAME ./deb/ && \ + docker run -it --rm -d --name $CONTAINER_NAME $CONTAINER_NAME && \ + check_metadata_deb" || exit 1 elif [[ $PACKAGE == *".rpm" ]]; then - docker build --build-arg PACKAGE=$PACKAGE -t $CONTAINER_NAME ./rpm/ - docker run -it --rm -d --name $CONTAINER_NAME $CONTAINER_NAME - check_metadata_rpm + retry_operation "test rpm package" 5 15 \ + "docker build --build-arg PACKAGE=$PACKAGE -t $CONTAINER_NAME ./rpm/ && \ + docker run -it --rm -d --name $CONTAINER_NAME $CONTAINER_NAME && \ + check_metadata_rpm" || exit 1 else echo "ERROR: $PACKAGE is not a valid package (valid packages are .deb and .rpm ))" exit 1 diff --git a/dev-tools/utils/retry-operation.sh b/dev-tools/utils/retry-operation.sh new file mode 100644 index 000000000000..e128ff0538cf --- /dev/null +++ b/dev-tools/utils/retry-operation.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# Function to retry an operation a number of times +# Arguments: +# $1: Name of the operation +# $2: Number of attempts +# $3: Delay between attempts +# $4+: Command to execute +retry_operation() { + local operation_name=$1 + local max_attempts=$2 + local sleep_time=$3 + shift 3 # Delete the first 3 arguments + + for i in $(seq 1 $max_attempts); do + if eval "$@"; then + return 0 + else + if [ $i -eq $max_attempts ]; then + echo "Failed to $operation_name after $max_attempts attempts" + return 1 + fi + sleep $sleep_time + fi + done +}