DEV Community

K@zuki.
K@zuki.

Posted on

Introducing a Custom GitHub Action for Command Retry with Output Capture

I've published a GitHub Action that I've been using in my work.
It's designed to retry specific commands, which sets it apart from similar actions available in the Marketplace.
The key feature of this action is its ability to capture the command output, which is often a limitation in existing retry actions.

Marketplace

retry-command

Retries an Action step on failure.
This action is unique compared to other actions in that it is possible to obtain the results of retries.

Marketplace

Inputs

command

Required

The command to run.

working_directory

Required

The directory in which to execute the command.

max_attempts

Required

The maximum number of times to attempt the command.
Default is 5.

retry_interval

Required

The time to wait between retry attempts, in seconds. Default is 5.
You can also write $((RANDOM % 31)) to make it a random value.

Output

exit_code

Exit code of the last command executed

result

Output of the last command executed

Example

Simple

- uses: corrupt952/actions-retry-command@v1
  with:
    command: terraform plan -no-color
    max_attempts: 3
    retry_interval: 10
Enter fullscreen mode Exit fullscreen mode

Retry interval to a random time

- uses: corrupt952/actions-retry-command@v1
  with:
    command: terraform plan -no-color
    retry_interval: $((RANDOM % 31))
Enter fullscreen mode Exit fullscreen mode

Set working directory

- uses: corrupt952/actions-retry-command@v1
  with
Enter fullscreen mode Exit fullscreen mode

How to Use

The usage is straightforward.
For example, if you want to retry terraform plan, you can use it like this:

- uses: corrupt952/actions-retry-command@v1
  with:
    command: terraform plan -no-color
    working_directory: ${{ github.workspace }}
    max_attempts: 3
    retry_interval: 10
Enter fullscreen mode Exit fullscreen mode

This will execute the command up to 3 times with a 10-second interval between retries.

Advanced Usage: Random Retry Interval

Since the action is implemented as a shell script, you can easily add randomness to the retry interval:

- uses: corrupt952/retry-command@v1
  with:
    command: terraform plan -no-color
    working_directory: ${{ github.workspace }}
    retry_interval: $((RANDOM % 31))
Enter fullscreen mode Exit fullscreen mode

This will set a random retry interval between 0 and 30 seconds.

Capturing Command Output

The main reason I created this action was to capture the command output.
Here's how you can do that:

- uses: corrupt952/retry-command@v1
  id: terraform_plan
  continue-on-error: true
  with:
    command: terraform plan -no-color
    working_directory: ${{ github.workspace }}
    max_attempts: 3
- if: steps.terraform_plan.outcome == 'failure'
  run: |
    echo "Exit code: ${{ steps.terraform_plan.outputs.exit_code }}"
    echo "Result: ${{ steps.terraform_plan.outputs.result }}"
Enter fullscreen mode Exit fullscreen mode

Currently, the action captures the result of the last successful or failed attempt.

This feature allows you to easily use the results of the retry-command in other actions.
or example, I use this to share Terraform plan results on GitHub, making it simple to review and discuss infrastructure changes within pull requests.

Conclusion

If you need similar functionality in your GitHub Actions workflows, feel free to give this action a try.
It's particularly useful when you need to both retry commands and capture their output.

Top comments (0)