|
| 1 | +name: Cron Retry Failed Workflows |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: '0 */5 * * *' |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +jobs: |
| 9 | + retry-failed: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + steps: |
| 12 | + - name: Re-run failed workflow runs on the current branch |
| 13 | + uses: actions/github-script@v6 |
| 14 | + if: github.repository == 'openframeworks/apothecary' && github.event_name == 'push' && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/bleeding' || contains(github.ref, 'refs/tags/')) |
| 15 | + with: |
| 16 | + script: | |
| 17 | + // Remove 'refs/heads/' from the branch name |
| 18 | + const branch = process.env.GITHUB_REF.replace('refs/heads/', ''); |
| 19 | + console.log("Current branch:", branch); |
| 20 | + |
| 21 | + // List all workflow runs for the branch with 'failure' status |
| 22 | + const runsResponse = await github.rest.actions.listWorkflowRunsForRepo({ |
| 23 | + owner: context.repo.owner, |
| 24 | + repo: context.repo.repo, |
| 25 | + branch: branch, |
| 26 | + status: 'failure', |
| 27 | + per_page: 50 // Adjust as needed |
| 28 | + }); |
| 29 | + const failedRuns = runsResponse.data.workflow_runs; |
| 30 | + console.log(`Found ${failedRuns.length} failed workflow runs.`); |
| 31 | + |
| 32 | + if (failedRuns.length === 0) { |
| 33 | + console.log("No failed workflow runs found. Nothing to re-run."); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + // Iterate over each failed workflow run and trigger a re-run using the API |
| 38 | + for (const run of failedRuns.slice(0, 5)) { // Limit to 5 runs |
| 39 | + console.log(`Re-running workflow run id: ${run.id} (${run.name})`); |
| 40 | + try { |
| 41 | + await github.rest.actions.reRunWorkflow({ |
| 42 | + owner: context.repo.owner, |
| 43 | + repo: context.repo.repo, |
| 44 | + run_id: run.id |
| 45 | + }); |
| 46 | + } catch (error) { |
| 47 | + console.error(`Failed to re-run run ${run.id}: ${error.message}`); |
| 48 | + } |
| 49 | + await new Promise(resolve => setTimeout(resolve, 1000)); // 1-second delay |
| 50 | + } |
0 commit comments