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

How to get build status in a Bamboo build task script?

Hélène_Droal
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!
February 14, 2019

Hello,

I would like to call the github API at the end of the build in order to tell him if the build was successfull or not.

I've created a final task for this but I can't find a bamboo variable who contains the build state.

Could you help me?

Thanks :)

Hélène

3 answers

1 accepted

5 votes
Answer accepted
Hélène_Droal
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!
February 15, 2019

Okay so I found the perfect variable (undocumented) : `bamboo_jobFailed` :)

Bharath_Venkat_Rajan March 22, 2020

Exactly what I wanted. Thanks @Hélène_Droal 

Elizabeth Goldstein
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!
November 10, 2022

Thank you @Hélène_Droal this is going to save me a headache with the API! 

Vigneshwaran UK
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!
November 24, 2022

Can someone please share the api, it would be helpful for me.
Iam searching it for past week couldn't able to get the proper one.
Thanks

PatrickS
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!
April 17, 2023

Github Commit Status API:

https://docs.github.com/en/rest/commits/statuses#create-a-commit-status

Script Task in Bamboo:

REPO="repo-owner/my-repo-name"

if [[ "$bamboo_jobFailed" == "false" ]]
then
    STATE=success
else
    STATE=failure
fi

DATA="{\"state\":\"$STATE\",\"target_url\":\"$bamboo_buildResultsUrl\",\"description\":\"Bamboo build succeeded!\",\"context\":\"default\"}"

curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $bamboo_github_token"\
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/$REPO/statuses/$bamboo_repository_revision_number" \
-d "$DATA"
Suguna Tejaswini Nandakumara August 23, 2023

This is not working anymore - 

${bamboo_jobFailed} 

Is there any other way we can get the build status in the script task? 

3 votes
Phill Pafford May 19, 2023

looks like there was a syntax update to reference the variable, example script

${bamboo_jobFailed} instead of $bamboo_jobFailed

 

#!/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: $BUILD_STATUS"

if [[ "$BUILD_STATUS" == "true" ]]
then
echo "the build has failed"
else
echo "the build is passing"
fi

echo =================== END $TASK ===================
Adrian Raszkiewicz August 3, 2023

Hey!

 

Do you mind me asking how accessing this variable works? I cannot seem to be able to call it in my script, as if it doesn't exist? 

Phill Pafford August 3, 2023

Are you running the script in a

Final Tasks

so the script always runs, even if the build fails

 

Here is how I set it up

  • Default Stage -> Tasks -> Final Task 

(Scripts is the task type)

Like Adrian Raszkiewicz likes this
Adrian Raszkiewicz August 3, 2023

Hey! 

My bad, I did not read the top of the script properly.... I think it works like a charm now!

 

This will always grab the status of the current job only right? I don't need to bother with passing the jobs build number etc?

Phill Pafford August 3, 2023

yep current job

Like Adrian Raszkiewicz likes this
Suguna Tejaswini Nandakumara August 23, 2023

 I am trying to use the same, but in powershell, but this variable doesn't return any value. I am also using it in the Final Task as mentioned in the above note. 

I mean it returns a null value.

Phill Pafford August 24, 2023

I have not used PowerShell but a quick google looks like there is a Syntax change 

BUILD_STATUS=$Env:bamboo_jobFailed
Like Steffen Opel _Utoolity_ likes this
Suguna Tejaswini Nandakumara August 24, 2023

Now it is working. Thank you so much.

Suguna Tejaswini Nandakumara August 28, 2023

I am trying to get the status of each step or task available in the build plan. Will that be possible? (I tried the $lastexitcode, but it won't work)

Phill Pafford August 28, 2023

Bamboo already does this, kinda. You can assume the task was successful if bamboo continues the process, else if it fails, bamboo will log the issue and from their you can determine.

 

But here is an idea, not tested

 

#!/usr/bin/env bash

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

set -e

TASK_STATUS="STARTED"
echo "TASK_STATUS: $TASK_STATUS"

if [task executes successfully]
then
...
TASK_STATUS="SUCCESS"
echo "TASK_STATUS: $TASK_STATUS"
else
TASK_STATUS="FAILED
echo "TASK_STATUS: $TASK_STATUS"
fi




Suguna Tejaswini Nandakumara September 6, 2023

Yes, its true. But I am trying to check the status of a previous task in a script task, like if the previous task passed or was it disabled. Is that possible?

Phill Pafford September 6, 2023

not to my knowledge, you could try to use inject variables https://confluence.atlassian.com/bamboo/configuring-a-variables-task-687213475.html

example:

  • create an inject variable for each task
  • set the initial key=value for each inject variable to a failed status
  • in the task, after successful steps, update the inject variable to successful status, so any tasks that fail do not get the initial failed status overwritten and could later be used as a potential task status

NOTE: this is not 100%, just trying to think outside the box 

Suguna Tejaswini Nandakumara September 6, 2023

Got it, will look into it, Thank you,

0 votes
Alexey Chystoprudov
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 18, 2019

I would suggest to install this plugin: https://github.com/HackAttack/bamboo-github-status

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events