Dealing With Flakey CI Commands With a Retry Loop in Bash

| ci | devops |

One of the most frustrating things to deal with in Continuous Integration is flakey commands. Whether it’s flakey tests, or intermittent networking issues, when your build fails for issues outside of your control not only does it cause frustration, it reduces the trust in your CI process.

One strategy for dealing with this type of issue is to introduce some retry logic into your commands. This can easily be accomplished with good old bash.

For example, pretend that I have $FLAKEY_COMMAND and I want to retry it three times before finally failing my build. I could wrap the whole thing up in a bash loop like this.

counter=1
max=3
$FLAKEY_COMMAND

while [[ $? -ne 0 && $counter -lt $max ]]; do
    counter=$((counter+1))
    $FLAKEY_COMMAND
done

This script will run my command, if the exit code (the output of $?) is non zero (i.e something went wrong) and my counter is less than three, then it will retry the command. You can increase or decrease the number of attempts by adjusting the max variable.

This is not a foolproof strategy, but is one approach to handle flakey commands in your CI pipeline.

Thank you for reading! Share your thoughts with me on bluesky, mastodon, or via email.

Check out some more stuff to read down below.

Most popular posts this month

Recent Favorite Blog Posts

This is a collection of the last 8 posts that I bookmarked.

Articles from blogs I follow around the net

Pluralistic: Boss-politics antitrust and the MAGA crackup (29 Jul 2025)

Today's links Boss-politics antitrust and the MAGA crackup: The Tunney Act stirs the pot. Hey look at this: Delights to delectate. Object permanence: Hearware, 10,000 superballs; Bitcoin is not socialist; Stupid and dangerous video game cheating lawsu…

via Pluralistic: Daily links from Cory Doctorow July 29, 2025

What Are We Losing With AI?

Once it’s well integrated into our lives, any new technology can be seen as an exchange. We lose something and get something else in return. When we started using our calculator app, we lost the ability to do basic operations by hand, but we got accurate r…

via Articles on Jose M. July 29, 2025

Crypto lender Abra pauses withdrawals for international customers

The Abra cryptocurrency lender sent an email to customers announcing that "Abra Earn international services are currently paused, effective immediately", attributing the decision to "broader risk management efforts" a…

via Web3 is Going Just Great July 29, 2025

Generated by openring