Skip to content

Commit bf8f9e9

Browse files
committed
add slack notification
1 parent 11bd4d1 commit bf8f9e9

File tree

3 files changed

+168
-1
lines changed

3 files changed

+168
-1
lines changed

cypress/jenkins/Jenkinsfile

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ node {
2929
string(credentialsId: 'AZURE_CLIENT_ID', variable: 'AZURE_CLIENT_ID'),
3030
string(credentialsId: 'AZURE_CLIENT_SECRET', variable: 'AZURE_CLIENT_SECRET'),
3131
string(credentialsId: 'GKE_SERVICE_ACCOUNT', variable: 'GKE_SERVICE_ACCOUNT'),
32-
string(credentialsId: 'PERCY_TOKEN', variable: 'PERCY_TOKEN')
32+
string(credentialsId: 'PERCY_TOKEN', variable: 'PERCY_TOKEN'),
33+
string(credentialsId: 'UI_SLACK_BOT_TOKEN', variable: 'UI_SLACK_BOT_TOKEN'),
34+
string(credentialsId: 'UI_SLACK_CHANNEL', variable: 'UI_SLACK_CHANNEL')
3335
]) {
3436
withEnv(paramsMap) {
3537
stage('Checkout') {
@@ -118,6 +120,14 @@ node {
118120
currentBuild.result = 'FAILURE'
119121
}
120122
}
123+
124+
// Send Slack notification on failure or unstable builds
125+
if (currentBuild.result == 'FAILURE' || currentBuild.result == 'UNSTABLE') {
126+
echo "Sending Slack notification for ${currentBuild.result} build"
127+
sh "cypress/jenkins/slack-notification.sh ${currentBuild.result}"
128+
} else {
129+
echo "Build successful, no Slack notification needed"
130+
}
121131
}
122132
}
123133
}

cypress/jenkins/init.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,17 @@ case "${RANCHER_IMAGE_TAG}" in
238238
*)
239239
esac
240240
corral config vars set cypress_tags "${CYPRESS_TAGS}"
241+
242+
# Save all values to a file for the Slack notification script
243+
if [[ -n "${RANCHER_IMAGE_TAG}" ]]; then
244+
cat > "${WORKSPACE}/notification_values.txt" << EOF
245+
RANCHER_VERSION=${RANCHER_VERSION}
246+
RANCHER_IMAGE_TAG=${RANCHER_IMAGE_TAG}
247+
RANCHER_CHART_URL=${RANCHER_CHART_URL}
248+
RANCHER_HELM_REPO=${RANCHER_HELM_REPO}
249+
CYPRESS_TAGS=${CYPRESS_TAGS}
250+
EOF
251+
fi
241252
corral config vars set cypress_version "${CYPRESS_VERSION}"
242253
corral config vars set yarn_version "${YARN_VERSION}"
243254
corral config vars set kubectl_version "${KUBECTL_VERSION}"
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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

Comments
 (0)