Accessing meta-data varibale that was set in a previos step by a bash script

Hello all!

I have created a bash script that sets a variable we can call failure_reason using meta-data. I want to use that value to include it in a slack notification message. This is how I am setting the value:
buildkite-agent meta-data set “failure_reason” “failure_reason_value”

To access the value I have tried several approaches. Including:
1-) In the same pipeline step where the bash script is called I added a notify step:

notify:
- slack:
channels:
- “#channel-alerts
message: "${failure_reason}”

2-) I tried following this thread ( Meta-data in notification message text ) but also did not work

Any advice on how to get this done? I have been stuck for quite a while. Thanks a lot

Hey @iclark :waving_hand: Welcome to the community!

So meta-data is a bit tricky, in that once you’ve set that meta-data key from your script, you’ll need to call it again in order to reference it in your Slack message, via buildkite-agent meta-data get. It won’t be available to you in your pipeline YAML.

It’s mentioned in that thread you linked, but you’ll need to have another script (or use your existing script to do this) which uploads the Notify step, using the meta-data you set previously.

The script that generates and uploads that step might look something like this:

#!/bin/bash

FAILURE_REASON=$(buildkite-agent meta-data get failure-reason)

cat <<- YAML | buildkite-agent pipeline upload
steps:
  - label: ":slack: :buildkite:"
    command: echo "Slack notifications"
    notify:
    - slack:
        channels:
        - "#pipeline-notifications"
        message: |
            Your notification message with variables (${FAILURE_REASON}) here, like Pipeline: ${BUILDKITE_PIPELINE_SLUG}, Build: ${BUILDKITE_BUILD_NUMBER}
YAML

And you would have a pipeline that looks something like this to invoke it:

steps:
  - command: buildkite-agent meta-data set "failure-reason" "failure"
  - wait: ~
  - command: ./failure-reason-notification.sh

Hopefully that helps, but reach out if you have any questions :slight_smile:

Hey @Jeremy, thanks again for your help — your solution worked well for the most part. I’m just running into one final issue.

The Slack notification step shows a “Passed” (green) status, even though the message correctly includes the failure reason. I’m guessing this happens because the notification command itself runs successfully, regardless of the pipeline outcome.

Is there a way to make the notification step reflect the actual failure state of the pipeline? This is the last piece I need to get working.

@iclark - You are correct. The Slack notification is showing as “Passed” because the command step that the notification was sent from passed. One way you could make the notification more representative of the state of the pipeline would be to evaluate the failure-reason meta-data value and selectively provide an exit code that can be used along with the soft_fail attribute. Having a step that has “soft” failed will show a red status in the Slack notification, but will not contribute to the build being marked as failed.

Here is an updated example of what this might look like (using @jeremy‘s example):

#!/bin/bash

FAILURE_REASON=$(buildkite-agent meta-data get failure-reason)

if [[ -n "${FAILURE_REASON}" ]]; then
  cat <<- YAML | buildkite-agent pipeline upload
  steps:
    - label: ":slack: :buildkite: Send Slack notification"
      commands: |
        exit 123
      soft_fail:
        - exit_status: 123
      notify:
      - slack:
          channels:
          - "#pipeline-notifications"
          message: |
              Your notification message with variables (${FAILURE_REASON}) here, like Pipeline: ${BUILDKITE_PIPELINE_SLUG}, Build: ${BUILDKITE_BUILD_NUMBER}
YAML
fi

This example also only sends a Slack notification (via a conditionally uploaded pipeline step) when the meta-data for failure-reason has been previously set.

Do let us know if you have any other questions!