name: Retry Deploy on: workflow_run: workflows: [Deploy] types: [completed] permissions: actions: write jobs: retry: if: >- ${{ (gitea.event.workflow_run.conclusion == 'failure' || gitea.event.workflow_run.conclusion == 'timed_out') && gitea.event.workflow_run.run_attempt >= 1 && gitea.event.workflow_run.run_attempt <= 3 }} runs-on: ubuntu-latest steps: - name: Retry the entire Deploy workflow env: GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} GITEA_SERVER_URL: ${{ gitea.server_url }} GITEA_REPOSITORY: ${{ gitea.repository }} RUN_ID: ${{ gitea.event.workflow_run.id }} RUN_ATTEMPT: ${{ gitea.event.workflow_run.run_attempt }} run: | python3 - <<'PY' import json import os from urllib.request import Request, urlopen max_retries = 3 event_attempt = int(os.environ['RUN_ATTEMPT']) server = os.environ['GITEA_SERVER_URL'].rstrip('/') repository = os.environ['GITEA_REPOSITORY'] run_id = os.environ['RUN_ID'] run_url = f'{server}/api/v1/repos/{repository}/actions/runs/{run_id}' headers = { 'Authorization': f'token {os.environ["GITEA_TOKEN"]}', 'Accept': 'application/json', } with urlopen(Request(run_url, headers=headers), timeout=30) as response: current_run = json.load(response) # Ignore stale completion events if a newer attempt has already started. if (current_run['run_attempt'] != event_attempt or current_run['status'] not in ('completed', 'failure', 'timed_out') or current_run['conclusion'] not in ('failure', 'timed_out') or not 1 <= event_attempt <= max_retries): print('No retry needed for this completion event.') else: request = Request(f'{run_url}/rerun', headers=headers, method='POST') with urlopen(request, timeout=30) as response: response.read() print(f'Retrying all jobs: retry {event_attempt}/{max_retries}.') PY