-
Notifications
You must be signed in to change notification settings - Fork 42
131 lines (127 loc) · 5.49 KB
/
rerun-flaky-workflows.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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}}"
}
]
}
]
}
]
}