I use the pre-exit script below to produce annotations linking to external URLs logged in each step. At this point, I have a lot of steps, so the annotations are getting pretty noisy. I’d like to detect the job’s status, and use info or error accordingly to make it easier to spot the annotations of interest.
I saw that I can use buildkite-agent to get a step’s state, but it appears the state is always running in the job’s pre-exit hook (though I should admit I haven’t tried to simulate a failure yet). Is there any way to do what I’m trying to do?
#!/usr/bin/env bash
set -euo pipefail
lines=$(awk '/cloud.nx.app/' "$BUILDKITE_JOB_LOG_TMPFILE")
if [ -z "$lines" ]; then
exit 0
fi
urls=$(echo "$lines" | grep -o 'https[^ ]\+' | sort | uniq)
if [ -z "$urls" ]; then
exit 0
fi
cat << EOT | buildkite-agent annotate --style info --context "nx-$BUILDKITE_JOB_ID"
## $BUILDKITE_LABEL NX Cloud Runs
$(echo "$urls" | awk '{print "- ["$1"]("$1")"}')
EOT