Skip to content

Instantly share code, notes, and snippets.

@noahlz
Last active June 29, 2020 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save noahlz/ec8313a0af81ea7e6f36 to your computer and use it in GitHub Desktop.
Save noahlz/ec8313a0af81ea7e6f36 to your computer and use it in GitHub Desktop.
Post Bamboo Builds to Slack via curl
#!/bin/bash
WEBHOOK_URL=$1
# https://confluence.atlassian.com/display/BAMBOO050/Bamboo+variables#Bamboovariables-Build-specificvariables
PLAN="${bamboo.buildPlanName}"
RESULTS="${bamboo.buildResultsUrl}"
KEY="${bamboo.buildResultKey}"
BRANCH="${bamboo.repository.branch.name}"
# OMGWTF Hack because Bamboo interpolates the variable name if no value is set.
# OMGWTF2 if [[ "$TRIGGERED_BY" = *"ManualBuildTriggerReason"* ]] doesn't work, so I had to use shell "case" block.
case "$TRIGGERED_BY" in
*"ManualBuildTriggerReason"*) TRIGGERED_BY=Git
;;
esac
echo "Plan Name: $PLAN"
echo "Plan Key: $KEY"
echo "Plan Branch: $BRANCH"
echo "Plan URL: $RESULTS"
echo "Triggered By: $TRIGGERED_BY"
WEBHOOK_JSON="payload={\"channel\": \"#bamboo-builds\", \"username\": \"bamboo\", \"icon_emoji\": \":bamboo:\", \"attachments\": [ { \"fallback\": \"Build Started: <${RESULTS}|${PLAN} - ${KEY}>\", \"pretext\" : \"Build Started\", \"title\": \"${PLAN}\", \"text\" : \"<${RESULTS}|${KEY}>\",\"fields\": [ { \"title\": \"Branch\", \"value\": \"${BRANCH}\", \"short\": true }, { \"title\": \"Triggered By\", \"value\": \"${TRIGGERED_BY}\", \"short\": true } ] } ] }"
echo "Sending JSON to Slack:"
echo $WEBHOOK_JSON
curl -s -X POST --data-urlencode "$WEBHOOK_JSON" $WEBHOOK_URL
@noahlz
Copy link
Author

noahlz commented Oct 31, 2015

Environment variable configuration:
TRIGGERED_BY=${bamboo.ManualBuildTriggerReason.userName}

Because Bamboo just puts the literal variable into the script if it is not set with a value, so we hide it in valid variable name (variable names cannot have dots in them).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment