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:

- 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!

2 answers

This widget could not be displayed.

Hi Adam,

Currently Bamboo builds are not able to access active data about a build from within that build. The data required to determine that a build failed would not be available until the build completed, not giving the script time to run. Depending on the types of tasks you are running in Bamboo you could potentially create a test task that runs the script based on the results so far.

It would take the results of the build from the last Job that would usually run just before the build finished. Then it should parse those, and run the script based on the results, before sending the exact same results it tested on and sending those to Bamboo.

If this doesn't work for your particular workflow could you please detail what you're looking to do here so I can maybe provide further assistance.




Thanks for the reply Turner. I consider my use-case to be quite simple. We use Slack, and I want to message my team in Slack when the build goes down via a curl command which does a POST with a JSON payload. I just want to fire off a curl when the build fails. If you have an idea for a different approach I'm all ears! Basically I want exactly what bitbucket does with its notifications drop down with hipchat, except with Slack.

@Carlen Benard Another thought I had: can I set a variable from one script task and read that variable from another script?

Hi Adam, That makes perfect sense. What you'll wanna do to achieve it is this: - Add a new task at the end of your build that takes the results as input - Have this new task check for a non-zero exit code (this is the check Bamboo does to determine pass v fail). -If the build exit code is non-zero (aka would fail to Bamboo) have it run the script then push the results it got as input to their appropriate test or out to Bamboo. - If the build would succeed just pass Bamboo a good exit code Thanks! Turner

@Carlen Benard Phew, that seems like a good approach, but I'm not sure how to start due to my relative newness to Bamboo. Can you provide any code snippets or docs that might get me started?

Hi Adam, Unfortunately I do not know of any docs that have the exact code bits you would need to accomplish this. No promises but if you can provide a bit more detail on what your build is doing I may be able to point you in the right direction. The way I see it now: - Have your current build output it's results to a file (xml, log, text, etc.) - Have the current build save that file somewhere accessible to the build (local or remote server where it can be downloaded from though this may be redundant) - Create a new script that runs at the end of the build. This guy will pull in the newly created output file, parse it to determine if it's a failure, and respond according. Unfortunately without specifics of the code you're currently running I have to give you general terms! Best of luck! Turner

@Carlen Benard I'm doing a node build, so it's quite simplistic. I currently have a single stage that has a single job: Build and package. This (Task 1) gets the code from bitbucket, then (Task 2) runs a script which does things like "npm install" and then bundles up a zip file which represents the build artifact. I suspect the presence of this build artifact could be used as a description of "success" of the build. I just need to know: 1) How to run a script that will be able to look for this log file regardless of the previous task failing or not, 2) How to check to see if the artifact file exists from an inline script. As a side note, can you get me up to 25 rep so we can continue this conversation without me needing to create another account to get around the 3 post limit per 24 hours?

Hi Adam, You should be able to respond with one of the two accounts here. I've gotten it a couple upvotes/rep. Please allow me to address your questions in order. 1) You'll want to create a script and drag it to the "Final Task" section of the job:https://www.dropbox.com/s/e6fvrykga3cychf/Screenshot%202014-10-29%2015.16.33.png?dl=0 1.5) In order to create that file initially you'll want the original script creating your artifact to direct output to a file. 2) Since the task is final (after the other build tasks) Bamboo will already have identified whether the artifact was created. If you'd like to incorporate that into the final script you'd have to first check where the artifact is created locally before being copied back to Bamboo sever. Run the build once and look at the logs to find this out. Once you have the location of the artifact you'll just substitute the upper level folder name (all named after builds) with a variable supplying your current build. Now you can have the script manually crawl the folder and check for the file. Hope this helps! Turner

@Carlen Benard Maybe this is easier... How do I grep the log file for my build? As in, the log file that I see on the Logs tab of the build summary screen?

Hi Adam, Apologies for not doing a great job explaining. Maybe I can give you another approach that makes more sense. This will build off of your current job: - Create a new task that simply says "touch buildfinished" -- This should create a file in Linux/Unix - You'll put this task at the end of your job _but_ before the 'final task' line -- This way if the build fails in any way this task won't be reach and the {{buildfinished}} will not exist. - Create a second task that will be a final task that will check for the existence of that file -- If the build failed the file won't exist and the script can fire off the curl command to notify slack -- If the build succeeds the final task will check for the file (it'll exist) and simply delete it, allowing the build to complete as normal Please let me know if I can clarify this further! Turner

I think that sounds like a really good approach. What path do I use to look for that file? http://stackoverflow.com/questions/638975/how-do-i-tell-if-a-file-does-not-exist-in-bash <--- Here's what I'm planning on doing. What do I replace '/tmp/foo.txt' with?

@Carlen Benard Can you help me just this *one* last time? I think with understanding this last bit I'll be squared away and can stop bugging you! Thank you for all your help!

Hi Adam, You can replace it with anything. The specific path will depend on the file structure of your remote agent instance. There isn't a requirement for where the file needs to go and it'll be deleted anyways so wherever you like! Thanks! Turner

@Carlen Benard Thanks for the reply. I was more asking, how do I refer to the same directory from both jobs? Will a relative path work? Do I need to use an environment variable? If I did `touch somefile.temp` in one job, can I then just do `if [ ! -f somefile.temp ]; then` in the next?

Hi Adam! Sorry I can't be more specific and have to keep suggestions general (as I don't know your specific env). A relative path should do the trick, you can also definitely search for it. If it were me I would do a simple search, but that's simply preference. You can configure the path to an environment variable if you like, but it's not necessary. Thanks! Turner

Hi guys! Thank you Turner, your instructions made perfect sense. I had the same situation, and with this tricky workaround I was able to send notification via Slack. I hope that you will consider a more simple integration in the future :) Adam: it works with relative path ;) simply use a file 'buildfinished' without absolute reference in both scripts Zsolt

This widget could not be displayed.

Sorry, I think you may have posted the wrong link or something.

Thanks, I fixed the link

Yes indeed it does do exactly what I want. However, it is not available for cloud.

Could be a good solution, but my install was unable to add this plugin :( (bamboo 5.7.1)

@Zsolt Bako - what was the issue with the installation?

UPM was unable to install it because it's a 'version 1' type plugin... so I downloaded and placed it into the right directory, restarted bamboo, and it showed the plugin, but didn't allow to enable it.

Please contact support on this. This should definitely work

We were able to install it and it shows as active but it does not appear to have enabled any of the functionality.

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted yesterday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

78 views 1 0
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you