Skip to content

Rerun Flaky Workflows #4165

Rerun Flaky Workflows

Rerun Flaky Workflows #4165

name: Rerun Flaky Workflows
on:
workflow_run:
workflows: ["Tests"]
types: [completed]
jobs:
rerun-on-failure:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'failure'}}
permissions:
# permission required to rerun a workflow https://docs.github.com/en/rest/authentication/permissions-required-for-github-apps?apiVersion=2022-11-28#repository-permissions-for-actions
# gh run rerun --failed requires write permission
actions: write
steps:
- name: Display Workflow Run Information
env:
WORKFLOW_RUN_ID: ${{ github.event.workflow_run.id }}
WORKFLOW_RUN_NAME: ${{ github.event.workflow_run.name }}
WORKFLOW_RUN_ATTEMPT: ${{ github.event.workflow_run.run_attempt }}
WORKFLOW_RUN_HTML_URL: ${{ github.event.workflow_run.html_url }}
GITHUB_JOB: ${{ github.job }}
GITHUB_RUN_ATTEMPT: ${{ github.run_attempt }}
AUTHOR_NAME: ${{ github.event.workflow_run.head_commit.author.name }}
GITHUB_ACTOR: ${{ github.actor }}
run: |
echo "The Workflow Run $WORKFLOW_RUN_NAME has failed! " >> $GITHUB_STEP_SUMMARY
echo "View the failed run attempt ($WORKFLOW_RUN_ATTEMPT) here: WORKFLOW_RUN_HTML_URL" >> $GITHUB_STEP_SUMMARY
echo "Workflow Run ID: $WORKFLOW_RUN_ID"
echo "Job ID: $GITHUB_JOB"
echo "Current run: $GITHUB_RUN_ATTEMPT"
echo "Author: $AUTHOR_NAME"
echo "Actor: $GITHUB_ACTOR"
- name: Rerun failed jobs in the current workflow
env:
GH_TOKEN: ${{ github.token }}
WORKFLOW_RUN_ATTEMPT: ${{ github.event.workflow_run.run_attempt }}
WORKFLOW_RUN_ID: ${{ github.event.workflow_run.id }}
GITHUB_REPOSITORY: ${{ github.repository }}
run: |
MAX_RUNS=2
CURRENT_RUN="$WORKFLOW_RUN_ATTEMPT"
if [ $CURRENT_RUN -lt $MAX_RUNS ]; then
gh run rerun "$WORKFLOW_RUN_ID" --repo "$GITHUB_REPOSITORY" --failed
else
exit 1
fi
- name: Check out the repo
if: ${{ !cancelled() && failure() }} # Run only if the previous step failed
uses: actions/checkout@v4
- name: Update env vars
if: ${{ !cancelled() && failure() }} # Run only if the previous step failed
env:
WORKFLOW_RUN_HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
run: |
commit_short="$(git rev-parse --short $WORKFLOW_RUN_HEAD_SHA)"
echo "BREAKING_COMMIT_SHORT=$commit_short" >> $GITHUB_ENV
- name: Slack Notification
uses: ./.github/actions/slack-notification
if: ${{ !cancelled() && failure() && github.event.workflow_run.head_branch == 'main'}} # Run only if the previous step failed
continue-on-error: true
env:
BREAKING_COMMIT: "${{ github.event.workflow_run.head_sha }}"
RUN_URL: "${{ github.event.workflow_run.html_url }}"
RUN_NAME: "${{ github.event.workflow_run.name }}"
BRANCH: "${{ github.event.workflow_run.head_branch }}"
REPO: "${{ github.repository }}"
WORKFLOW_NAME: "${{ github.event.workflow.name }}"
WORKFLOW_HEAD_BRANCH: "${{ github.event.workflow_run.head_branch }}"
GITHUB_ACTOR: "${{ github.actor }}"
with:
vault-url: ${{ secrets.VAULT_ADDR }}
vault-role-id: ${{ secrets.CSP_VAULT_ROLE_ID }}
vault-secret-id: ${{ secrets.CSP_VAULT_SECRET_ID }}
slack-payload: |
{
"text": ":alert: CI ${{ env.WORKFLOW_NAME }} is failing on ${{ env.WORKFLOW_HEAD_BRANCH }} :alert:",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": ":alert: CI ${{ env.WORKFLOW_NAME }} is failing on ${{ env.WORKFLOW_HEAD_BRANCH }} :alert:",
"emoji": true
}
}
],
"attachments": [
{
"color": "#D40E0D",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Commit <https://github.com/elastic/cloudbeat/commit/${{env.BREAKING_COMMIT}}|${{env.BREAKING_COMMIT_SHORT}}> by ${{env.GITHUB_ACTOR}} has failing tests on the `${{env.BRANCH}}` branch."
}
},
{
"type": "divider"
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Failing Commit",
"emoji": true
},
"url": "https://github.com/elastic/cloudbeat/commit/${{env.BREAKING_COMMIT}}"
},
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Failed CI Run",
"emoji": true
},
"url": "${{env.RUN_URL}}"
}
]
}
]
}
]
}