-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yaml
More file actions
175 lines (158 loc) · 7.84 KB
/
Copy pathaction.yaml
File metadata and controls
175 lines (158 loc) · 7.84 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
---
name: Send Notification via Slack
description: "This action sends notifications for different types of workflows"
inputs:
channel-id:
description: The channel to sent the message to
type:
description: |
The type of notification to send. Supported types are `container-image-build`
and `integration-test`.
build-result:
description: The result of the build job
publish-manifests-result:
description: The result of the publish manifests job
publish-helm-chart-result:
description: The result of the publish helm chart job (if any)
default: "N/A"
generate-provenance-result:
description: The result of the generate provenance job (if any)
default: "N/A"
test-result:
description: The result of an integration test
test-health:
description: The health of the past few integration tests
failed-tests:
description: The list (plain text) of failed tests
slack-token:
description: The Slack token
message-subject:
description: |
The main message subject of the notification message. This defaults to github.workflow for
build notifications and github.repository for integration test notifications.
runs:
using: composite
steps:
- name: Validate Inputs
id: valid_inputs
env:
NOTIFICATION_TYPE: ${{ inputs.type }}
PUBLISH_MANIFESTS_RESULT: ${{ inputs.publish-manifests-result }}
PUBLISH_HELM_CHART_RESULT: ${{ inputs.publish-helm-chart-result }}
GENERATE_PROVENANCE_RESULT: ${{ inputs.generate-provenance-result }}
BUILD_RESULT: ${{ inputs.build-result }}
FAILED_TESTS: ${{ inputs.failed-tests }}
TEST_RESULT: ${{ inputs.test-result }}
TEST_HEALTH: ${{ inputs.test-health }}
shell: bash
run: |
if [ -z "${NOTIFICATION_TYPE:-}" ]; then
echo "The type input must be provided"
exit 1
fi
if [ "$NOTIFICATION_TYPE" == "container-image-build" ]; then
[ -z "${PUBLISH_MANIFESTS_RESULT:-}" ] && echo "The publish-manifests-result input must be provided" && exit 1
[ -z "${BUILD_RESULT:-}" ] && echo "The build-result input must be provided" && exit 1
echo "PUBLISH_MANIFESTS_RESULT=$PUBLISH_MANIFESTS_RESULT" | tee -a "$GITHUB_OUTPUT"
echo "PUBLISH_HELM_CHART_RESULT=$PUBLISH_HELM_CHART_RESULT" | tee -a "$GITHUB_OUTPUT"
echo "GENERATE_PROVENANCE_RESULT=$GENERATE_PROVENANCE_RESULT" | tee -a "$GITHUB_OUTPUT"
echo "BUILD_RESULT=$BUILD_RESULT" | tee -a "$GITHUB_OUTPUT"
elif [ "$NOTIFICATION_TYPE" == "integration-test" ]; then
[ -z "${TEST_RESULT:-}" ] && echo "The test-result input must be provided" && exit 1
[ -z "${TEST_HEALTH:-}" ] && echo "The test-health input must be provided" && exit 1
echo "FAILED_TESTS=${FAILED_TESTS:-No failed tests}" | tee -a "$GITHUB_OUTPUT"
echo "TEST_RESULT=$TEST_RESULT" | tee -a "$GITHUB_OUTPUT"
echo "TEST_HEALTH=$TEST_HEALTH" | tee -a "$GITHUB_OUTPUT"
else
echo "Supported notification types are: 'container-image-build' and 'integration-test'"
exit 1
fi
echo "NOTIFICATION_TYPE=$NOTIFICATION_TYPE" | tee -a "$GITHUB_OUTPUT"
- name: Retrieve Slack Thread ID
id: retrieve-slack-thread-id
continue-on-error: true
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: slack-thread-id-${{ github.run_id }}
- name: Extract Slack Thread ID into Environment Variable
id: extract
if: steps.retrieve-slack-thread-id.outcome == 'success'
shell: bash
run: |
echo "SLACK_THREAD_ID=$(cat slack-thread-id)" | tee -a "$GITHUB_OUTPUT"
- name: Provide message template variables
id: template_variables
env:
PUBLISH_MANIFESTS_RESULT: ${{ steps.valid_inputs.outputs.PUBLISH_MANIFESTS_RESULT }}
PUBLISH_HELM_CHART_RESULT: ${{ steps.valid_inputs.outputs.PUBLISH_HELM_CHART_RESULT }}
GENERATE_PROVENANCE_RESULT: ${{ steps.valid_inputs.outputs.GENERATE_PROVENANCE_RESULT }}
BUILD_RESULT: ${{ steps.valid_inputs.outputs.BUILD_RESULT }}
FAILED_TESTS: ${{ steps.valid_inputs.outputs.FAILED_TESTS }}
TEST_HEALTH: ${{ steps.valid_inputs.outputs.TEST_HEALTH }}
NOTIFICATION_TYPE: ${{ steps.valid_inputs.outputs.NOTIFICATION_TYPE }}
GITHUB_RUN_ATTEMPT: ${{ github.run_attempt }}
GITHUB_SERVER_URL: ${{ github.server_url }}
GITHUB_REPOSITORY: ${{ github.repository }}
GITHUB_WORKFLOW: ${{ github.workflow }}
GITHUB_RUN_ID: ${{ github.run_id }}
SLACK_THREAD_YAML: |
${{ steps.retrieve-slack-thread-id.outcome == 'success' && format('thread_ts: "{0}"', steps.extract.outputs.SLACK_THREAD_ID) || '' }}
CHANNEL_ID: ${{ inputs.channel-id }}
MESSAGE_SUBJECT: ${{ inputs.message-subject }}
shell: bash
run: |
export WORKFLOW_RUN_URL="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}/attempts/${GITHUB_RUN_ATTEMPT}"
if [ "$NOTIFICATION_TYPE" == "container-image-build" ]; then
# Only override the message subject with the default value if it is empty
if [ -z "${MESSAGE_SUBJECT:-}" ]; then
export MESSAGE_SUBJECT="$GITHUB_WORKFLOW"
fi
# TODO (@Techassi): Also add success template
if [ "$PUBLISH_MANIFESTS_RESULT" = "failure" ] || [ "$PUBLISH_HELM_CHART_RESULT" = "failure" ] || [ "$GENERATE_PROVENANCE_RESULT" = "failure" ] || [ "$BUILD_RESULT" = "failure" ]; then
export MESSAGE_VERB=failed
export MESSAGE_COLOR=aa0000
else
export MESSAGE_VERB=succeeded
export MESSAGE_COLOR=10c400
fi
export MESSAGE_TEXT="*$MESSAGE_SUBJECT* $MESSAGE_VERB (attempt $GITHUB_RUN_ATTEMPT)"
PAYLOAD=$(envsubst < "${GITHUB_ACTION_PATH}/templates/container-image-build/failure.tpl")
echo -e "PAYLOAD<<EOF\n$PAYLOAD\nEOF" | tee -a "$GITHUB_OUTPUT"
elif [ "$NOTIFICATION_TYPE" == "integration-test" ]; then
# Only override the message subject with the default value if it is empty
if [ -z "${MESSAGE_SUBJECT:-}" ]; then
export MESSAGE_SUBJECT="$GITHUB_REPOSITORY"
fi
export HEALTH_SLACK_EMOJI=$(echo "$TEST_HEALTH" | cut -d ',' -f 1)
export HEALTH_RATE=$(echo "$TEST_HEALTH" | cut -d ',' -f 3)
if [ "$TEST_RESULT" == "failure" ]; then
export MESSAGE_TEXT="The integration test for *`$MESSAGE_SUBJECT`* failed."
PAYLOAD=$(envsubst < "${GITHUB_ACTION_PATH}/templates/integration-test/failure.tpl")
echo -e "PAYLOAD<<EOF\n$PAYLOAD\nEOF" | tee -a "$GITHUB_OUTPUT"
else
export MESSAGE_TEXT="The integration test for *`$MESSAGE_SUBJECT`* succeeded."
PAYLOAD=$(envsubst < "${GITHUB_ACTION_PATH}/templates/integration-test/success.tpl")
echo -e "PAYLOAD<<EOF\n$PAYLOAD\nEOF" | tee -a "$GITHUB_OUTPUT"
fi
fi
- name: Send Notification
id: send-notification
uses: slackapi/slack-github-action@45a88b9581bfab2566dc881e2cd66d334e621e2c # v3.0.3
with:
method: chat.postMessage
token: ${{ inputs.slack-token }}
payload: ${{ steps.template_variables.outputs.PAYLOAD }}
payload-templated: false
- name: Save Slack Thread ID to File
if: steps.retrieve-slack-thread-id.outcome == 'failure'
env:
SLACK_THREAD_ID_OUTPUT: ${{ steps.send-notification.outputs.ts }}
shell: bash
run: |
echo "$SLACK_THREAD_ID_OUTPUT" > slack-thread-id
- name: Store Slack Thread ID as Artifact
if: steps.retrieve-slack-thread-id.outcome == 'failure'
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: slack-thread-id-${{ github.run_id }}
path: slack-thread-id