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 bot_token=" $3 "
13+ local channel=" $4 "
14+
15+ if [ -z " $bot_token " ]; then
16+ echo " Warning: UI_SLACK_BOT_TOKEN not set, skipping Slack notification"
17+ return 0
18+ fi
19+
20+ # Prepare the JSON payload
21+ local payload=$( cat << EOF
22+ {
23+ "channel": "$channel ",
24+ "text": "$message ",
25+ "username": "Jenkins E2E Tests"
26+ }
27+ EOF
28+ )
29+
30+ # Send the notification using Slack Web API
31+ # Try-catch block to handle any errors when communicating with Slack API
32+ set +e # Disable exit on error for try block
33+ curl -X POST \
34+ -H " Content-type: application/json; charset=utf-8" \
35+ -H " Authorization: Bearer $bot_token " \
36+ --data " $payload " \
37+ " https://slack.com/api/chat.postMessage" > /dev/null 2>&1
38+ local curl_exit_code=$?
39+ set -e # Re-enable exit on error
40+
41+ # Catch block - handle any errors
42+ if [ $curl_exit_code -ne 0 ]; then
43+ echo " Error: Failed to send Slack notification"
44+ return 1
45+ fi
46+
47+ return 0
48+ }
49+
50+ # Function to read value from file
51+ read_from_file () {
52+ local file_path=" $1 "
53+
54+ if [ -f " $file_path " ]; then
55+ cat " $file_path " | tr -d ' [:space:]'
56+ fi
57+ }
58+
59+ # Function to read value from notification_values.txt
60+ read_notification_value () {
61+ local key=" $1 "
62+ local file_path=" ${WORKSPACE} /notification_values.txt"
63+
64+ if [ -f " $file_path " ]; then
65+ grep " ^${key} =" " $file_path " | cut -d' =' -f2- | tr -d ' [:space:]'
66+ fi
67+ }
68+
69+ # Main execution
70+ send_jenkins_e2e_failure_notification () {
71+ local build_status=" $1 "
72+ local job_name=" ${JOB_NAME:- Unknown Job} "
73+ local build_number=" ${BUILD_NUMBER:- Unknown} "
74+ local build_url=" ${BUILD_URL:- } "
75+
76+ # Job-specific variables from init.sh
77+ local rancher_version=$( read_notification_value " RANCHER_VERSION" )
78+ local rancher_image_tag=$( read_notification_value " RANCHER_IMAGE_TAG" )
79+ local rancher_chart_url=$( read_notification_value " RANCHER_CHART_URL" )
80+ local rancher_helm_repo=$( read_notification_value " RANCHER_HELM_REPO" )
81+ local cypress_tags=$( read_notification_value " CYPRESS_TAGS" )
82+
83+ # Get Slack bot token and channel from Secrets Manager
84+ local slack_bot_token=" ${UI_SLACK_BOT_TOKEN:- } "
85+ local slack_channel=" ${UI_SLACK_CHANNEL:- } "
86+
87+ # Only send notifications for failures
88+ if [ " $build_status " != " FAILURE" ] && [ " $build_status " != " UNSTABLE" ]; then
89+ echo " Build status is $build_status , no notification needed"
90+ return 0
91+ fi
92+
93+ # Prepare the message
94+ local emoji=" ❌"
95+ local status_text=" FAILED"
96+
97+ if [ " $build_status " = " UNSTABLE" ]; then
98+ emoji=" ⚠️"
99+ status_text=" UNSTABLE"
100+ fi
101+
102+ local message=" *E2E Tests $status_text * $emoji \n"
103+ message+=" • *Job:* $job_name \n"
104+
105+ # Add build number with link
106+ if [ -n " $build_url " ] && [ " $build_number " != " Unknown" ]; then
107+ message+=" • *Build:* <$build_url |#$build_number >\n"
108+ elif [ " $build_number " != " Unknown" ]; then
109+ message+=" • *Build:* #$build_number \n"
110+ fi
111+
112+ if [ -n " $rancher_version " ] && [ " $rancher_version " != " Unknown" ]; then
113+ message+=" • *Rancher Version:* $rancher_version \n"
114+ fi
115+
116+ if [ -n " $rancher_image_tag " ] && [ " $rancher_image_tag " != " Unknown" ]; then
117+ message+=" • *Rancher Image:* $rancher_image_tag \n"
118+ fi
119+
120+ if [ -n " $rancher_chart_url " ] && [ " $rancher_chart_url " != " Unknown" ]; then
121+ message+=" • *Chart URL:* $rancher_chart_url \n"
122+ fi
123+
124+ if [ -n " $rancher_helm_repo " ] && [ " $rancher_helm_repo " != " Unknown" ]; then
125+ message+=" • *Helm Repo:* $rancher_helm_repo \n"
126+ fi
127+
128+ if [ -n " $cypress_tags " ] && [ " $cypress_tags " != " Unknown" ]; then
129+ message+=" • *Cypress Tags:* $cypress_tags \n"
130+ fi
131+
132+ message+=" • *Timestamp:* $( date -u ' +%Y-%m-%d %H:%M:%S UTC' ) "
133+
134+ echo " Sending Slack notification for $build_status build..."
135+ send_slack_notification " $build_status " " $message " " $slack_bot_token " " $slack_channel "
136+
137+ if [ $? -eq 0 ]; then
138+ echo " Slack notification sent successfully"
139+ else
140+ echo " Failed to send Slack notification"
141+ return 1
142+ fi
143+ }
144+
145+ # Execute main function with build status
146+ send_jenkins_e2e_failure_notification " $1 "
0 commit comments