Skip to content

Instantly share code, notes, and snippets.

@letmaik
Last active November 23, 2020 04:37
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save letmaik/caa0f6cc4375cbfcc1ff26bd4530c2a3 to your computer and use it in GitHub Desktop.
Save letmaik/caa0f6cc4375cbfcc1ff26bd4530c2a3 to your computer and use it in GitHub Desktop.
Using travis_retry inside shell scripts together with set -e

If you want to use travis_retry from within your own shell script files then you first have to make the travis_retry shell function available by sourcing the travis_retry.sh file, otherwise you just get a "command not found" error. See example.sh for a full example.

Note that the original function as found in the travis-ci/travis-build repository was slightly modified to allow it to be used in a shell context where set -e is enabled.

For reference, a tweet by Travis CI saying that you should copy the travis_retry code as I've done here: https://twitter.com/plexus/status/499194992632811520

#!/bin/bash
# fail script immediately on any errors in external commands
set -e
source travis_retry.sh
travis_retry pip install numpy
# https://github.com/travis-ci/travis-build/blob/master/lib/travis/build/templates/header.sh
travis_retry() {
local result=0
local count=1
while [ $count -le 3 ]; do
[ $result -ne 0 ] && {
echo -e "\n${ANSI_RED}The command \"$@\" failed. Retrying, $count of 3.${ANSI_RESET}\n" >&2
}
# ! { } ignores set -e, see https://stackoverflow.com/a/4073372
! { "$@"; result=$?; }
[ $result -eq 0 ] && break
count=$(($count + 1))
sleep 1
done
[ $count -gt 3 ] && {
echo -e "\n${ANSI_RED}The command \"$@\" failed 3 times.${ANSI_RESET}\n" >&2
}
return $result
}
@tianon
Copy link

tianon commented Jun 12, 2018

FYI, this was fixed in travis-ci/travis-build#1162! ❤️

@azbarcea
Copy link

This seems very valuable if your build runs inside a docker container, and one needs to push the travis_retry.bash (also changed the location in the meanwhile and the original template file name) inside the docker image (Dockerfile).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment