Skip to content

Commit 7d4cfee

Browse files
authored
Merge pull request sonic-net#79 from PeterTSW-EC/build_docker_crash_fix
[SONiC-Build] Support "timeout and retry" scheme to build_docker.sh
2 parents 4554ea0 + c768611 commit 7d4cfee

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

build_docker.sh

+36-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ usage() {
1010
cat >&2 <<EOF
1111
Usage:
1212
sudo ./build_docker.sh [-i DOCKER_IMAGE_NAME] [-t DOCKER_IMAGE_TAG] DOCKER_BUILD_DIR [REGISTRY_SERVER REGISTRY_PORT REGISTRY_USERNAME REGISTRY_PASSWD]
13-
13+
1414
Description:
1515
-i DOCKER_IMAGE_NAME
1616
Specify the docker image's name, by default it is DOCKER_BUILD_DIR
@@ -22,14 +22,15 @@ Description:
2222
The server name of the docker registry
2323
REGISTRY_PORT
2424
The port of the docker registry
25-
25+
2626
Example:
2727
./build_docker.sh -i docker-orchagent-mlnx docker-orchagent
2828
EOF
2929
}
3030

3131
docker_image_name=''
3232
docker_image_tag=latest
33+
retry_times=3
3334
## The option-string tells getopts which options to expect and which of them must have an argument
3435
## When you want getopts to expect an argument for an option, just place a : (colon) after the proper option flag
3536
## If the very first character of the option-string is a :, getopts switches to "silent error reporting mode".
@@ -78,9 +79,41 @@ cp -r files $DOCKER_BUILD_DIR/files
7879
docker_try_rmi $docker_image_name
7980

8081
## Build the docker image
81-
docker build --no-cache -t $docker_image_name $DOCKER_BUILD_DIR
82+
set +e
83+
for (( i=$retry_times; i>0; i-- )); do
84+
timeout 1h docker build --no-cache -t $docker_image_name $DOCKER_BUILD_DIR
85+
ret_code=$?
86+
case $ret_code in
87+
# Docker build without error.
88+
"0")
89+
break
90+
;;
91+
# Docker build timeout
92+
"124")
93+
docker_try_rmi $docker_image_name # Remove the exist image
94+
if [[ $i -eq 1 ]]; then
95+
echo "Failed to build container [$docker_image_name] in $retry_times times, exit. "
96+
rm -rf $DOCKER_BUILD_DIR
97+
exit $ret_code
98+
else
99+
echo "Failed to build container [$docker_image_name], retry. "
100+
fi
101+
;;
102+
# Command "Timeout" return error.
103+
"125"|"126"|"127"|"137")
104+
echo "Error with command \"timeout\", exit. "
105+
break
106+
;;
107+
# Docker build got error
108+
*)
109+
rm -rf $DOCKER_BUILD_DIR # Replace the trap_up function
110+
exit $ret_code ;;
111+
esac
112+
done
113+
82114
## Get the ID of the built image
83115
## Note: inspect output has quotation characters, so sed to remove it as an argument
116+
set -e
84117
image_id=$(docker inspect --format="{{json .Id}}" $docker_image_name | sed -e 's/^"//' -e 's/"$//')
85118

86119
## Flatten the image by importing an exported container on this image

0 commit comments

Comments
 (0)