retry.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/bin/bash
  2. #MIT LICENSE
  3. #
  4. #Copyright (c) 2018 Travis CI GmbH <contact+travis-build@travis-ci.org>
  5. #
  6. #Permission is hereby granted, free of charge, to any person obtaining a copy of
  7. #this software and associated documentation files (the "Software"), to deal in
  8. #the Software without restriction, including without limitation the rights to
  9. #use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10. #the Software, and to permit persons to whom the Software is furnished to do so,
  11. #subject to the following conditions:
  12. #
  13. #The above copyright notice and this permission notice shall be included in all
  14. #copies or substantial portions of the Software.
  15. #
  16. #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. #FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. #COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. #IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. #CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. # From https://github.com/tianon/travis-build/blob/e3400b7bd417407492e3916e9f7a62315f584ad5/lib/travis/build/templates/header.sh
  23. ANSI_RED="\033[31;1m"
  24. ANSI_GREEN="\033[32;1m"
  25. ANSI_YELLOW="\033[33;1m"
  26. ANSI_RESET="\033[0m"
  27. ANSI_CLEAR="\033[0K"
  28. # Number of attempts.
  29. RETRY_COUNT=2
  30. travis_retry() {
  31. local result=0
  32. local count=1
  33. while [ $count -le $RETRY_COUNT ]; do
  34. [ $result -ne 0 ] && {
  35. echo -e "\n${ANSI_RED}The command \"$@\" failed. Retrying, $count of ${RETRY_COUNT}.${ANSI_RESET}\n" >&2
  36. }
  37. "$@" && { result=0 && break; } || result=$?
  38. count=$(($count + 1))
  39. sleep 1
  40. done
  41. [ $count -gt $RETRY_COUNT ] && {
  42. echo -e "\n${ANSI_RED}The command \"$@\" failed ${RETRY_COUNT} times.${ANSI_RESET}\n" >&2
  43. }
  44. return $result
  45. }
  46. travis_retry "$@"