action.yml 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. name: 'Retry Command'
  2. description: 'Run a shell command, retrying up to N attempts with backoff; fails if all attempts fail'
  3. inputs:
  4. command:
  5. description: 'Shell command(s) to run'
  6. required: true
  7. attempts:
  8. description: 'Maximum number of attempts (default 5)'
  9. required: false
  10. default: '5'
  11. delay:
  12. description: 'Seconds to wait between attempts (default 5)'
  13. required: false
  14. default: '5'
  15. workdir:
  16. description: 'Working directory to run in (default: workspace)'
  17. required: false
  18. default: '${{ github.workspace }}'
  19. runs:
  20. using: composite
  21. steps:
  22. - shell: bash
  23. working-directory: ${{ inputs.workdir }}
  24. run: |
  25. set -uo pipefail
  26. attempts="${{ inputs.attempts }}"
  27. delay="${{ inputs.delay }}"
  28. cmd() {
  29. ${{ inputs.command }}
  30. }
  31. for i in $(seq 1 "$attempts"); do
  32. echo "::group::retry attempt $i/$attempts"
  33. cmd
  34. rc=$?
  35. echo "::endgroup::"
  36. if [ "$rc" -eq 0 ]; then
  37. echo "retry: command succeeded on attempt $i"
  38. exit 0
  39. fi
  40. if [ "$i" -lt "$attempts" ]; then
  41. echo "retry: attempt $i failed (rc=$rc); retrying in ${delay}s"
  42. sleep "$delay"
  43. else
  44. echo "::error::retry: command failed after $attempts attempts (rc=$rc)"
  45. fi
  46. done
  47. exit 1