diff --git a/.circleci/config.yml b/.circleci/config.yml index b04d9ee4a3..57796d6f57 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -36,9 +36,8 @@ _get_base_image: &get_base_image echo "Pulling base image ..." docker pull nipype/nipype:base elif [ "$GET_BASE" == "BUILD" ]; then - e=1 && for i in {1..5}; do - docker build -t nipype/nipype:base - < docker/Dockerfile.base && e=0 && break || sleep 15 - done && [ "$e" -eq "0" ] + tools/retry_cmd.sh -n 5 -s 15 \ + docker build -t nipype/nipype:base - < docker/Dockerfile.base else echo "Error: method to get base image not understood" exit 1 @@ -48,7 +47,7 @@ _build_main_image_py36: &build_main_image_py36 name: Build main image (py36) no_output_timeout: 60m command: | - e=1 && for i in {1..5}; do + tools/retry_cmd.sh -n 5 -s 15 \ docker build \ --rm=false \ --tag nipype/nipype:latest \ @@ -56,14 +55,12 @@ _build_main_image_py36: &build_main_image_py36 --build-arg BUILD_DATE="$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \ --build-arg VCS_REF="$(git rev-parse --short HEAD)" \ --build-arg VERSION="${CIRCLE_TAG}" /home/circleci/nipype \ - && e=0 && break || sleep 15 - done && [ "$e" -eq "0" ] _build_main_image_py27: &build_main_image_py27 name: Build main image (py27) no_output_timeout: 60m command: | - e=1 && for i in {1..5}; do + tools/retry_cmd.sh -n 5 -s 15 \ docker build \ --rm=false \ --tag nipype/nipype:py27 \ @@ -72,8 +69,6 @@ _build_main_image_py27: &build_main_image_py27 --build-arg BUILD_DATE="$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \ --build-arg VCS_REF="$(git rev-parse --short HEAD)" \ --build-arg VERSION="${CIRCLE_TAG}-py27" /home/circleci/nipype \ - && e=0 && break || sleep 15 - done && [ "$e" -eq "0" ] _download_test_data: &_download_test_data name: Download test data diff --git a/tools/retry_cmd.sh b/tools/retry_cmd.sh new file mode 100755 index 0000000000..78e9c40924 --- /dev/null +++ b/tools/retry_cmd.sh @@ -0,0 +1,36 @@ +#!/bin/sh +# +# retry_cmd.sh [-n NLOOPS] [-s SLEEP] CMD +# +# Retry command until success or pre-specified number of failures +# +# 2018 Chris Markiewicz +# Released into public domain + +NLOOPS=3 +TOSLEEP=5 + +while true; do + case "$1" in + -n ) NLOOPS="$2"; shift 2 ;; + -s ) TOSLEEP="$2"; shift 2 ;; + -- ) shift; break ;; + * ) break ;; + esac +done + +# Normalize whitespace in command, preserving quotes +CMD="" +for ARG; do + CMD="$CMD \"$ARG\""; +done + +RET=0 +for i in `seq $NLOOPS`; do + sh -c "$CMD" + RET="$?" + if [ "$RET" -eq 0 ]; then break; fi + if [ "$i" -ne "$NLOOPS" ]; then sleep $TOSLEEP; fi +done + +exit $RET