Skip to content

Instantly share code, notes, and snippets.

@Sija
Created April 11, 2020 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sija/4b14abb38ab9cd39f5f24d3626389c6f to your computer and use it in GitHub Desktop.
Save Sija/4b14abb38ab9cd39f5f24d3626389c6f to your computer and use it in GitHub Desktop.
CircleCI Orb allowing to retry jobs failing with certain pattern
# This code is licensed from CircleCI to the user under the MIT license. See
# https://circleci.com/orbs/registry/licensing for details.
version: 2.1
description: Run commands with retry
commands:
run-with-retry:
description: Run command with retry
parameters:
command:
description: Command to run
type: string
pattern:
description: Pattern on which to retry
type: string
retry-count:
description: Number of retries
type: integer
default: 3
sleep:
description: Wait duration until next retry
type: integer
default: 5
steps:
- run: |
retry() {
PATTERN=<< parameters.pattern >>
MAX_RETRY=<< parameters.retry-count >>
error=""
n=0
until [ $n -ge $MAX_RETRY ]; do
error=$( cmd "$@" 2>&1 >/dev/null )
if [ $? -eq 0 ]; then; break; fi
if [[ ! $error =~ $PATTERN ]]; then; break; fi
n=$[$n+1]
sleep << parameters.sleep >>
done
if [ $n -ge $MAX_RETRY ]; then
echo "failed: ${@}" >&2
echo "with error: $error" >&2
exit 1
fi
}
retry << parameters.command >>
@mpushkin
Copy link

mpushkin commented May 4, 2022

Hi, it looks like if there is an error that doesn't match the pattern, it will quit the retry loop, but then it won't report this as failure because number of max retries wasn't reached.
Looks like a bug to me, what do you think ?

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