Last active
June 29, 2020 15:22
-
-
Save noahlz/ec8313a0af81ea7e6f36 to your computer and use it in GitHub Desktop.
Post Bamboo Builds to Slack via curl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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).