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

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.

9 answers

1 accepted

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

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.

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.

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

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?

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

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!

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

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.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Monday in Jira Ops

Jira Ops Early Access Program Update #1: Announcing our next feature and a new integration

Thanks for signing up for Jira Ops! I’m Matt Ryall, leader for the Jira Ops product team at Atlassian. Since this is a brand new product, we’ll be delivering improvements quickly and sharing updates...

427 views 0 8
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you