Bamboo REST API: how to trigger a plan and waiting for a result
Asked by Ilan Hazan:

Hello,  
I would like to write a bash script to use the Bamboo REST API in order to trigger a plan tests and would like to wait for it to finish and get a result (failed or success).  
Upon success, I would like to do something in the pipeline.  

I found the following commands:  
#This triggers the plan   
curl -k --user user:password -X POST -d "stage\&executeAllStages" <https://bamboo.myserver.com/rest/api/latest/queue/XXX-YYY>  

I can parse the result of that command in order to get the build number by:  
sed 's/.\*buildNumber\\="\\(\[0-9\]\*\\)".\*/\\1/'  

I can use the extracted build number in order to query the result:  
curl -k --user user:password -X GET <https://bamboo.myserver.com/rest/api/latest/result/XXX-YYY/>*build-number*  

<br />

And then i need to parse result and do a busy wait to ask again and parse until there is an answer.  

I am sure this is bad practice and would like to ask if there is one command (REST API) that do that for me. Just not returning until there is an answer.  

Many thanks in advanced


Answers:
author: Ilan Hazan
body: I implemented the algorithm.

I improved the bash's sed regular expression and did a loop with some sleeps between the Bamboo API calls:  
[Bamboo REST API: how to trigger a plan and waiting for a result](http://www.rndblog.com/bamboo-rest-api-how-to-trigger-a-plan-and-waiting-for-a-result/ "trigger a plan and waiting for a result")

comments: author: Daniel Santos
body: That is great [@Ilan Hazan](/t5/user/viewprofilepage/user-id/1678258), thank you for taking the time to share your solution here =\]


author: Michael Fellinger
body: The provided link is dead. Can you post it again, please?


author: Damon Stamper
body: [@Michael Fellinger](/t5/user/viewprofilepage/user-id/4939058) <https://web.archive.org/web/20220516032354/http://www.rndblog.com/bamboo-rest-api-how-to-trigger-a-plan-and-waiting-for-a-result/>


author: Suguna Tejaswini Nandakumara
body: Hi,

I am trying to trigger the bamboo build on a bamboo plan with a given branch/commit using rest api, using powershell scripting (probably using Invoke-RestMethod). any example or reference for this?


author: Daniel Santos
body: Hi [@Ilan Hazan](/t5/user/viewprofilepage/user-id/1678258)

I don't see a better option to get the information you want other than using REST API and asynchronously checking if the build finished.  

I have a suggestion for small improvements in your approach.

* Try to use [jq (JSON processor)](https://stedolan.github.io/jq/) to filter the request responses. There is one example you can see jq being used: [Solved: How to get deploymentResultId using REST API?](https://community.atlassian.com/t5/Bamboo-questions/How-to-get-deploymentResultId-using-REST-API/qaq-p/1097153?utm_source=atlcomm&utm_medium=email&utm_campaign=answer_accepted_as_solution&utm_content=topic)
* Try to use \<Bamboo_URL\>/rest/api/latest/result/status/XXX-YYY-# (to check the build status) before calling the result itself. It should be a call with a lower cost for Bamboo and will give you the current status of the build.
* Remember to implement a timeout in case the bamboo build has a routine that freezes it.

I guess those are pieces of advice can give you.  

comments: author: Ilan Hazan
body: [@Daniel Santos](/t5/user/viewprofilepage/user-id/502125) Many thanks


Tags: developer

rest-api