Skip to content

Commit e7dd893

Browse files
committed
Fixed Slack notification sending environment URL and login link on pre_deployment.
1 parent a340180 commit e7dd893

7 files changed

Lines changed: 195 additions & 77 deletions

File tree

.vortex/docs/content/workflows/notifications.mdx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,15 @@ Slack notifications are sent for both `pre_deployment` and `post_deployment` eve
316316

317317
### Example
318318

319-
Notification posted to Slack channel using custom "VortexBot" Slack app:
319+
When deployment starts, a notification is posted to Slack by the "VortexBot"
320+
Slack app with `Deployment Starting` status:
320321

321-
<img src="/img/notification-slack.png" alt="Slack notification" width="528"/>
322+
<img src="/img/notification-slack-before.png" alt="Slack notification - Deployment Starting" width="500"/>
323+
324+
After deployment succeeds, a notification is posted to Slack by the "VortexBot"
325+
Slack app with `Deployment Complete` status:
326+
327+
<img src="/img/notification-slack-after.png" alt="Slack notification - Deployment Complete" width="500"/>
322328

323329
## Webhook
324330

54.1 KB
Loading
41.6 KB
Loading
-36.8 KB
Binary file not shown.

.vortex/tests/bats/unit/notify-slack.bats

Lines changed: 114 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,93 +6,167 @@
66

77
load ../_helper.bash
88

