Skip to content

Commit e446466

Browse files
authored
Merge pull request #2516 from effigies/ci/loop_script
CI: Add retry script for Docker commands
2 parents f2aaeb9 + 2cd3da3 commit e446466

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

.circleci/config.yml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ _get_base_image: &get_base_image
3636
echo "Pulling base image ..."
3737
docker pull nipype/nipype:base
3838
elif [ "$GET_BASE" == "BUILD" ]; then
39-
e=1 && for i in {1..5}; do
40-
docker build -t nipype/nipype:base - < docker/Dockerfile.base && e=0 && break || sleep 15
41-
done && [ "$e" -eq "0" ]
39+
tools/retry_cmd.sh -n 5 -s 15 \
40+
docker build -t nipype/nipype:base - < docker/Dockerfile.base
4241
else
4342
echo "Error: method to get base image not understood"
4443
exit 1
@@ -48,22 +47,20 @@ _build_main_image_py36: &build_main_image_py36
4847
name: Build main image (py36)
4948
no_output_timeout: 60m
5049
command: |
51-
e=1 && for i in {1..5}; do
50+
tools/retry_cmd.sh -n 5 -s 15 \
5251
docker build \
5352
--rm=false \
5453
--tag nipype/nipype:latest \
5554
--tag nipype/nipype:py36 \
5655
--build-arg BUILD_DATE="$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \
5756
--build-arg VCS_REF="$(git rev-parse --short HEAD)" \
5857
--build-arg VERSION="${CIRCLE_TAG}" /home/circleci/nipype \
59-
&& e=0 && break || sleep 15
60-
done && [ "$e" -eq "0" ]
6158
6259
_build_main_image_py27: &build_main_image_py27
6360
name: Build main image (py27)
6461
no_output_timeout: 60m
6562
command: |
66-
e=1 && for i in {1..5}; do
63+
tools/retry_cmd.sh -n 5 -s 15 \
6764
docker build \
6865
--rm=false \
6966
--tag nipype/nipype:py27 \
@@ -72,8 +69,6 @@ _build_main_image_py27: &build_main_image_py27
7269
--build-arg BUILD_DATE="$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \
7370
--build-arg VCS_REF="$(git rev-parse --short HEAD)" \
7471
--build-arg VERSION="${CIRCLE_TAG}-py27" /home/circleci/nipype \
75-
&& e=0 && break || sleep 15
76-
done && [ "$e" -eq "0" ]
7772
7873
_download_test_data: &_download_test_data
7974
name: Download test data

tools/retry_cmd.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/sh
2+
#
3+
# retry_cmd.sh [-n NLOOPS] [-s SLEEP] CMD
4+
#
5+
# Retry command until success or pre-specified number of failures
6+
#
7+
# 2018 Chris Markiewicz
8+
# Released into public domain
9+
10+
NLOOPS=3
11+
TOSLEEP=5
12+
13+
while true; do
14+
case "$1" in
15+
-n ) NLOOPS="$2"; shift 2 ;;
16+
-s ) TOSLEEP="$2"; shift 2 ;;
17+
-- ) shift; break ;;
18+
* ) break ;;
19+
esac
20+
done
21+
22+
# Normalize whitespace in command, preserving quotes
23+
CMD=""
24+
for ARG; do
25+
CMD="$CMD \"$ARG\"";
26+
done
27+
28+
RET=0
29+
for i in `seq $NLOOPS`; do
30+
sh -c "$CMD"
31+
RET="$?"
32+
if [ "$RET" -eq 0 ]; then break; fi
33+
if [ "$i" -ne "$NLOOPS" ]; then sleep $TOSLEEP; fi
34+
done
35+
36+
exit $RET

0 commit comments

Comments
 (0)