Skip to content

Commit 0658f66

Browse files
committed
add slack notification
1 parent 0655985 commit 0658f66

File tree

4 files changed

+157
-9
lines changed

4 files changed

+157
-9
lines changed

cypress/jenkins/Jenkinsfile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ 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: 'SLACK_WEBHOOK_URL_TESTING', variable: 'SLACK_WEBHOOK_URL_TESTING')
3334
]) {
3435
withEnv(paramsMap) {
3536
stage('Checkout') {
@@ -118,6 +119,14 @@ node {
118119
currentBuild.result = 'FAILURE'
119120
}
120121
}
122+
123+
// Send Slack notification on failure or unstable builds
124+
if (currentBuild.result == 'FAILURE' || currentBuild.result == 'UNSTABLE') {
125+
echo "Sending Slack notification for ${currentBuild.result} build"
126+
sh "cypress/jenkins/slack-notification.sh ${currentBuild.result}"
127+
} else {
128+
echo "Build successful, no Slack notification needed"
129+
}
121130
}
122131
}
123132
}

cypress/jenkins/cypress.config.jenkins.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ require('dotenv').config();
1212
*/
1313

1414
const testDirs = [
15-
'cypress/e2e/tests/priority/**/*.spec.ts',
16-
'cypress/e2e/tests/components/**/*.spec.ts',
17-
'cypress/e2e/tests/setup/**/*.spec.ts',
18-
'cypress/e2e/tests/pages/**/*.spec.ts',
15+
// 'cypress/e2e/tests/priority/**/*.spec.ts',
16+
// 'cypress/e2e/tests/components/**/*.spec.ts',
17+
// 'cypress/e2e/tests/setup/**/*.spec.ts',
18+
// 'cypress/e2e/tests/pages/**/*.spec.ts',
1919
'cypress/e2e/tests/navigation/**/*.spec.ts',
20-
'cypress/e2e/tests/global-ui/**/*.spec.ts',
21-
'cypress/e2e/tests/features/**/*.spec.ts',
22-
'cypress/e2e/tests/extensions/**/*.spec.ts'
20+
// 'cypress/e2e/tests/global-ui/**/*.spec.ts',
21+
// 'cypress/e2e/tests/features/**/*.spec.ts',
22+
// 'cypress/e2e/tests/extensions/**/*.spec.ts'
2323
];
2424
const skipSetup = process.env.TEST_SKIP?.includes('setup');
2525
const baseUrl = (process.env.TEST_BASE_URL || 'https://localhost:8005').replace(/\/$/, '');
@@ -70,7 +70,7 @@ export default defineConfig({
7070
trashAssetsBeforeRuns: true,
7171
chromeWebSecurity: false,
7272
retries: {
73-
runMode: 2,
73+
runMode: 0,
7474
openMode: 0
7575
},
7676
env: {

cypress/jenkins/init.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ create_initial_clusters() {
144144
RANCHER_VERSION=$(helm search repo "rancher-${RANCHER_HELM_REPO}" --devel --versions | grep "${version_string}" | head -n 1 | cut -f2 | tr -d '[:space:]')
145145
fi
146146
corral config vars set rancher_image_tag "${RANCHER_IMAGE_TAG}"
147+
148+
# Save all values to a file for the Slack notification script
149+
cat > "${WORKSPACE}/notification_values.txt" << EOF
150+
RANCHER_VERSION=${RANCHER_VERSION}
151+
RANCHER_IMAGE_TAG=${RANCHER_IMAGE_TAG}
152+
RANCHER_CHART_URL=${RANCHER_CHART_URL}
153+
RANCHER_HELM_REPO=${RANCHER_HELM_REPO}
154+
CYPRESS_TAGS=${CYPRESS_TAGS}
155+
EOF
147156
fi
148157
cd "${WORKSPACE}/corral-packages"
149158
yq -i e ".variables.rancher_version += [\"${RANCHER_VERSION}\"] | .variables.rancher_version style=\"literal\"" packages/aws/rancher-k3s.yaml
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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

Comments
 (0)