Cron Retry Failed Workflows #267
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Cron Retry Failed Workflows | |
on: | |
schedule: | |
- cron: '0 */5 * * *' | |
workflow_dispatch: | |
jobs: | |
retry-failed: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Re-run failed workflow runs on the current branch | |
uses: actions/github-script@v6 | |
if: github.repository == 'openframeworks/apothecary' && github.event_name == 'push' && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/bleeding' || contains(github.ref, 'refs/tags/')) | |
with: | |
script: | | |
// Remove 'refs/heads/' from the branch name | |
const branch = process.env.GITHUB_REF.replace('refs/heads/', ''); | |
console.log("Current branch:", branch); | |
// List all workflow runs for the branch with 'failure' status | |
const runsResponse = await github.rest.actions.listWorkflowRunsForRepo({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
branch: branch, | |
status: 'failure', | |
per_page: 50 // Adjust as needed | |
}); | |
const failedRuns = runsResponse.data.workflow_runs; | |
console.log(`Found ${failedRuns.length} failed workflow runs.`); | |
if (failedRuns.length === 0) { | |
console.log("No failed workflow runs found. Nothing to re-run."); | |
return; | |
} | |
// Iterate over each failed workflow run and trigger a re-run using the API | |
for (const run of failedRuns.slice(0, 5)) { // Limit to 5 runs | |
console.log(`Re-running workflow run id: ${run.id} (${run.name})`); | |
try { | |
await github.rest.actions.reRunWorkflow({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
run_id: run.id | |
}); | |
} catch (error) { | |
console.error(`Failed to re-run run ${run.id}: ${error.message}`); | |
} | |
await new Promise(resolve => setTimeout(resolve, 1000)); // 1-second delay | |
} |