Meta-data in notification message text

There’s a value that gets calculated mid-way through my build (during the 4th step, to be precise). It’s just a simple short string. I’d like to include that value in the message text of a Slack notification that gets sent when the build finishes.

The value was previously only saved as an artifact (a text file), using buildkite-agent artifact upload "foo_mode.txt" . I figured it wouldn’t be possible to grab the contents of an artifact for the notification text, so I’ve made it also get saved as meta-data, using buildkite-agent meta-data set “foo-mode” “${FOO_MODE}” .

Currently I’m trying this in my build steps yaml (just after steps:):

notify:
  - slack:
      channels:
        - "#mychannel"
      message: "FOO_MODE: ${build.meta_data['foo-mode']}"

But that just results in the message text FOO_MODE: (i.e. no value).

Is there a way to insert a meta-data value in notification message text, as I’m trying to do?

If not, can anyone suggest another way that I can get this value coming through in notification message text?

2 Likes

Hi @jaza!

Env interpolation will only work there if the env itself is already set; but it won’t be set if the env: is in the same YAML as the notification. I’m not sure there’s a way for the meta-data interpolation to work there :confused:

Yes, that’s what I thought, that a given value has to be passed as an environment variable.

So I guess a better question for me to ask might be: is there any way that a value calculated during the build (i.e. it’s not available at the very start of the build) can be set as an environment variable, which would then be available for interpolation in the notification yaml?

1 Like

Did anyone work out how to inline meta data in the notifications? We also set a variable in a step that would be very useful in the slack notification so that it is not so generic.

Hi @flurdy,

Firstly welcome to Buildkite community and thank you for reaching out to us with your question.

One way to achieve what you wanted is to have your pipeline defined as stated in below example

- label: "notify with metadata"
command: "generate-notify-step.sh" | buildkite-agent pipeline upload

Now your generate-deploy-step.sh should have below steps:

FOO=$(buildkite-agent meta-data get foo)
echo '- label: "Step 1"'
echo 'command: "script.sh"'
echo 'notify:'
echo '- slack:'
echo 'channels:'
echo '- "#channel_name"'
echo 'message: "${FOO}"'

So you basically generate your pipeline yaml through this script along with the message for your notify. I hope this helps with your question and please feel free to let us know if you have any follow up questions.

1 Like

Thank you. That worked. We had to leave some failure notifications as non-stepped notify without the meta-data but our happy flows now include notifications with the meta-data.

(PS we changed from ’ to " and \ " so that the variable interpolation worked in the shell script, and also YAML indentations in the echos)

Thanks!

2 Likes

I’m getting

Invalid block scalar header

for

Do I need to modify this?

EDIT:
Removing the quotes around the script works.

command: generate-notify-step.sh | buildkite-agent pipeline upload

You’ll also need to give your agent permission to execute the script. I just prepend the command with

chmod u+x generate-notify-step.sh && ...
1 Like