Execute Final script only on build failure
Asked Answered
S

2

5

I want to execute an arbitrary shell script, but I only want it to run on build failure. Is there some way to either:

  • Set up Bamboo to run this script only on build failure
  • Or check from within the script whether the build was successful. Perhaps an environment/build variable?

Thoughts and ideas appreciated!

Selsyn answered 29/10, 2014 at 15:58 Comment(0)
M
6

bamboo unfortunately lacks a way to trigger something on failure. You can not launch other plan, other stage and so on - you can only send basic notification.

As some workaround, you can move all the jobs to one stage, and have the script in the Final section. You can pass ${bamboo.buildNumber} to your script, and based on that number request status data from https://user:[email protected]/builds/rest/api/latest/result/PROJECT/BUILD_NUMBER.json?expand=stages.stage.results - then you only have to parse the json and get your build state from json['stages']['stage'][0]['state']

Myotome answered 26/11, 2014 at 8:25 Comment(0)
M
1

My solution to this issue is more rudimentary and consists of 3 custom tasks:

  • one at the end of the tasks list which creates a status file, name is arbitrary but consistent, e.g. build.success
  • one at the beginning of the tasks list that deletes the status file (in case workdir directory cleanup is not enabled)
  • one task in the beginning of the final tasks list which checks on the existence of build.success file and acts accordingly

My use case for the above is sending notifications to IM system not supported by Bamboo plugins (RocketChat). Current limitations:

  • final tasks which run after the first one (that consumes the build.success file) and fail are not detected
  • no detection of stopped runs: the final task is always executed
  • complex logic when there are multiple stages, e.g. I add an extra initial stage for sending notifications at build start and an extra stage at the end for sending a "successful build" notification

It's ridiculous that a CI/CD system would not implement some kind of env var like "bamboo.build.result".

Moment answered 27/1, 2020 at 14:36 Comment(1)
Checked my implementation again and got reminded that what I describe above is only used for deployments (i.e. deployment environments). My implementation for plan notifications is what @Myotome describes in his answer.Moment

© 2022 - 2024 — McMap. All rights reserved.