Skip to content

Commit 8916c3f

Browse files
committed
[CI] Added CI failure notifications.
1 parent 275d74b commit 8916c3f

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

.github/workflows/Biohazrd.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ env:
1111
jobs:
1212
build-and-test:
1313
strategy:
14+
fail-fast: false
1415
matrix:
1516
configuration: ['Debug', 'Release']
1617
name: Build and Test ${{matrix.configuration}}
@@ -38,3 +39,30 @@ jobs:
3839
# ----------------------------------------------------------------------- Test
3940
- name: Test
4041
run: dotnet test --no-restore --no-build --configuration ${{matrix.configuration}} --verbosity normal
42+
43+
send-ci-failure-notification:
44+
name: Send CI Failure Notification
45+
needs: build-and-test
46+
if: failure() && github.event_name != 'pull_request'
47+
continue-on-error: true
48+
runs-on: ubuntu-latest
49+
steps:
50+
# ----------------------------------------------------------------------- Checkout
51+
- name: Checkout
52+
uses: actions/checkout@v2
53+
54+
# ----------------------------------------------------------------------- Setup Python
55+
- name: Setup Python 3.8
56+
uses: actions/setup-python@v2
57+
with:
58+
python-version: '3.8'
59+
60+
# ----------------------------------------------------------------------- Send CI Failure Notification
61+
- name: Send Notification
62+
run: python .github/workflows/send-ci-failure-notification.py
63+
env:
64+
webhook_url: ${{secrets.TEAMS_WEBHOOK_URL}}
65+
github_organization: ${{github.repository_owner}}
66+
github_repo: ${{github.repository}}
67+
github_workflow_name: ${{github.workflow}}
68+
github_run_number: ${{github.run_id}}

.github/workflows/gha.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# GitHub Actions Utility Functions
2+
# https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions
3+
import os
4+
import sys
5+
6+
errors_were_printed = False
7+
8+
def fail_if_errors():
9+
if errors_were_printed:
10+
print("Exiting due to previous errors.", file=sys.stderr)
11+
sys.exit(1)
12+
13+
def print_error(message):
14+
global errors_were_printed
15+
errors_were_printed = True
16+
print(f"::error::{message}", file=sys.stderr)
17+
18+
def print_warning(message):
19+
print(f"::warning::{message}", file=sys.stderr)
20+
21+
def set_output(name, value):
22+
if isinstance(value, bool):
23+
value = "true" if value else "false"
24+
print(f"::set-output name={name}::{value}")
25+
26+
def github_file_command(command, message):
27+
command = f"GITHUB_{command}"
28+
command_file = os.getenv(command)
29+
30+
if command_file is None:
31+
print_error(f"Missing required GitHub environment variable '{command}'")
32+
sys.exit(1)
33+
34+
if not os.path.exists(command_file):
35+
print_error(f"'{command}' points to non-existent file '{command_file}')")
36+
sys.exit(1)
37+
38+
with open(command_file, 'a') as command_file_handle:
39+
command_file_handle.write(message)
40+
command_file_handle.write('\n')
41+
42+
def set_environment_variable(name, value):
43+
github_file_command("ENV", f"{name}={value}")
44+
45+
def add_path(path):
46+
github_file_command("PATH", path)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import sys
4+
from urllib import request
5+
6+
def get_environment_variable(name):
7+
ret = os.getenv(name)
8+
9+
if ret is None or ret == '':
10+
gha.print_error(f"Missing required parameter '{name}'")
11+
12+
return ret
13+
14+
webhook_url = get_environment_variable('webhook_url')
15+
github_repo = get_environment_variable('github_repo')
16+
github_workflow_name = get_environment_variable('github_workflow_name')
17+
github_run_number = get_environment_variable('github_run_number')
18+
19+
card_data = f'''{{
20+
"type": "message",
21+
"attachments": [
22+
{{
23+
"contentType": "application/vnd.microsoft.card.adaptive",
24+
"contentUrl": null,
25+
"content": {{
26+
"type": "AdaptiveCard",
27+
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
28+
"version": "1.2",
29+
"body": [
30+
{{
31+
"type": "TextBlock",
32+
"text": "{github_repo}: {github_workflow_name} Failed!",
33+
"wrap": true,
34+
"weight": "bolder"
35+
}},
36+
{{
37+
"type": "ActionSet",
38+
"actions": [
39+
{{
40+
"type": "Action.OpenUrl",
41+
"title": "Show Run",
42+
"url": "https://github.com/{github_repo}/actions/runs/{github_run_number}"
43+
}}
44+
]
45+
}}
46+
]
47+
}}
48+
}}
49+
]
50+
}}'''
51+
card_data = card_data.encode('utf-8')
52+
53+
webhook_request = request.Request(webhook_url, data=card_data, headers={'Content-Type': 'application/json'})
54+
webhook_response = request.urlopen(webhook_request)
55+
56+
response_string = webhook_response.read().decode('utf-8')
57+
58+
if response_string != '1' or webhook_response.status != 200:
59+
print(f"Response code: {webhook_response.status}")
60+
print(f"Response body: {response_string}")
61+
sys.exit(1)

0 commit comments

Comments
 (0)