1+ #! /bin/bash
2+
3+ # Slack notification script for Jenkins e2e test failures
4+ # This script sends notifications to Slack when e2e tests fail in Jenkins
5+
6+ set -e
7+
8+ # Function to send Slack notification
9+ send_slack_notification () {
10+ local status=" $1 "
11+ local message=" $2 "
12+ local webhook_url=" $3 "
13+
14+ if [ -z " $webhook_url " ]; then
15+ echo " Warning: SLACK_WEBHOOK_URL not set, skipping Slack notification"
16+ return 0
17+ fi
18+
19+ # Prepare the JSON payload
20+ local payload=$( cat << EOF
21+ {
22+ "text": "$message ",
23+ "username": "Jenkins E2E Tests"
24+ }
25+ EOF
26+ )
27+
28+ # Send the notification
29+ curl -X POST \
30+ -H " Content-type: application/json; charset=utf-8" \
31+ --data " $payload " \
32+ " $webhook_url "
33+ }
34+
35+ # Function to read value from file
36+ read_from_file () {
37+ local file_path=" $1 "
38+
39+ if [ -f " $file_path " ]; then
40+ cat " $file_path " | tr -d ' [:space:]'
41+ fi
42+ }
43+
44+ # Function to read value from notification_values.txt
45+ read_notification_value () {
46+ local key=" $1 "
47+ local file_path=" ${WORKSPACE} /notification_values.txt"
48+
49+ if [ -f " $file_path " ]; then
50+ grep " ^${key} =" " $file_path " | cut -d' =' -f2- | tr -d ' [:space:]'
51+ fi
52+ }
53+
54+ # Main execution
55+ send_jenkins_e2e_failure_notification () {
56+ local build_status=" $1 "
57+ local job_name=" ${JOB_NAME:- Unknown Job} "
58+ local build_number=" ${BUILD_NUMBER:- Unknown} "
59+ local build_url=" ${BUILD_URL:- } "
60+
61+ # Job-specific variables from init.sh
62+ local rancher_version=$( read_notification_value " RANCHER_VERSION" )
63+ local rancher_image_tag=$( read_notification_value " RANCHER_IMAGE_TAG" )
64+ local rancher_chart_url=$( read_notification_value " RANCHER_CHART_URL" )
65+ local rancher_helm_repo=$( read_notification_value " RANCHER_HELM_REPO" )
66+ local cypress_tags=$( read_notification_value " CYPRESS_TAGS" )
67+
68+ # TODO: NEEDS UPDATE. Currently using a test Slack webhook
69+ local slack_webhook=" ${SLACK_WEBHOOK_URL:- ${SLACK_WEBHOOK_URL_TESTING:- } } "
70+
71+ # Only send notifications for failures
72+ if [ " $build_status " != " FAILURE" ] && [ " $build_status " != " UNSTABLE" ]; then
73+ echo " Build status is $build_status , no notification needed"
74+ return 0
75+ fi
76+
77+ # Prepare the message
78+ local emoji=" ❌"
79+ local status_text=" FAILED"
80+
81+ if [ " $build_status " = " UNSTABLE" ]; then
82+ emoji=" ⚠️"
83+ status_text=" UNSTABLE"
84+ fi
85+
86+ local message=" *E2E Tests $status_text * $emoji \n"
87+ message+=" • *Job:* $job_name \n"
88+
89+ # Add build number with link
90+ if [ -n " $build_url " ] && [ " $build_number " != " Unknown" ]; then
91+ message+=" • *Build:* <$build_url |#$build_number >\n"
92+ elif [ " $build_number " != " Unknown" ]; then
93+ message+=" • *Build:* #$build_number \n"
94+ fi
95+
96+ if [ " $rancher_version " != " Unknown" ]; then
97+ message+=" • *Rancher Version:* $rancher_version \n"
98+ fi
99+
100+ if [ " $rancher_image_tag " != " Unknown" ]; then
101+ message+=" • *Rancher Image:* $rancher_image_tag \n"
102+ fi
103+
104+ if [ " $rancher_chart_url " != " Unknown" ]; then
105+ message+=" • *Chart URL:* $rancher_chart_url \n"
106+ fi
107+
108+ if [ " $rancher_helm_repo " != " Unknown" ]; then
109+ message+=" • *Helm Repo:* $rancher_helm_repo \n"
110+ fi
111+
112+ if [ " $cypress_tags " != " Unknown" ]; then
113+ message+=" • *Cypress Tags:* $cypress_tags \n"
114+ fi
115+
116+ message+=" • *Timestamp:* $( date -u ' +%Y-%m-%d %H:%M:%S UTC' ) "
117+
118+ echo " Sending Slack notification for $build_status build..."
119+ send_slack_notification " $build_status " " $message " " $slack_webhook "
120+
121+ if [ $? -eq 0 ]; then
122+ echo " Slack notification sent successfully"
123+ else
124+ echo " Failed to send Slack notification"
125+ return 1
126+ fi
127+ }
128+
129+ # Execute main function with build status
130+ send_jenkins_e2e_failure_notification " $1 "
0 commit comments