-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yaml
More file actions
113 lines (103 loc) · 4.07 KB
/
Copy pathaction.yaml
File metadata and controls
113 lines (103 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
name: Notify Tailor Platform deployment
description: >-
Send a deployment notification after a Tailor Platform deploy.
Currently supports Slack (via Bot token + channel ID).
inputs:
provider:
description: Notification provider. Currently only 'slack' is supported.
required: true
status:
description: >-
Deployment status to report. Accepts 'success', 'failure', or 'cancelled'.
Typically pass `job.status` or `steps.<id>.outcome`.
Any value other than 'success' is reported as a failure (:x:).
required: true
workspace-name:
description: Tailor Platform workspace name shown in the notification message
required: false
slack-channel-id:
description: Slack channel ID to post to (required when provider is 'slack')
required: false
slack-token:
description: >-
Slack Bot token (xoxb-...) with chat:write permission (required when provider is 'slack').
required: false
user-mapping:
description: >-
Optional JSON string mapping GitHub usernames to Slack user IDs for @mention support.
e.g. '{"alice":"U0123456","bob":"U7890ABC"}'.
When a match is found the actor is mentioned as <@UXXXXX>; falls back to plain username.
required: false
default: ""
runs:
using: composite
steps:
- name: Validate provider
shell: bash
env:
PROVIDER: ${{ inputs.provider }}
run: |
if [ "$PROVIDER" != "slack" ]; then
echo "::error::Unsupported provider '$PROVIDER'. Currently only 'slack' is supported."
exit 1
fi
- name: Mask Slack token
if: inputs.provider == 'slack'
shell: bash
env:
SLACK_TOKEN: ${{ inputs.slack-token }}
run: |
if [ -n "$SLACK_TOKEN" ]; then
echo "::add-mask::$SLACK_TOKEN"
fi
- name: Notify Slack
if: inputs.provider == 'slack'
shell: bash
env:
SLACK_TOKEN: ${{ inputs.slack-token }}
SLACK_CHANNEL_ID: ${{ inputs.slack-channel-id }}
STATUS: ${{ inputs.status }}
WORKSPACE_NAME: ${{ inputs.workspace-name }}
GITHUB_ACTOR: ${{ github.actor }}
USER_MAPPING: ${{ inputs.user-mapping }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
if [ -z "$SLACK_TOKEN" ] || [ -z "$SLACK_CHANNEL_ID" ]; then
echo "::notice::notify: slack-token or slack-channel-id is not set — skipping Slack notification"
exit 0
fi
if [ "$STATUS" = "success" ]; then
EMOJI=":white_check_mark:"
else
EMOJI=":x:"
fi
# Resolve actor mention from user-mapping (JSON string), fall back to plain username.
if [ -n "$USER_MAPPING" ] && [ -n "$GITHUB_ACTOR" ]; then
SLACK_USER_ID=$(echo "$USER_MAPPING" | jq -r --arg user "$GITHUB_ACTOR" '.[$user] // ""' 2>/dev/null || true)
if [ -n "$SLACK_USER_ID" ]; then
ACTOR_MENTION="<@$SLACK_USER_ID>"
else
ACTOR_MENTION="$GITHUB_ACTOR"
fi
else
ACTOR_MENTION="${GITHUB_ACTOR:-}"
fi
LABEL="${WORKSPACE_NAME:-Tailor Platform}"
if [ -n "$ACTOR_MENTION" ]; then
TEXT="$EMOJI *$LABEL* deployment $STATUS by $ACTOR_MENTION — <$RUN_URL|View run>"
else
TEXT="$EMOJI *$LABEL* deployment $STATUS — <$RUN_URL|View run>"
fi
# Use jq to safely JSON-encode the payload (handles special characters in inputs).
PAYLOAD=$(jq -nc --arg channel "$SLACK_CHANNEL_ID" --arg text "$TEXT" \
'{channel: $channel, text: $text}')
RESPONSE=$(curl -sSf \
-H "Authorization: Bearer $SLACK_TOKEN" \
-H "Content-Type: application/json; charset=utf-8" \
--data-binary "$PAYLOAD" \
https://slack.com/api/chat.postMessage)
if ! echo "$RESPONSE" | jq -e '.ok == true' > /dev/null 2>&1; then
ERROR=$(echo "$RESPONSE" | jq -r '.error // "unknown error"' 2>/dev/null || echo "unknown error")
echo "::error::Slack notification failed: $ERROR"
exit 1
fi