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

Is there a bamboo variable with the build job's result?

Ken Wood
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.
September 21, 2012

Is there a bamboo variable with the build job's result? I cannot find one in the docs...

I have this situation:

1. In the first stage I need to start a server.

2. In every subsequent stage, if the job or jobs suceed, I need to leave the server running for the next stage

3. After the last server related stage, I'll have a final stage to shutdown the server.

The problem is, between 1.) and 3.), if any job fails I need to shutdown the server because the build will never get to the final cleanup stage.

I have a cleanup script I want to put into each job's 'final task', but if the job succeeded I want to skip the final task so the build can go on to the next stage WITHOUT DOING CLEANUP.

According to the docs, the 'final task' is always run, so I have to put a conditional in the script called from the final task so I can leave the server running if the job succeeded, and kill the server if the job failed.

10 answers

1 accepted

4 votes
Answer accepted
ArmenA
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.
December 26, 2012

Hi Kenneth,

Thanks for the question. I am not sure if you found a way to realise your plans (the question was raised in September), but I will still try to provide some useful information. As far as I understand, all you need is a conditional to check the states of your jobs.

  1. As per Bamboo's REST API documentation, you can use a REST API call from a Script task to check if a specific job failed (state="Failed") or succeeded (state="Successful"):
    curl -X GET --user admin:admin "http://localhost:8085/bamboo/rest/api/latest/result/PROJ2-STAGETEST-JOB1?buildstate"
  2. One thing to keep in mind is that jobs inside the same stage can possibly run parallel, so if you want to guarantee a specifc running order for your jobs, put them into separate sequential stages.
  3. You can add a script task to the beginning of every job in each stage to check the status of the previous job (from the previous stage) and decide how you want to continue the workflow (to shut down your server or let the execution of the plan continue)
  4. In the last stage you can add a final task to shut down your server as all the previous stages were successful.

Please let me know if you have any further questions or concerns.

Cheers,
Armen

Artur Grigoryan
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 17, 2019

հաճելիա, հաճելիա

3 votes
Vinh Hong March 24, 2020

I found these in version 6.9, but not documented anywhere.

${bamboo_buildFailed} for build plan
${bamboo_jobFailed} for deploy plan

Their values are either 'true' or 'false'.  You can catch this status in 'final-tasks' which makes clean up or notifications much easier.

M_D_ Klapwijk November 14, 2020

Let me just summarise that with 1 word: GREAT!

For the deployment the jobFailed works perfectly on 7.1.3, although it is only present in a script task, not in an ssh-task...

2 votes
Robert McKinnon
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 8, 2015

An approach that I use that seems to works is to create a tracking file.

Step 1: create a tracking file with a starting value in the contents.
Step 2: executes other build tasks.
Step 3: change the value of the tracking file.

In Final Task 
Step 4: check content of tracking file and act accordingly.  

If the job fails before step 3 then step 3 is never ran. Step 4 is alway ran because it is a Final Task at which point you can check the tracking file to see how far you job got.

0 votes
Richard Atkins
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 6, 2016

The current job status is available to custom Java tasks through the TaskContext passed to the Task's execute method. For example:

public TaskResult execute(TaskContext taskContext) {
    CurrentBuildResult currentBuildResult = taskContext.getBuildContext().getBuildResult();
    currentBuildResult.getBuildState(); // indicates the status of the whole job
    currentBuildResult.getTaskResults(); // indicates which tasks have passed or failed or not yet run within the current job

As far as I can tell, this same information is not automatically available to simple shell script tasks. If you don't want to maintain a Java plugin for Bamboo, you would have to fall back to using a temporary file within the build (created by the last task run in a successful run) to indicate build status, and add a check for this file in the final script task.

0 votes
ShantiS
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 12, 2014

Hello,

I would like to know if there is a way to prevent a code push on a Bitbucket branch if Bamboo is currently building a job from that branch. So a couple of things I am trying to figure out are:

(1) How does one figure out is a Bamboo job is in progress using HTTP or othe rmeans? Bamboo's "buildstate" variable does not seem to have a value, like, "running" for in-progress jobs.

(2) Does Bitbucket have a pre-commit hook that can then check for Bamboo's "buildstate" variable on a branch before accepting a code push into that branch?

Thanks,

-Shanti

0 votes
Oleksandr Presich July 29, 2013

Now I see, but that does not fit into our workflow.

We have some custom scripts with different behaviour that depends on build result: is it Successfully finished or Failed.

I can run those scripts if Build finished successfully. But, as I see, there is no way to run scripts if Build failed:
a) within the same Stage: because Job's build state is Unknown until Job is finished
b) within another Stage: because Stage 2 will not start if Stage 1 fails

I was able to get what I want with Pre/Post Build Command Plugin. We just wanted to get such behaviour out-of-the-box.

Anyway thanks for your suggestions!

0 votes
ArmenA
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.
July 29, 2013

Oleksandr,

I didn't use it from Bamboo; I used it from a different browser tab just to show you the different values of the state component. You can use a command like 'wget' or 'curl' with a similar URL to get the results from a script.

Armen

0 votes
Oleksandr Presich July 28, 2013

Thanks Armen,

could you advise where have you called this REST API requset: http://localhost:8085/rest/api/latest/result/PROJ-PLAN-JOB1/7 ?

From the Final Task of Job 1 or from another Job of Stage 1?

0 votes
ArmenA
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.
July 28, 2013

Hi Oleksandr,

You are right - Stage 1 must finish before Stage 2 can start. I tried to use this type of REST calls to get the status of Job1 in Stage 1 and Job2 in Stage 2:

http://localhost:8085/rest/api/latest/result/PROJ-PLAN-JOB1/7
http://localhost:8085/rest/api/latest/result/PROJ-PLAN-JOB2/7

When the first job was done, the status information was as:

<result onceOff="false" id="1474588" number="7" lifeCycleState="Finished" state="Successful" key="PROJ-PLAN-JOB1-7" expand="changes,testResults,metadata,logEntries,plan,vcsRevisions,artifacts,comments,labels,jiraIssues">

When Job2 hasn't started yet, this was the result:

<result onceOff="false" id="1474589" number="7" lifeCycleState="Pending" state="Unknown" key="PROJ-PLAN-JOB2-7" expand="changes,metadata,logEntries,plan,vcsRevisions,jiraIssues">

Does this answer your question? Yuou can experiment with the REST API and see what Bamboo shows in different scenarios.

Armen

0 votes
Oleksandr Presich July 17, 2013

Hi Armen,

please explain how we can run a Job with such REST API call from Stage 2, if Stage 1 already failed.

As I understand, Stage 2 will not start if Stage 1 fail.

I'm trying to find a way, how to check build status (Success/Fail) within one Stage. But it seems, that current REST API do not provide Job status information unitl this Job is finished.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events