Adam Terlson
Adam Terlson

Reputation: 12730

Execute Final script only on build failure

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

Thoughts and ideas appreciated!

Upvotes: 5

Views: 3200

Answers (2)

Alex
Alex

Reputation: 1639

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".

Upvotes: 1

kibitzer
kibitzer

Reputation: 4589

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']

Upvotes: 6

Related Questions