How to get build status in a Bamboo build task script?
Asked by H?l?ne_Droal:

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


Answers:
author: H?l?ne_Droal
body: Okay so I found the perfect variable (undocumented) : \`bamboo_jobFailed\` :)

comments: author: Bharath_Venkat_Rajan
body: Exactly what I wanted. Thanks [@H?l?ne_Droal](/t5/user/viewprofilepage/user-id/2722648)


author: Elizabeth Goldstein
body: Thank you [@H?l?ne_Droal](/t5/user/viewprofilepage/user-id/2722648) this is going to save me a headache with the API!


author: Vigneshwaran UK
body: 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


author: PatrickS
body: 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"
```


author: Suguna Tejaswini Nandakumara
body: This is not working anymore -

```
${bamboo_jobFailed}?
```

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


author: Phill Pafford
body: 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 ===================
```

comments: author: Adrian Raszkiewicz
body: 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?


author: Phill Pafford
body: 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)


author: Adrian Raszkiewicz
body: 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?


author: Phill Pafford
body: yep current job


author: Suguna Tejaswini Nandakumara
body: 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.


author: Phill Pafford
body: I have not used PowerShell but a quick google looks like there is a Syntax change

    BUILD_STATUS=$Env:bamboo_jobFailed


author: Suguna Tejaswini Nandakumara
body: Now it is working. Thank you so much.


author: Suguna Tejaswini Nandakumara
body: 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)


author: Phill Pafford
body: 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




```


author: Suguna Tejaswini Nandakumara
body: 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?


author: Phill Pafford
body: 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


author: Suguna Tejaswini Nandakumara
body: Got it, will look into it, Thank you,


author: Alexey Chystoprudov
body: I would suggest to install this plugin: <https://github.com/HackAttack/bamboo-github-status>


Tags: github