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

Script conditional condition depending on success of previous task

Adrian Raszkiewicz August 2, 2023

Hello,

I want to include a script with an IF condition, depending on the outcome of the task that comes before it. 

 

I have a JUnit Parser task which will either fail or succeed. I would like to catch the result and have different outcomes, like so:

IF 'PREVIOUS_TASK' = success:

          DO A

IF 'PREVIOUS_TASK' = fail:

          DO B

 

Now I am not sure how to catch the result of the previous task and have it evaluate against the if statement.

 

EDIT: Ideally, is there a way to check a task fail/succeed, without querying the API? 

simple	02-Aug-2023 09:55:00	Finished task 'Parse Test Results' with result: Failed

Thanks! 

2 answers

1 accepted

1 vote
Answer accepted
Adrian Raszkiewicz August 9, 2023

#!/usr/bin/env bash

## bamboo does not catch some script failures unless this flag is set

set -e

TASK="CHECK BUILD STATUS"

echo ================== START $TASK ==================

## bamboo_jobFailed is a boolean
BUILD_STATUS=${bamboo_jobFailed}
echo "BUILD_STATUS_failed?: $BUILD_STATUS"

text=$"@here Hi all! There will be a delay in the Superset data refresh today, we are looking into it :slightly_smiling_face: Thank you!"

if [[ "$BUILD_STATUS" == "false" ]]
then
echo "the build is passing"
else
echo "the build has failed"
curl -X POST -H 'Content-type: application/json' --data "{\"text\" : \"$text\"}" https://hooks.slack.com/services/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
fi

echo =================== END $TASK ===================
1 vote
Eduardo Alvarenga
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 2, 2023

Hello @Adrian Raszkiewicz,

Welcome to Atlassian Community!

You can have your task run based on a condition that checks for a variable. You need to add an Inject Variables Task to your plan that would consume a properties file. Hence, once the variable(s) are set, you can enable the condition on the task so it knows if it should run or not.

This is the workflow:

  1. Modify the "PREVIOUS_TASK" and have it create a properties file with a set of key=value lines
  2. The "PREVIOUS_TASK" should always succeed (return 0), even if it had failed. For that you'd have to wrap it in a script to force it to succeed, otherwise Bamboo will not advance to the next task
  3. Add an Inject Variables Task to the Plan that would consume the properties file from step #1 and set them as variables
  4. On Tasks "DO A" and "DO B" check the "Add condition to task" box. That would evaluate the injected variables from step #3 and make the decision to execute that task or skip it.

Kind regards,

Eduardo Alvarenga
Atlassian Support APAC

--please don't forget to Accept the answer if the reply is helpful-- 

Adrian Raszkiewicz August 3, 2023

Hey Eduardo!

Appreciate the steps, the previous task I have is a 'JUnit Parser' task so not entirely sure how I could have that always succeed? 

Also, how could I have a task create a properties file with a set of key=value lines. saving either a success/fail variable from previous job? I assume I would need to check if job successful = assign variable. Which is what I am trying to do in the conditional? 

 



Basically I have a script task running 'Python Unit Tests', which succeeds(technically), but than fails on the 'JUnit Parse' task when at least 1 test has failed.

Perhaps I could check for the 'Python Unit Tests' component, and then have my conditional task ran before the JUnit Parser?  Or if there is a way to force the JUnit Parser into success to do it the way described.


**I basically want to send a custom slack message only if my tests fail. I tried the using the Notification option which works well if the plan has failed - but I am in need of a more customisable message. 

Eduardo Alvarenga
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 3, 2023

Hello @Adrian Raszkiewicz,

The JUnit Parser task in Bamboo will not allow you to control or wrap its output or exit code. If you want that level of control, you'd have to run your JUnit tests from the command line in a Script Task, or even Maven or Gradle script if you wish.

Check this page for ideas on how to run the tests:

Best regards,

Eduardo Alvarenga
Atlassian Support APAC

--please don't forget to Accept the answer if the reply is helpful-- 

Like Steffen Opel _Utoolity_ likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events