Last active
April 7, 2017 05:21
-
-
Save toolmantim/8313dc5ededd66b88077aeaa0c71f593 to your computer and use it in GitHub Desktop.
A Bash function you can use in scripts to retry Buildkite steps. License: MIT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Retries a command a number of times, with exponential backoff | |
# | |
# Usage: | |
# retry <number-of-retries> <command> | |
# | |
# Examples: | |
# retry 3 make test | |
# retry 5 curl -O https://buildkite.com/ | |
function retry { | |
local retries=$1; shift | |
local attempts=1 | |
local status | |
until "$@"; do | |
status=$? | |
if (( attempts == retries )); then | |
echo "Failed $attempts retries" | |
return 1 | |
else | |
echo "Retrying $((retries - attempts)) more times..." | |
attempts=$((attempts + 1)) | |
sleep $(((attempts - 2) * 2)) | |
fi | |
done | |
return $status | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment