Skip to content

Commit

Permalink
Add string substitution to template and custom parameters (#469)
Browse files Browse the repository at this point in the history
* Add env subst for custom, template and channel in notify script

* Only expand when starts by $

* Remove channel and add debug
  • Loading branch information
marboledacci authored Nov 14, 2024
1 parent faaaeb0 commit ed921f2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .circleci/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ jobs:
step_name: "Basic on hold template"
template: basic_on_hold_1
event: always
- run:
name: Generate and Export Slack Message
command: |
export SLACK_MESSAGE=$(node src/tests/generate-payload.js | jq -c .)
echo "export SLACK_MESSAGE='$SLACK_MESSAGE'" >> $BASH_ENV
- slack/notify:
debug: true
step_name: "Custom template generated dynamicly"
event: pass
custom: $SLACK_MESSAGE
- slack/notify:
debug: true
step_name: "Custom template with env var in the message"
Expand Down
8 changes: 8 additions & 0 deletions src/scripts/notify.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
#!/usr/bin/env bash
# shellcheck disable=SC2016,SC3043
if [[ "$SLACK_PARAM_CUSTOM" == \$* ]]; then
echo "Doing substitution custom"
SLACK_PARAM_CUSTOM="$(eval echo "${SLACK_PARAM_CUSTOM}" | circleci env subst)"
fi
if [[ "$SLACK_PARAM_TEMPLATE" == \$* ]]; then
echo "Doing substitution template"
SLACK_PARAM_TEMPLATE="$(eval echo "${SLACK_PARAM_TEMPLATE}" | circleci env subst)"
fi

if [ "$SLACK_PARAM_DEBUG" -eq 1 ]; then
set -x
Expand Down
12 changes: 12 additions & 0 deletions src/tests/generate-payload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const payload = {
blocks: [
{
type: "section",
text: {
type: "plain_text",
text: "This message was generated with Javascript",
},
},
],
};
console.log(JSON.stringify(payload)); // Outputs JSON

0 comments on commit ed921f2

Please sign in to comment.