| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- name: 'Retry Command'
- description: 'Run a shell command, retrying up to N attempts with backoff; fails if all attempts fail'
- inputs:
- command:
- description: 'Shell command(s) to run'
- required: true
- attempts:
- description: 'Maximum number of attempts (default 5)'
- required: false
- default: '5'
- delay:
- description: 'Seconds to wait between attempts (default 5)'
- required: false
- default: '5'
- workdir:
- description: 'Working directory to run in (default: workspace)'
- required: false
- default: '${{ github.workspace }}'
- runs:
- using: composite
- steps:
- - shell: bash
- working-directory: ${{ inputs.workdir }}
- run: |
- set -uo pipefail
- attempts="${{ inputs.attempts }}"
- delay="${{ inputs.delay }}"
- cmd() {
- ${{ inputs.command }}
- }
- for i in $(seq 1 "$attempts"); do
- echo "::group::retry attempt $i/$attempts"
- cmd
- rc=$?
- echo "::endgroup::"
- if [ "$rc" -eq 0 ]; then
- echo "retry: command succeeded on attempt $i"
- exit 0
- fi
- if [ "$i" -lt "$attempts" ]; then
- echo "retry: attempt $i failed (rc=$rc); retrying in ${delay}s"
- sleep "$delay"
- else
- echo "::error::retry: command failed after $attempts attempts (rc=$rc)"
- fi
- done
- exit 1
|