Skip to content

Commit 976e8c2

Browse files
authored
fix(send-slack-notification): Use envsubst for templating (#85)
1 parent 75e0756 commit 976e8c2

File tree

4 files changed

+40
-43
lines changed

4 files changed

+40
-43
lines changed

send-slack-notification/action.yaml

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -80,52 +80,49 @@ runs:
8080
GITHUB_REPOSITORY: ${{ github.repository }}
8181
GITHUB_WORKFLOW: ${{ github.workflow }}
8282
GITHUB_RUN_ID: ${{ github.run_id }}
83+
SLACK_THREAD_YAML: |
84+
${{ steps.retrieve-slack-thread-id.outcome == 'success' && format('thread_ts: "{0}"', env.SLACK_THREAD_ID) || '' }}
85+
CHANNEL_ID: ${{ inputs.channel-id }}
8386
shell: bash
8487
run: |
85-
WORKFLOW_RUN_URL="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}/attempts/${GITHUB_RUN_ATTEMPT}"
86-
echo "WORKFLOW_RUN_URL=$WORKFLOW_RUN_URL" | tee -a "$GITHUB_ENV"
88+
export WORKFLOW_RUN_URL="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}/attempts/${GITHUB_RUN_ATTEMPT}"
8789
8890
if [ "$NOTIFICATION_TYPE" == "container-image-build" ]; then
8991
# TODO (@Techassi): Also add success template
9092
if [ "$PUBLISH_MANIFESTS_RESULT" = "failure" ] || [ "$BUILD_RESULT" = "failure" ]; then
91-
MESSAGE_VERB=failed
92-
echo "MESSAGE_VERB=$MESSAGE_VERB" | tee -a "$GITHUB_ENV"
93-
94-
echo "MESSAGE_COLOR=aa0000" | tee -a "$GITHUB_ENV"
93+
export MESSAGE_VERB=failed
94+
export MESSAGE_COLOR=aa0000
9595
else
96-
MESSAGE_VERB=succeeded
97-
echo "MESSAGE_VERB=$MESSAGE_VERB" | tee -a "$GITHUB_ENV"
98-
99-
echo "MESSAGE_COLOR=10c400" | tee -a "$GITHUB_ENV"
96+
export MESSAGE_VERB=succeeded
97+
export MESSAGE_COLOR=10c400
10098
fi
10199
102-
echo "MESSAGE_TEXT=*$GITHUB_WORKFLOW* $MESSAGE_VERB (attempt $GITHUB_RUN_ATTEMPT)" | tee -a "$GITHUB_ENV"
103-
echo -e "MESSAGE_TEMPLATE<<EOF\n$(cat ${GITHUB_ACTION_PATH}/templates/container-image-build/failure.tpl)\nEOF" | tee -a "$GITHUB_ENV"
100+
export MESSAGE_TEXT="*$GITHUB_WORKFLOW* $MESSAGE_VERB (attempt $GITHUB_RUN_ATTEMPT)"
101+
PAYLOAD=$(envsubst < "${GITHUB_ACTION_PATH}/templates/container-image-build/failure.tpl")
102+
echo -e "PAYLOAD<<EOF\n$PAYLOAD\nEOF" | tee -a "$GITHUB_ENV"
104103
elif [ "$NOTIFICATION_TYPE" == "integration-test" ]; then
105-
echo "HEALTH_SLACK_EMOJI=$(echo "$TEST_HEALTH" | cut -d ',' -f 1)" | tee -a "$GITHUB_ENV"
106-
echo "HEALTH_RATE=$(echo "$TEST_HEALTH" | cut -d ',' -f 3)" | tee -a "$GITHUB_ENV"
104+
export HEALTH_SLACK_EMOJI=$(echo "$TEST_HEALTH" | cut -d ',' -f 1)
105+
export HEALTH_RATE=$(echo "$TEST_HEALTH" | cut -d ',' -f 3)
107106
108107
if [ "$TEST_RESULT" == "failure" ]; then
109-
echo "MESSAGE_TEXT=The integration test for *`$GITHUB_REPOSITORY`* failed." | tee -a "$GITHUB_ENV"
110-
echo -e "MESSAGE_TEMPLATE<<EOF\n$(cat ${GITHUB_ACTION_PATH}/templates/integration-test/failure.tpl)\nEOF" | tee -a "$GITHUB_ENV"
108+
export MESSAGE_TEXT="The integration test for *`$GITHUB_REPOSITORY`* failed."
109+
PAYLOAD=$(envsubst < "${GITHUB_ACTION_PATH}/templates/integration-test/failure.tpl")
110+
echo -e "PAYLOAD<<EOF\n$PAYLOAD\nEOF" | tee -a "$GITHUB_ENV"
111111
else
112-
echo "MESSAGE_TEXT=The integration test for *`$GITHUB_REPOSITORY`* succeeded." | tee -a "$GITHUB_ENV"
113-
echo -e "MESSAGE_TEMPLATE<<EOF\n$(cat ${GITHUB_ACTION_PATH}/templates/integration-test/success.tpl)\nEOF" | tee -a "$GITHUB_ENV"
112+
export MESSAGE_TEXT="The integration test for *`$GITHUB_REPOSITORY`* succeeded."
113+
PAYLOAD=$(envsubst < "${GITHUB_ACTION_PATH}/templates/integration-test/success.tpl")
114+
echo -e "PAYLOAD<<EOF\n$PAYLOAD\nEOF" | tee -a "$GITHUB_ENV"
114115
fi
115116
fi
116117
117118
- name: Send Notification
118119
id: send-notification
119120
uses: slackapi/slack-github-action@91efab103c0de0a537f72a35f6b8cda0ee76bf0a # 2.1.1
120-
env:
121-
SLACK_THREAD_YAML: |
122-
${{ steps.retrieve-slack-thread-id.outcome == 'success' && format('thread_ts: "{0}"', env.SLACK_THREAD_ID) || '' }}
123-
CHANNEL_ID: ${{ inputs.channel-id }}
124121
with:
125122
method: chat.postMessage
126123
token: ${{ inputs.slack-token }}
127-
payload: ${{ env.MESSAGE_TEMPLATE }}
128-
payload-templated: true
124+
payload: ${{ env.PAYLOAD }}
125+
payload-templated: false
129126

130127
- name: Save Slack Thread ID to File
131128
if: steps.retrieve-slack-thread-id.outcome == 'failure'
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
channel: "${{ env.CHANNEL_ID }}"
2-
text: "${{ env.MESSAGE_TEXT }}"
3-
${{ env.SLACK_THREAD_YAML }}
1+
channel: "${CHANNEL_ID}"
2+
text: "${MESSAGE_TEXT}"
3+
${SLACK_THREAD_YAML}
44
attachments:
5-
- pretext: "See the details below for a summary of which job(s) ${{ env.MESSAGE_VERB }}."
6-
color: "${{ env.MESSAGE_COLOR }}"
5+
- pretext: "See the details below for a summary of which job(s) ${MESSAGE_VERB}."
6+
color: "${MESSAGE_COLOR}"
77
fields:
88
- title: Build/Publish Image
99
short: true
10-
value: "${{ env.BUILD_RESULT }}"
10+
value: "${BUILD_RESULT}"
1111
- title: Build/Publish Manifests
1212
short: true
13-
value: "${{ env.PUBLISH_MANIFESTS_RESULT }}"
13+
value: "${PUBLISH_MANIFESTS_RESULT}"
1414
actions:
1515
- type: button
1616
text: Go to workflow run
17-
url: "${{ env.WORKFLOW_RUN_URL }}"
17+
url: "${WORKFLOW_RUN_URL}"

send-slack-notification/templates/integration-test/failure.tpl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
channel: "${{ env.CHANNEL_ID }}"
2-
${{ env.SLACK_THREAD_YAML }}
1+
channel: "${CHANNEL_ID}"
2+
${SLACK_THREAD_YAML}
33
blocks:
44
- type: "section"
55
text:
66
type: "mrkdwn"
7-
text: "${{ env.MESSAGE_TEXT }}"
7+
text: "${MESSAGE_TEXT}"
88
- type: "section"
99
text:
1010
type: "mrkdwn"
11-
text: "${{ env.HEALTH_SLACK_EMOJI }} (${{ env.HEALTH_RATE }}) The integration test failed because of the following individual tests:"
11+
text: "${HEALTH_SLACK_EMOJI} (${HEALTH_RATE}) The integration test failed because of the following individual tests:"
1212
- type: "rich_text"
1313
elements:
1414
- type: "rich_text_preformatted"
1515
elements:
1616
- type: "text"
17-
text: "${{ env.FAILED_TESTS }}"
17+
text: "${FAILED_TESTS}"
1818
- type: "actions"
1919
elements:
2020
- type: button
2121
text:
2222
type: "plain_text"
2323
text: "View Workflow Run"
2424
emoji: false
25-
url: "${{ env.WORKFLOW_RUN_URL }}"
25+
url: "${WORKFLOW_RUN_URL}"
2626
- type: button
2727
text:
2828
type: "plain_text"

send-slack-notification/templates/integration-test/success.tpl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
channel: "${{ env.CHANNEL_ID }}"
2-
${{ env.SLACK_THREAD_YAML }}
1+
channel: "${CHANNEL_ID}"
2+
${SLACK_THREAD_YAML}
33
blocks:
44
- type: "section"
55
text:
66
type: "mrkdwn"
7-
text: "${{ env.MESSAGE_TEXT }}"
7+
text: "${MESSAGE_TEXT}"
88
- type: "section"
99
text:
1010
type: "mrkdwn"
11-
text: "${{ env.HEALTH_SLACK_EMOJI }} (${{ env.HEALTH_RATE }}) The integration test for *${{ github.repository }}* succeeded."
11+
text: "${HEALTH_SLACK_EMOJI} (${HEALTH_RATE}) The integration test for *${{ github.repository }}* succeeded."
1212
- type: "actions"
1313
elements:
1414
- type: button
1515
text:
1616
type: "plain_text"
1717
text: "View Workflow Run"
1818
emoji: false
19-
url: "${{ env.WORKFLOW_RUN_URL }}"
19+
url: "${WORKFLOW_RUN_URL}"
2020
- type: button
2121
text:
2222
type: "plain_text"

0 commit comments

Comments
 (0)