-
Notifications
You must be signed in to change notification settings - Fork 22
117 lines (103 loc) · 4.26 KB
/
failure-notification.yaml
File metadata and controls
117 lines (103 loc) · 4.26 KB
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
name: Create Issue on Workflow Failure
permissions:
contents: read
issues: write
on:
workflow_call:
inputs:
workflow_name:
description: "Name of the failed workflow"
required: true
type: string
job_name:
description: "Name of the failed job(s)"
required: false
type: string
default: "Unknown"
failure_reason:
description: "Reason for the failure(s)"
required: false
type: string
default: "Unknown failure reason"
jobs:
create-failure-issue:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v5
- name: Create Issue
uses: actions/github-script@v7
env:
WORKFLOW_NAME: ${{ inputs.workflow_name }}
JOB_NAME: ${{ inputs.job_name }}
FAILURE_REASON: ${{ inputs.failure_reason }}
with:
script: |
const workflowName = process.env.WORKFLOW_NAME;
const jobName = process.env.JOB_NAME;
const failureReason = process.env.FAILURE_REASON;
const runUrl = `${context.payload.repository.html_url}/actions/runs/${context.runId}`;
const commitSha = context.sha;
const commitUrl = `${context.payload.repository.html_url}/commit/${commitSha}`;
const branch = context.ref.replace('refs/heads/', '');
const actor = context.actor;
// Check if there's already an open issue for this workflow
const existingIssues = await github.rest.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
labels: 'workflow-failure,' + workflowName.toLowerCase().replace(/\s+/g, '-')
});
const title = `🚨 Workflow Failure: ${workflowName}`;
const body = `## Workflow Failure Report
**Workflow:** ${workflowName}
**Job:** ${jobName}
**Branch:** ${branch}
**Commit:** [${commitSha.substring(0, 7)}](${commitUrl})
**Triggered by:** @${actor}
**Run URL:** [View Failed Run](${runUrl})
### Failure Details
${failureReason}
### Debugging Information
- **Timestamp:** ${new Date().toISOString()}
- **Repository:** ${context.payload.repository.full_name}
- **Event:** ${context.eventName}
### Next Steps
1. Check the [workflow run logs](${runUrl}) for detailed error information
2. Review the changes in [commit ${commitSha.substring(0, 7)}](${commitUrl})
3. Fix the issue and re-run the workflow
4. Close this issue once resolved
---
*This issue was automatically created by the failure notification workflow.*`;
// If no existing open issue, create a new one
if (existingIssues.data.length === 0) {
await github.rest.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: title,
body: body,
labels: [
'workflow-failure',
'bug',
workflowName.toLowerCase().replace(/\s+/g, '-'),
'automated'
]
});
console.log(`Created new issue for ${workflowName} failure`);
} else {
console.log(`Issue already exists for ${workflowName} failure`);
// Optionally add a comment to the existing issue
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: existingIssues.data[0].number,
body: `## Additional Failure Report
**New failure detected:**
- **Job:** ${jobName}
- **Commit:** [${commitSha.substring(0, 7)}](${commitUrl})
- **Run URL:** [View Failed Run](${runUrl})
- **Timestamp:** ${new Date().toISOString()}
${failureReason}`
});
console.log(`Added comment to existing issue for ${workflowName} failure`);
}