Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Execute Final script only on build failure

Adam Terlson October 29, 2014

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

1 vote
cbenard1
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 29, 2014

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

Turner

Adam Terlson October 29, 2014

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.

Adam Terlson October 29, 2014

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

cbenard1
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 29, 2014

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

adamterlson October 29, 2014

@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?

cbenard1
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 29, 2014

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

adamterlson October 29, 2014

@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?

cbenard1
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 29, 2014

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

adamterlson October 29, 2014

@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?

cbenard1
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 29, 2014

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

Adam Terlson October 29, 2014

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?

Adam Terlson October 30, 2014

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

cbenard1
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 31, 2014

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

Adam Terlson October 31, 2014

@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?

cbenard1
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 31, 2014

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

Zsolt Bako January 10, 2015

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

0 votes
Krystian Brazulewicz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
October 29, 2014
Adam Terlson October 30, 2014

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

Krystian Brazulewicz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
October 31, 2014

Thanks, I fixed the link

Adam Terlson October 31, 2014

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

Zsolt Bako January 10, 2015

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

Krystian Brazulewicz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 12, 2015

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

Zsolt Bako January 12, 2015

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.

Krystian Brazulewicz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 13, 2015

Please contact support on this. This should definitely work

phil October 27, 2017

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
TAGS
AUG Leaders

Atlassian Community Events