Setting up notifications within a pipeline step

Are there are ways around setting up a specific/conditional notification within a step or if it can be added to “notify: if:” block at the end of the pipeline to send a notification when a particular step fails?

1 Like

Hey @Soneeka!

Have you had a look at agent step outcomes? That could be utilised dynamically to then post a notification pertient to the result of said step you obtain.

You definitely also can use conditionals with the notify step too!

@james.s Does the conditionals support the status of the step? I’m trying to send slack notification only if the step failed.
something like this

    notify:
      - slack: "#channel"
        if: step.state == "failed"

Hi @Daniel,

Lizette here jumping in for James! :wave:

The syntax in your example is correct but it will fail since the step.state is currently not a supported conditional variable.

What James has suggested is to use the step outcome and create the notification dynamically. Here’s a sample yaml step:

steps:
  - label: echo This step will fail""
    command: exit 1
    key: "main-step"  
  - wait: ~
    continue_on_failure: true
  - command: |
      if [ $$(buildkite-agent step get "outcome" --step "main-step") != "passed" ]; then
         cat <<- YAML | buildkite-agent pipeline upload 
         steps:
           - label: "Notify slack about failing step"
             command: echo "The main step has failed!"
             notify:
               - slack:
                   channels:
                     - "#channel" 
      YAML
      fi

Cheers!

Thanks @lizette, having an example helps. It would be great if we can have a conditional variable on step output. We have many steps in a pipeline and each step belongs to a different team that needs to be notified so I wish there’s a cleaner way to do this.

1 Like

Hey Daniel, I was wondering this too. The way I’ve gone about it is something similar to what Lizette has done except incorporate a .sh file with bash script to set the condition and notify slack out of the pipeline.
The project and webhook are variables defined in the pipeline. Hope that makes sense.

#!/usr/bin/env bash
set -uo pipefail  # do not include e option as this would cause the script to exit if it fails

step="$1"
project="$DBT_PROJECT_NAME"
webhook="$ALERT_SLACK_CHANNEL"

message="$step in $project has failed"

generate_post_data() {
  cat <<EOF
  {
      "text": "$message"
    }
EOF
  }

# Check if the outcome of the step is "failed"
if [ "$(buildkite-agent step get "outcome" --step "$step")" != "passed" ]; then
  # Send to slack channel
  curl -X POST -H 'Content-type: application/json' --data "$(generate_post_data)" $webhook
fi
2 Likes

Thanks @Soneeka for that suggestion.

@Daniel , would the above approach work better for you in this scenario?

@lizette It might work, but it’s still a workaround that requires an extra step to send the slack, not as clean as having it in the step config, so I still would like to make a request for this feature.

2 Likes

I would also like this feature! It is awkward because I only want to notify if a step fails and not if the whole build fails. Supporting build.state and not step.state is just frustrating when you can have notify clauses on specific steps.

Another problem that comes up because step.state is missing is that you cannot easily link back to the actual failed step. You have to manually construct the url back to the job and it won’t show up in the expected link location. The View Step button becomes an alternate and unavoidable wrong piece of information.

Hey folks!

Thanks for the feedback! Currently, the only option to get the outcome of a step is with `buildkite-agent step` v3 | Buildkite Documentation Conditionals are evaluated at pipeline upload, so we don’t know if a step failed or passed within the same step (even having the step.state we cannot determine if it ended; the step would be still running). Dynamic pipelines were created for those use cases.
But we’ll take this into consideration if we decide to change the pipeline upload behavior.

Best!

1 Like