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

Terminate Bamboo Job with Python Exception

Billy Sharpe January 3, 2024

Hello all,

I have a Bamboo job that runs a Python script, followed by a PowerShell script. What I am trying to figure out, is if the python script throws an exception (like below), how can I make the bamboo job terminate so it does not run the PowerShell script? I tried passing sys.exit(1) but bamboo still reads it as 0.

except Exception as error:
    print(f"FAILED with the following error: {error} in the file: {filename}")
    sys.exit(1)
build	03-Jan-2024 16:18:49	FAILED with the following error: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). in the file: Afilename.xlsx
simple	03-Jan-2024 16:18:49	Finished task 'Run Python: CreateUploadForm' with result: Success

 Thank you!

Billy

1 answer

1 vote
Shashank Kumar
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 4, 2024

Hello Billy,

Welcome to Atlassian Community

If i understand correctly you have a single script task which contains Python and Powershell scripts.

You can try to use set -e at the beginning of the script task like below

#!/bin/bash
set -e

 Bamboo will terminate the task when it sees any error and won't execute the next commands.

You can read similar information on this thread https://community.atlassian.com/t5/Bamboo-questions/Bamboo-does-not-respect-exit-status-of-docker-compose/qaq-p/1197430?utm_source=atlcomm&utm_medium=email&utm_campaign=kudos_answer&utm_content=topic

Regards,

Shashank Kumar

**please don't forget to Accept the answer if your query was answered**

Billy Sharpe January 4, 2024

Hello!

Thanks for the info. 

Below is my current bamboo task list.

 

What I am looking to do is, if the script that runs python throws an exception, the powershell after it does not run.

 

According to bamboo, despite the exception being thrown, the script ran "successfully' and it continues running the powershell script. 
bamboo.png

I should mention that my bamboo servers are windows, not linux if that matters. 

 

Where do I add the -e?

 

Is it here? 

bamboo2.png

 

Thanks again! 

Shashank Kumar
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 9, 2024

Hello Billy,

Thanks for providing the clarification regarding the structure of your Job, now my understanding is clear.

Basically you have 1 task which is Python script and if this Task fails, you don't want another Task which is Powershell script to be executed.

In your case somehow Bamboo is returning exit code as 0, hence marking your Python script task as success. 

set -e option will not work here as it is for Linux and your scripts are getting executed in Powershell environment.

I am not an expert in Windows shell programming, you can try couple of Options.

Option 1:

You'll need to exit with the exit code if it's non zero manually, you can try something 

if ($lastexitcode -ne 0) { exit $lastexitcode }

Option 2:

You can try the below

echo "Exiting with return code: " %ERRORLEVEL%
exit %ERRORLEVEL%

 You can read more about these at

https://community.atlassian.com/t5/Bamboo-questions/How-do-you-capture-a-task-error-during-a-build/qaq-p/1238630

https://stackoverflow.com/questions/15777492/why-are-my-powershell-exit-codes-always-0

Regards,

Shashank Kumar

**please don't forget to Accept the answer if your query was answered**

Like Steffen Opel _Utoolity_ likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events