How can I attribute a label to a Bamboo build result based on its outcome (success or failure)?
Purpose
We need to apply a label to a Bamboo build result, depending on if the build succeeded or failed.
Solution
One option is to use the Bamboo automatic labelling which will use a Regex pattern to apply a label based on the contents of your build log. Your build log will need some kind matchable indicator that the build was a success or fail for this to work. Please see the following documentation on how you can configure this:
The other alternative involves the add-on Pre-Post Build Command Runner. This a free add-on and it allows you to run an arbitrary command before a build runs or when a build passes or fails.
Here's an example of how you can configure a simple label to a build result after it successfully completes using a combination of the Bamboo REST API and Pre-Post Build Command Runner.
- Install the Pre-Post Build Command Runner add-on
- Access the
Plan Configuration >> Job >> Miscellaneous
page for the plan you're going to implement it on. - Go to the Post Build Commands (this is from the add-on you just installed).
- Select if you want to have the command run from the Agent server or Bamboo server.
Paste the path (inside the Success Command field) to a script file located on either the agent or Bamboo Server, you chose and add the following parameters at the end (example):
/Users/Atlassian/Desktop/script.sh "${bamboo.planKey}-${bamboo.buildNumber}"
The script file doesn't exist yet, so let's create it, below is an example of the content of the script file that will execute a POST command against Bamboo's API to add a label to the build:
#!/bin/sh curl -u user:password -H "Content-Type: application/json" -X POST -d '{"name":"LABEL-NAME"}' "http://BAMBOO-BASE-URL/rest/api/latest/result/$1/label"
With the above, every time a build is run in that plan, the add-on will trigger the post command that will execute the script file and add a label to the last build that completed successfully (via REST API). You can do the same with the Failure Command field for the opposing scenario.