How to run next job/stage in case previous one Failed ONLY?
Asked by Viktoriia Mishchanyn:

I need to add mvn rollback stage in case the previous stage is Failed ONLY. It should not run in case previous jobs are successful. Can be between tasks as well

I was wondering if is there a bamboo variable I can use to add as a condition for with failed/passed value? and how can I correctly add this?


Answers:
author: Charlie Misonne
body: Hi [@Viktoriia Mishchanyn](/t5/user/viewprofilepage/user-id/5284929)

The easiest way would be to put your mvn commands in a shell script and handle the failed logic in there.  
You can easily catch the exit code of the mvn command and if it is a failure code you proceed with the mvn rollback.

If you want to build this with separate Bamboo stages/ tasks:

When the stage fails and becomes red in Bamboo it will not run any other stages or tasks afterwards except for the **final** stage.  
So you need to mark the stage where you do the mvn rollback as final.

In the final stage you'll need a way to know if the previous stage was successful.  
For that you'll have to catch the exit code of you intitial mvn command and store it as a Bamboo variable. Bamboo is not great at sharing variables between stages but here is a way: [Inject Bamboo variables task](https://confluence.atlassian.com/bamboo/configuring-a-variables-task-687213475.html)

In the final stage you can use a **condition** on the task where you execute mvn rollback where you check for the injected variable value

comments: author: Viktoriia Mishchanyn
body: Hi [@Charlie Misonne](/t5/user/viewprofilepage/user-id/502263) ,

Thank you very much for your response!

I would appreciate it if you could share more details as I still have difficulties resolving my problem.

<br />

Regarding the first solution (*put your mvn commands in a shell script* ):  
1) how do I know the agent host which I need to add to sshtask as a host? (the one that my bamboo plan currently using)  
2) directory from which to run my mvn commands would be ${bamboo.build.working.directory}?

Regarding the second solution:

1) where do I get the first mvn task results from?   
I was trying to add release:clean release:prepare release:perform --batch-mode *\> ${bamboo.build.working.directory}/build.log 2\>\&1* but couldn\`t find the log file anywhere later

2) or should I use *Use Maven return code* option to get the mvn task result?


author: Viktoriia Mishchanyn
body: Hi All,

I finally made *the first solution* work. Here are the tips who those who will step into the same problems:

1) *${bamboo.capability.HOSTNAME}* - variable to get your agent host

2) Simple of SSH command to run mvn which I took from bamboo logs while try to run as a mvn task:  
\<my/mvn/location/.../apache-maven-3.2.5/bin/mvn\> -Djava.io.tmpdir=/tmp/\<my-bamboo-plan_number\> release:clean release:prepare release:perform --batch-mode

3) also needed to add export java as it couldn\`t find java_home

++My final version of the script:++

*# Navigate to the project directory*   
*cd ${bamboo.build.working.directory}*

*export JAVA_HOME=\<..java64/openjdk17.0.4.1\>*

*# Log in to private docker register*   
*docker login -u \<username\> \<private_register\> -p \<password\>*   
*# Execute the Maven release process*   
\<my/mvn/location/.../apache-maven-3.2.5/bin/mvn\> -Djava.io.tmpdir=/tmp/\<my-bamboo-plan_number\> release:clean release:prepare release:perform --batch-mode

*# Check if the release succeeded*   
*if \[ $? -eq 0 \]; then*   
*echo "Release was successful."*   
*else*   
*echo "Release failed. Performing rollback..."*   
*\<my/mvn/location/.../apache-maven-3.2.5/bin/mvn\> -Djava.io.tmpdir=/tmp/\<my-bamboo-plan_number\> --batch-mode*   
*#mvn release:rollback --batch-mode*   
*echo "Rollback complete."*   
*fi*


author: Viktoriia Mishchanyn
body: Also, we found **Pre-Post Build Command Runner** plugin which probably will be useful for my general question however I did not try this yet.


author: Charlie Misonne
body: Hi!

I'm sorry I did not get back to your question sooner but I couldn't.

That's script is indeed what I had in mind. I'm glad you sorted it out!

While separate build tasks are easier to setup sometimes scripts are more powerful (and easier to migrate if you would ever move away from Bamboo).