Re-run Flaky Workflows #15
This file contains 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: Re-run Flaky Workflows | |
on: | |
workflow_run: | |
workflows: [Backend, Driver Tests, E2E Tests, Frontend] | |
types: [completed] | |
branches: [master, 'release-**'] | |
jobs: | |
rerun-on-failure: | |
name: 'Re-run ''${{ github.event.workflow_run.name }}'' workflow' | |
runs-on: ubuntu-22.04 | |
# Do not re-run scheduled workflow runs. That's only Replay.io E2E tests for now. | |
if: github.event.workflow_run.conclusion == 'failure' && github.event.workflow_run.event != 'schedule' | |
steps: | |
- name: Generate job summary | |
run: | | |
echo "# ${{ github.event.workflow_run.name }} workflow failed! :x:" >> $GITHUB_STEP_SUMMARY | |
echo "" >> $GITHUB_STEP_SUMMARY | |
echo "View the failed run attempt (#${{ github.event.workflow_run.run_attempt }}) using the following link:" >> $GITHUB_STEP_SUMMARY | |
echo "${{ github.event.workflow_run.html_url }}" >> $GITHUB_STEP_SUMMARY | |
- uses: actions/setup-node@v3 | |
with: | |
node-version: lts/Hydrogen # 18.x.x | |
- run: npm install @slack/web-api | |
- name: Trigger a re-run | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const MAX_ATTEMPTS = 2; | |
const ATTEMPT = "${{ github.event.workflow_run.run_attempt }}"; | |
const BRANCH = "${{ github.event.workflow_run.head_branch }}"; | |
const FAILED_RUN_URL = "${{ github.event.workflow_run.html_url }}"; | |
const FAILED_RUN_NAME = "${{ github.event.workflow_run.name }}"; | |
const AUTHOR = "${{ github.event.workflow_run.head_commit.author.name }}"; | |
const BREAKING_COMMIT = "${{ github.event.workflow_run.head_sha }}"; | |
if (ATTEMPT <= MAX_ATTEMPTS) { | |
github.rest.actions.reRunWorkflowFailedJobs({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
run_id: ${{ github.event.workflow_run.id }}, | |
}); | |
} else { | |
// notify slack of repeated failure | |
const { WebClient } = require('@slack/web-api'); | |
const slack = new WebClient('${{ secrets.SLACK_BOT_TOKEN }}'); | |
await slack.chat.postMessage({ | |
channel: 'engineering-ci', | |
blocks: [ | |
{ | |
"type": "header", | |
"text": { | |
"type": "plain_text", | |
"text": `:alert: CI is failing on ${BRANCH} :alert:`, | |
"emoji": true, | |
} | |
}, | |
], | |
attachments: [{ | |
color: '#f85149', | |
blocks: [ | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": `Commit <https://github.com/metabase/metabase/commit/${BREAKING_COMMIT}|${BREAKING_COMMIT.slice(0,7)}> by ${AUTHOR} has failing <${FAILED_RUN_URL}|${FAILED_RUN_NAME}> tests on the \`${BRANCH}\` branch . :sad-panda:\nPlease fix ASAP. \n\nEmoji this message when it's fixed.` | |
} | |
}, | |
{ | |
"type": "actions", | |
"elements": [ | |
{ | |
"type": "button", | |
"text": { | |
"type": "plain_text", | |
"text": "Failing Commit", | |
"emoji": true | |
}, | |
"url": `https://github.com/metabase/metabase/commit/${BREAKING_COMMIT}` | |
}, | |
{ | |
"type": "button", | |
"text": { | |
"type": "plain_text", | |
"text": `Recent commits to ${BRANCH}`, | |
"emoji": true | |
}, | |
"url": `https://github.com/metabase/metabase/commits/${BRANCH}` | |
}, | |
{ | |
"type": "button", | |
"text": { | |
"type": "plain_text", | |
"text": "Failed CI Run", | |
"emoji": true | |
}, | |
"url": `${FAILED_RUN_URL}` | |
} | |
] | |
} | |
] | |
}] | |
}); | |
} |