9-
@test "Notify: slack, branch" {
9+
@test "Notify: slack, branch pre_deployment" {
1010
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1
1111

1212
mock_curl=$(mock_command "curl")
1313
mock_set_output "${mock_curl}" "200" 1
1414

15-
export VORTEX_NOTIFY_CHANNELS="slack"
16-
export VORTEX_NOTIFY_PROJECT="testproject"
17-
export VORTEX_NOTIFY_BRANCH="develop"
18-
export VORTEX_NOTIFY_SHA="abc123def456"
19-
export VORTEX_NOTIFY_LABEL="develop"
20-
export VORTEX_NOTIFY_ENVIRONMENT_URL="https://develop.testproject.com"
15+
export VORTEX_NOTIFY_SLACK_PROJECT="testproject"
16+
export VORTEX_NOTIFY_SLACK_LABEL="develop"
17+
export VORTEX_NOTIFY_SLACK_ENVIRONMENT_URL="https://develop.testproject.com"
18+
export VORTEX_NOTIFY_SLACK_LOGIN_URL="https://develop.testproject.com/user/login"
2119
export VORTEX_NOTIFY_SLACK_WEBHOOK="https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXX"
20+
export VORTEX_NOTIFY_SLACK_CHANNEL="#deployments"
21+
export VORTEX_NOTIFY_SLACK_USERNAME="Deploy Bot"
22+
export VORTEX_NOTIFY_SLACK_ICON_EMOJI=":rocket:"
23+
export VORTEX_NOTIFY_SLACK_EVENT="pre_deployment"
2224

23-
run ./scripts/vortex/notify.sh
25+
run ./scripts/vortex/notify-slack.sh
2426
assert_success
2527

26-
assert_output_contains "Started dispatching notifications."
28+
# Assert script output
2729
assert_output_contains "Started Slack notification."
28-
assert_output_contains "Notification sent to Slack."
29-
assert_output_contains "Project: testproject"
30-
assert_output_contains 'Deployment: develop'
30+
assert_output_contains "Project : testproject"
31+
assert_output_contains "Deployment : develop"
3132
assert_output_contains "Environment URL: https://develop.testproject.com"
33+
assert_output_contains "Login URL : https://develop.testproject.com/user/login"
34+
assert_output_contains "Channel : #deployments"
35+
assert_output_contains "Username : Deploy Bot"
36+
assert_output_contains "Event : Deployment Starting"
37+
assert_output_contains "Notification sent to Slack."
3238
assert_output_contains "Finished Slack notification."
33-
assert_output_contains "Finished dispatching notifications."
39+
40+
# Verify curl payload does NOT contain View Site or Login Here for pre-deployment
41+
run mock_get_call_args "${mock_curl}" 1
42+
assert_output_contains "Deployment"
43+
assert_output_contains "Time"
44+
assert_output_not_contains "View Site"
45+
assert_output_not_contains "Login Here"
3446

3547
popd >/dev/null || exit 1
3648
}
3749

38-
@test "Notify: slack, PR" {
50+
@test "Notify: slack, branch post_deployment" {
3951
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1
4052

4153
mock_curl=$(mock_command "curl")
4254
mock_set_output "${mock_curl}" "200" 1
4355

44-
export VORTEX_NOTIFY_CHANNELS="slack"
45-
export VORTEX_NOTIFY_PROJECT="testproject"
46-
export VORTEX_NOTIFY_BRANCH="feature/feature-123"
47-
export VORTEX_NOTIFY_SHA="abc123def456"
48-
export VORTEX_NOTIFY_PR_NUMBER="123"
49-
export VORTEX_NOTIFY_LABEL="PR-123"
50-
export VORTEX_NOTIFY_ENVIRONMENT_URL="https://pr-123.testproject.com"
56+
export VORTEX_NOTIFY_SLACK_PROJECT="testproject"
57+
export VORTEX_NOTIFY_SLACK_LABEL="develop"
58+
export VORTEX_NOTIFY_SLACK_ENVIRONMENT_URL="https://develop.testproject.com"
59+
export VORTEX_NOTIFY_SLACK_LOGIN_URL="https://develop.testproject.com/user/login"
5160
export VORTEX_NOTIFY_SLACK_WEBHOOK="https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXX"
61+
export VORTEX_NOTIFY_SLACK_CHANNEL="#deployments"
62+
export VORTEX_NOTIFY_SLACK_USERNAME="Deploy Bot"
63+
export VORTEX_NOTIFY_SLACK_ICON_EMOJI=":rocket:"
64+
export VORTEX_NOTIFY_SLACK_EVENT="post_deployment"
5265

53-
run ./scripts/vortex/notify.sh
66+
run ./scripts/vortex/notify-slack.sh
5467
assert_success
5568

56-
assert_output_contains "Started dispatching notifications."
69+
# Assert script output
5770
assert_output_contains "Started Slack notification."
71+
assert_output_contains "Project : testproject"
72+
assert_output_contains "Deployment : develop"
73+
assert_output_contains "Environment URL: https://develop.testproject.com"
74+
assert_output_contains "Login URL : https://develop.testproject.com/user/login"
75+
assert_output_contains "Channel : #deployments"
76+
assert_output_contains "Username : Deploy Bot"
77+
assert_output_contains "Event : Deployment Complete"
5878
assert_output_contains "Notification sent to Slack."
59-
assert_output_contains "Project: testproject"
60-
assert_output_contains 'Deployment: PR-123'
61-
assert_output_contains "Environment URL: https://pr-123.testproject.com"
6279
assert_output_contains "Finished Slack notification."
63-
assert_output_contains "Finished dispatching notifications."
80+
81+
# Verify curl payload DOES contain View Site and Login Here for post-deployment
82+
run mock_get_call_args "${mock_curl}" 1
83+
assert_output_contains "Deployment"
84+
assert_output_contains "Time"
85+
assert_output_contains "View Site"
86+
assert_output_contains "Login Here"
6487

6588
popd >/dev/null || exit 1
6689
}
6790

68-
@test "Notify: slack, pre_deployment" {
91+
@test "Notify: slack, PR pre_deployment" {
6992
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1
7093

7194
mock_curl=$(mock_command "curl")
7295
mock_set_output "${mock_curl}" "200" 1
7396

74-
export VORTEX_NOTIFY_CHANNELS="slack"
75-
export VORTEX_NOTIFY_EVENT="pre_deployment"
76-
export VORTEX_NOTIFY_PROJECT="testproject"
77-
export VORTEX_NOTIFY_BRANCH="develop"
78-
export VORTEX_NOTIFY_SHA="abc123def456"
79-
export VORTEX_NOTIFY_LABEL="develop"
80-
export VORTEX_NOTIFY_ENVIRONMENT_URL="https://develop.testproject.com"
81-
export VORTEX_NOTIFY_SLACK_LOGIN_URL="https://develop.testproject.com/user/login"
97+
export VORTEX_NOTIFY_SLACK_PROJECT="testproject"
98+
export VORTEX_NOTIFY_SLACK_LABEL="PR-123"
99+
export VORTEX_NOTIFY_SLACK_ENVIRONMENT_URL="https://pr-123.testproject.com"
100+
export VORTEX_NOTIFY_SLACK_LOGIN_URL="https://pr-123.testproject.com/user/login"
82101
export VORTEX_NOTIFY_SLACK_WEBHOOK="https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXX"
102+
export VORTEX_NOTIFY_SLACK_CHANNEL="#deployments"
103+
export VORTEX_NOTIFY_SLACK_USERNAME="Deploy Bot"
104+
export VORTEX_NOTIFY_SLACK_ICON_EMOJI=":rocket:"
105+
export VORTEX_NOTIFY_SLACK_EVENT="pre_deployment"
83106

84-
# Pre-deployment forces github channel, but we're testing slack directly
85-
export VORTEX_NOTIFY_CHANNELS="github"
107+
run ./scripts/vortex/notify-slack.sh
108+
assert_success
109+
110+
# Assert script output
111+
assert_output_contains "Started Slack notification."
112+
assert_output_contains "Project : testproject"
113+
assert_output_contains "Deployment : PR-123"
114+
assert_output_contains "Environment URL: https://pr-123.testproject.com"
115+
assert_output_contains "Login URL : https://pr-123.testproject.com/user/login"
116+
assert_output_contains "Channel : #deployments"
117+
assert_output_contains "Username : Deploy Bot"
118+
assert_output_contains "Event : Deployment Starting"
119+
assert_output_contains "Notification sent to Slack."
120+
assert_output_contains "Finished Slack notification."
121+
122+
# Verify curl payload does NOT contain View Site or Login Here for pre-deployment
123+
run mock_get_call_args "${mock_curl}" 1
124+
assert_output_contains "Deployment"
125+
assert_output_contains "Time"
126+
assert_output_not_contains "View Site"
127+
assert_output_not_contains "Login Here"
128+
129+
popd >/dev/null || exit 1
130+
}
131+
132+
@test "Notify: slack, PR post_deployment" {
133+
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1
134+
135+
mock_curl=$(mock_command "curl")
136+
mock_set_output "${mock_curl}" "200" 1
137+
138+
export VORTEX_NOTIFY_SLACK_PROJECT="testproject"
139+
export VORTEX_NOTIFY_SLACK_LABEL="PR-123"
140+
export VORTEX_NOTIFY_SLACK_ENVIRONMENT_URL="https://pr-123.testproject.com"
141+
export VORTEX_NOTIFY_SLACK_LOGIN_URL="https://pr-123.testproject.com/user/login"
142+
export VORTEX_NOTIFY_SLACK_WEBHOOK="https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXX"
143+
export VORTEX_NOTIFY_SLACK_CHANNEL="#deployments"
144+
export VORTEX_NOTIFY_SLACK_USERNAME="Deploy Bot"
145+
export VORTEX_NOTIFY_SLACK_ICON_EMOJI=":rocket:"
146+
export VORTEX_NOTIFY_SLACK_EVENT="post_deployment"
86147

87-
# Test Slack script directly for pre-deployment
88-
export VORTEX_NOTIFY_CHANNELS="slack"
89148
run ./scripts/vortex/notify-slack.sh
90149
assert_success
91150

151+
# Assert script output
92152
assert_output_contains "Started Slack notification."
153+
assert_output_contains "Project : testproject"
154+
assert_output_contains "Deployment : PR-123"
155+
assert_output_contains "Environment URL: https://pr-123.testproject.com"
156+
assert_output_contains "Login URL : https://pr-123.testproject.com/user/login"
157+
assert_output_contains "Channel : #deployments"
158+
assert_output_contains "Username : Deploy Bot"
159+
assert_output_contains "Event : Deployment Complete"
93160
assert_output_contains "Notification sent to Slack."
94161
assert_output_contains "Finished Slack notification."
95162

163+
# Verify curl payload DOES contain View Site and Login Here for post-deployment
164+
run mock_get_call_args "${mock_curl}" 1
165+
assert_output_contains "Deployment"
166+
assert_output_contains "Time"
167+
assert_output_contains "View Site"
168+
assert_output_contains "Login Here"
169+
96170
popd >/dev/null || exit 1
97171
}
98172

.vortex/tests/manual/try-slack-notification.sh

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22
##
33
# Manual test script for Slack notifications.
44
#
5+
# Sends all 4 notification scenarios to Slack:
6+
# 1. Branch pre-deployment (deployment starting, no links)
7+
# 2. Branch post-deployment (deployment complete, with links)
8+
# 3. PR pre-deployment (deployment starting, no links)
9+
# 4. PR post-deployment (deployment complete, with links)
10+
#
511
# Usage:
612
# export SLACK_WEBHOOK_URL="your-webhook-url"
7-
# ./try-slack-notification.sh [branch|pr]
13+
# ./try-slack-notification.sh
814

915
set -eu
1016
[ "${VORTEX_DEBUG-}" = "1" ] && set -x
@@ -25,50 +31,74 @@ SLACK_WEBHOOK_URL="${SLACK_WEBHOOK_URL:-}"
2531
# Check if SLACK_WEBHOOK_URL is set
2632
if [ -z "${SLACK_WEBHOOK_URL}" ]; then
2733
echo "Error: SLACK_WEBHOOK_URL environment variable is required"
28-
echo "Usage: export SLACK_WEBHOOK_URL=\"your-webhook-url\" && $0 [branch|pr]"
34+
echo "Usage: export SLACK_WEBHOOK_URL=\"your-webhook-url\" && $0"
2935
exit 1
3036
fi
3137

32-
echo "Testing Slack notification..."
38+
echo "Testing Slack notifications..."
3339
echo ""
3440
echo "Webhook URL: ${SLACK_WEBHOOK_URL:0:50}..."
3541
echo ""
42+
echo "This will send 4 notifications to Slack:"
43+
echo " 1. Branch pre-deployment (no View Site or Login links)"
44+
echo " 2. Branch post-deployment (with View Site and Login links)"
45+
echo " 3. PR pre-deployment (no View Site or Login links)"
46+
echo " 4. PR post-deployment (with View Site and Login links)"
47+
echo ""
3648

37-
# Determine test scenario
38-
SCENARIO="${1:-branch}"
39-
40-
if [ "${SCENARIO}" = "pr" ]; then
41-
echo "Testing PR deployment notification"
42-
export VORTEX_NOTIFY_PROJECT="Test Project with PR"
43-
export VORTEX_NOTIFY_BRANCH="feature/PROJ-123-test-feature"
44-
export VORTEX_NOTIFY_SHA="abc123def456"
45-
export VORTEX_NOTIFY_PR_NUMBER="123"
46-
export VORTEX_NOTIFY_LABEL="PR-123"
47-
else
48-
echo "Testing branch deployment notification"
49-
export VORTEX_NOTIFY_PROJECT="Test Project"
50-
export VORTEX_NOTIFY_BRANCH="main"
51-
export VORTEX_NOTIFY_SHA="abc123def456"
52-
export VORTEX_NOTIFY_LABEL="main"
53-
fi
49+
# Change to project root
50+
cd "${PROJECT_ROOT}" || exit 1
5451

55-
# Set required environment variables
52+
# Common settings
5653
export VORTEX_NOTIFY_CHANNELS=slack
5754
export VORTEX_NOTIFY_SLACK_WEBHOOK="${SLACK_WEBHOOK_URL}"
5855
export VORTEX_NOTIFY_ENVIRONMENT_URL="https://example.com"
59-
export VORTEX_NOTIFY_EVENT="post_deployment"
60-
61-
# Optional: Customize these
6256
export VORTEX_NOTIFY_SLACK_CHANNEL="${VORTEX_NOTIFY_SLACK_CHANNEL:-}"
6357
export VORTEX_NOTIFY_SLACK_USERNAME="${VORTEX_NOTIFY_SLACK_USERNAME:-Deployment Bot}"
6458
export VORTEX_NOTIFY_SLACK_ICON_EMOJI="${VORTEX_NOTIFY_SLACK_ICON_EMOJI:-:rocket:}"
6559

66-
echo "Running notification script..."
60+
# Scenario 1: Branch pre-deployment
61+
echo "==> Scenario 1: Branch pre-deployment"
62+
export VORTEX_NOTIFY_PROJECT="Test Project"
63+
export VORTEX_NOTIFY_BRANCH="main"
64+
export VORTEX_NOTIFY_SHA="abc123def456"
65+
export VORTEX_NOTIFY_LABEL="main"
66+
export VORTEX_NOTIFY_PR_NUMBER=""
67+
export VORTEX_NOTIFY_EVENT="pre_deployment"
68+
./scripts/vortex/notify.sh
6769
echo ""
6870

69-
# Change to project root and run the notification
70-
cd "${PROJECT_ROOT}" || exit 1
71+
# Scenario 2: Branch post-deployment
72+
echo "==> Scenario 2: Branch post-deployment"
73+
export VORTEX_NOTIFY_PROJECT="Test Project"
74+
export VORTEX_NOTIFY_BRANCH="main"
75+
export VORTEX_NOTIFY_SHA="abc123def456"
76+
export VORTEX_NOTIFY_LABEL="main"
77+
export VORTEX_NOTIFY_PR_NUMBER=""
78+
export VORTEX_NOTIFY_EVENT="post_deployment"
79+
./scripts/vortex/notify.sh
80+
echo ""
81+
82+
# Scenario 3: PR pre-deployment
83+
echo "==> Scenario 3: PR pre-deployment"
84+
export VORTEX_NOTIFY_PROJECT="Test Project with PR"
85+
export VORTEX_NOTIFY_BRANCH="feature/PROJ-123-test-feature"
86+
export VORTEX_NOTIFY_SHA="abc123def456"
87+
export VORTEX_NOTIFY_LABEL="PR-123"
88+
export VORTEX_NOTIFY_PR_NUMBER="123"
89+
export VORTEX_NOTIFY_EVENT="pre_deployment"
7190
./scripts/vortex/notify.sh
91+
echo ""
7292

93+
# Scenario 4: PR post-deployment
94+
echo "==> Scenario 4: PR post-deployment"
95+
export VORTEX_NOTIFY_PROJECT="Test Project with PR"
96+
export VORTEX_NOTIFY_BRANCH="feature/PROJ-123-test-feature"
97+
export VORTEX_NOTIFY_SHA="abc123def456"
98+
export VORTEX_NOTIFY_LABEL="PR-123"
99+
export VORTEX_NOTIFY_PR_NUMBER="123"
100+
export VORTEX_NOTIFY_EVENT="post_deployment"
101+
./scripts/vortex/notify.sh
73102
echo ""
74-
echo "Check your Slack channel for the notification!"
103+
104+
echo "All 4 notifications sent! Check your Slack channel."

scripts/vortex/notify-slack.sh

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fi
100100
title="${event_label}: ${VORTEX_NOTIFY_SLACK_PROJECT}"
101101

102102
# Build payload using PHP with proper escaping from environment variables.
103-
payload=$(VORTEX_NOTIFY_SLACK_USERNAME="${VORTEX_NOTIFY_SLACK_USERNAME}" VORTEX_NOTIFY_SLACK_ICON_EMOJI="${VORTEX_NOTIFY_SLACK_ICON_EMOJI}" color="${color}" fallback_message="${fallback_message}" title="${title}" VORTEX_NOTIFY_SLACK_LABEL="${VORTEX_NOTIFY_SLACK_LABEL}" VORTEX_NOTIFY_SLACK_ENVIRONMENT_URL="${VORTEX_NOTIFY_SLACK_ENVIRONMENT_URL}" VORTEX_NOTIFY_SLACK_LOGIN_URL="${VORTEX_NOTIFY_SLACK_LOGIN_URL}" timestamp="${timestamp}" VORTEX_NOTIFY_SLACK_CHANNEL="${VORTEX_NOTIFY_SLACK_CHANNEL}" php -r '
103+
payload=$(VORTEX_NOTIFY_SLACK_USERNAME="${VORTEX_NOTIFY_SLACK_USERNAME}" VORTEX_NOTIFY_SLACK_ICON_EMOJI="${VORTEX_NOTIFY_SLACK_ICON_EMOJI}" color="${color}" fallback_message="${fallback_message}" title="${title}" VORTEX_NOTIFY_SLACK_LABEL="${VORTEX_NOTIFY_SLACK_LABEL}" VORTEX_NOTIFY_SLACK_ENVIRONMENT_URL="${VORTEX_NOTIFY_SLACK_ENVIRONMENT_URL}" VORTEX_NOTIFY_SLACK_LOGIN_URL="${VORTEX_NOTIFY_SLACK_LOGIN_URL}" timestamp="${timestamp}" VORTEX_NOTIFY_SLACK_CHANNEL="${VORTEX_NOTIFY_SLACK_CHANNEL}" VORTEX_NOTIFY_SLACK_EVENT="${VORTEX_NOTIFY_SLACK_EVENT}" php -r '
104104
$username = getenv("VORTEX_NOTIFY_SLACK_USERNAME");
105105
$icon = getenv("VORTEX_NOTIFY_SLACK_ICON_EMOJI");
106106
$color = getenv("color");
@@ -111,6 +111,19 @@ $envUrl = getenv("VORTEX_NOTIFY_SLACK_ENVIRONMENT_URL");
111111
$login_url = getenv("VORTEX_NOTIFY_SLACK_LOGIN_URL");
112112
$timestamp = getenv("timestamp");
113113
$channel = getenv("VORTEX_NOTIFY_SLACK_CHANNEL");
114+
$event = getenv("VORTEX_NOTIFY_SLACK_EVENT");
115+
116+
$fields = [
117+
["title" => "Deployment", "value" => $label, "short" => true],
118+
["title" => "Time", "value" => $timestamp, "short" => true]
119+
];
120+
121+
// Only include Environment and Login links for post-deployment notifications.
122+
// Pre-deployment notifications should not show these as the site is not yet available.
123+
if ($event !== "pre_deployment") {
124+
$fields[] = ["title" => "Environment", "value" => "<" . $envUrl . "|View Site>", "short" => true];
125+
$fields[] = ["title" => "Login", "value" => "<" . $login_url . "|Login Here>", "short" => true];
126+
}
114127
115128
$data = [
116129
"username" => $username,
@@ -120,12 +133,7 @@ $data = [
120133
"color" => $color,
121134
"fallback" => $fallback,
122135
"title" => $title,
123-
"fields" => [
124-
["title" => "Deployment", "value" => $label, "short" => true],
125-
["title" => "Environment", "value" => "<" . $envUrl . "|View Site>", "short" => true],
126-
["title" => "Login", "value" => "<" . $login_url . "|Login Here>", "short" => true],
127-
["title" => "Time", "value" => $timestamp, "short" => true]
128-
],
136+
"fields" => $fields,
129137
"footer" => "Vortex Deployment",
130138
"ts" => time()
131139
]

0 commit comments

Comments
 (0)