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…


This content originally appeared on DEV Community and was authored by K@zuki.

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

Retry interval to a random time

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

Set working directory

- uses: corrupt952/actions-retry-command@v1
  with

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

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))

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 }}"

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.


This content originally appeared on DEV Community and was authored by K@zuki.


Print Share Comment Cite Upload Translate Updates
APA

K@zuki. | Sciencx (2024-08-19T03:01:59+00:00) Introducing a Custom GitHub Action for Command Retry with Output Capture. Retrieved from https://www.scien.cx/2024/08/19/introducing-a-custom-github-action-for-command-retry-with-output-capture/

MLA
" » Introducing a Custom GitHub Action for Command Retry with Output Capture." K@zuki. | Sciencx - Monday August 19, 2024, https://www.scien.cx/2024/08/19/introducing-a-custom-github-action-for-command-retry-with-output-capture/
HARVARD
K@zuki. | Sciencx Monday August 19, 2024 » Introducing a Custom GitHub Action for Command Retry with Output Capture., viewed ,<https://www.scien.cx/2024/08/19/introducing-a-custom-github-action-for-command-retry-with-output-capture/>
VANCOUVER
K@zuki. | Sciencx - » Introducing a Custom GitHub Action for Command Retry with Output Capture. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/19/introducing-a-custom-github-action-for-command-retry-with-output-capture/
CHICAGO
" » Introducing a Custom GitHub Action for Command Retry with Output Capture." K@zuki. | Sciencx - Accessed . https://www.scien.cx/2024/08/19/introducing-a-custom-github-action-for-command-retry-with-output-capture/
IEEE
" » Introducing a Custom GitHub Action for Command Retry with Output Capture." K@zuki. | Sciencx [Online]. Available: https://www.scien.cx/2024/08/19/introducing-a-custom-github-action-for-command-retry-with-output-capture/. [Accessed: ]
rf:citation
» Introducing a Custom GitHub Action for Command Retry with Output Capture | K@zuki. | Sciencx | https://www.scien.cx/2024/08/19/introducing-a-custom-github-action-for-command-retry-with-output-capture/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.