Slack Notify with Custom Condition
canfelli25 opened this issue ยท 4 comments
Describe Request:
Hi, I have a case where I need to notify slack when a certain condition happen. But this condition doesn't bound to the CI job's state of fail or pass. After exploring a bit I don't think this orb can accommodate my need, please correct me if I'm wrong. Is it possible to add this feature? I'm thinking it can be implemented like how to implement condition in CircleCI Job.
Examples:
version: '2.1'
orbs:
node: circleci/node:4.1
slack: circleci/slack@4.1
jobs:
deploy:
executor:
name: node/default
steps:
- checkout
- node/install-packages
- run:
command: npm run deploy
- slack/notify:
channel: ABCXYZ
event:
- when:
- equal: [<< variable_a >>, "intended condition a"]
- and: [<< variable_b >>, "intended condition b"]
template: success_tagged_deploy_1
workflows:
deploy_and_notify:
jobs:
- deploy:
context:
- slack-secrets
Supporting Documentation Links:
I am basically in the same situation, it seems that there is no custom condition as far as I checked in the source code.
I would also support it if we can make it work with custom conditions, maybe something better than what I came up with:
#!/bin/sh
# Check if notifications should be sent
should_send_notification() {
local condition="$1"
# Set custom condition to "not_notify" to suppress notifications
if [ "$condition" = "not_notify" ]; then
echo "Notification suppressed due to custom condition: $condition"
return 1
else
return 0
fi
}
# Before sending the Slack notification, call should_send_notification and check the return value
if should_send_notification "$CUSTOM_CONDITION"; then
PostToSlack
else
echo "Notification suppressed due to custom condition: $CUSTOM_CONDITION"
fi
As a workaround you could use circleci-agent step halt that executes under your desired conditions. Example:
version: '2.1'
orbs:
node: circleci/node:4.1
slack: circleci/slack@4.1
jobs:
deploy:
executor:
name: node/default
steps:
- checkout
- node/install-packages
- run:
command: npm run deploy
- run:
name: Condition
command: |
if [[ $intended_condition_a && $intended_condition_b ]] ; then
echo "Conditions met, proceeding with Slack notify"
else
circleci-agent step halt
fi
- slack/notify:
channel: ABCXYZ
template: success_tagged_deploy_1
workflows:
deploy_and_notify:
jobs:
- deploy:
context:
- slack-secrets
A problem with above is that circleci-agent step halt
sets the build to pass.
In case we need build to fail BUT do not want the slack notification to be triggered, above won't work.
@atanass any solution for it?
A problem with above is that
circleci-agent step halt
sets the build to pass.In case we need build to fail BUT do not want the slack notification to be triggered, above won't work.
@atanass any solution for it?
Would explicitly killing the job with exit 1
at the right moment work for you?