Is there an environment variable shared by all jobs of a given stage in Bamboo?
Asked by Fran?ois Guillot:

Hi,

I'm using Bamboo environment variables to link various jobs together.

Given the following plan:

- Stage 1

- Job 1-1

- Job 1-2

- Stage 2

- Job 2-1

- Job 2-2

I can use \`bamboo_planName\` to get all the jobs for a given plan. This env var is available when the 4 \`Job 1-1\`, \`Job 1-2\`, \`Job 2-1\`, \`Job 2-2\` jobs execute.

I can also use \`bamboo_buildPlanName\`, to uniquely identify a given job across executions. I can use this to link together all executions of \`Job 1-1\` for instance.

Is there something similar for stages ? I'd like to be able to group via this mean all jobs for a given stage, say \`Stage 1\`. I would like to have an env var that is shared by \`Job 1-1\`, \`Job 1-2\` only, and \*NOT\* \`Job 2-1\`, \`Job 2-2\`.

Did I miss something ?


Answers:
author: Daniel Santos
body: Hi [@Fran?ois Guillot](/t5/user/viewprofilepage/user-id/3553316),

As far as I can tell we can't get that from variables.  
I have a different suggestion for you. Get the plan key (it could be from a variable) and use a REST API:

```
curl -u admin:admin -H 'Accept: application/json' \
     -X GET <BAMBOO-SERVER_URL>/rest/api/latest/search/jobs/<PLAN-KEY> \
     | jq -r '.searchResults[]|" JobKey: " + .searchEntity.key + "\tStageName: " + .searchEntity.stageName'

```

??Please notice that I used JQ - <https://stedolan.github.io/jq/>.  

Expected outcome:

```
JobKey: MISC-STAG-JOB1? ?StageName: Default Stage
JobKey: MISC-STAG-JOB2? ?StageName: Default Stage
JobKey: MISC-STAG-JOB3? ?StageName: stage2
JobKey: MISC-STAG-JOB4? ?StageName: stage2
```

<br />

The will help you to know from which stage your JOBs are.  
I hope that helps.

comments: author: Fran?ois Guillot
body: [@Daniel Santos](/t5/user/viewprofilepage/user-id/502125) Great idea

I can make use of that

I tried to look at the REST API documentation, but couldn't find the '/rest/api/latest/search/jobs/' one ?

Is it not public yet ?

I would like to refine the query to get only a given job.

Basically I know the plan and the job, and I want the stage this jobs belongs too


author: Fran?ois Guillot
body: Also, I tried

```
${bambooUrl}/rest/api/latest/search/jobs/${planKey}?searchTerm=${jobName}
```

but it's a 'startsWIth' search, meaning that if several jobs starts with the 'jobName', I get several results.

Could we get around that. Or find a way to pass the job key instead (which is unique) ? Passing the job key doesn't seem to work


author: Fran?ois Guillot
body: Nonetheless, I think it would be great if Bamboo was providing something (like an env var) in future versions, as calling the API on each build brings a little overhead that could easily be avoided.


author: Daniel Santos
body: Hi [@Fran?ois Guillot](/t5/user/viewprofilepage/user-id/3553316)  

In my tests, it worked with the full name and the job key. I know the search mechanism is *startWith* which some times will bring multiple results, but that is less likely to happen due to the fact that we are searching inside the same plan (restricted set of results).  

I've created this feature with the intention to simplify this scenario in future releases of Bamboo. Please vote and add yourself as a watcher to receive updates from our development team.


author: Fran?ois Guillot
body: Hi [@Daniel Santos](/t5/user/viewprofilepage/user-id/502125)

Can you point me to the issue I should upvote ?

Regarding the job key, did you put in the 'searchTerm' query parameter of the REST api call ?


author: Daniel Santos
body: Hi [@Fran?ois Guillot](/t5/user/viewprofilepage/user-id/3553316)  

It was my bad, like an e-mail without attachments...  
This is the feature request I've opened:

* [\[BAM-20620\] Add variable with stage information during build time.](https://jira.atlassian.com/browse/BAM-20620)

Yes, I used the *searchTerm*to add the job key. When I tested I used only the last part of the job key. Something like:

```
/rest/api/latest/search/jobs/PROJ-PLAN?searchTerm=JOB2
```

Assuming the full job key was something like PROJ-PLAN-JOB2.


author: Fran?ois Guillot
body: In my test, I have two jobs belonging to 2 stages of the same plan.

- one with

```
bamboo_buildKey=TESTGRADLE-TG-TES
```

- one with

```
bamboo_buildKey=TESTGRADLE-TG-TESG
```

If I call the API with

```
searchTerm = TES
```

I get the 2 jobs above. OK, both starts with 'TES'.

But if I call it with

```
TESTGRADLE-TG-TES
```

I get 0 results (weird, as the 2nd one starts with the same, isn't it ?)

I can't get what I'm doing wrong


author: Daniel Santos
body: I don't think you are doing anything wrong.   
The implementation is a *startwith* search as you already discovered.

The second format call with the full key is not covered by the search. It makes sense since the PLAN-KEY is already present in the REST API call, but it could work if designed for that.  

This is how I was able to avoid the duplicated names when referencing JOB with similar names.  

```
curl -u admin:admin -H 'Accept: application/json' \
     -X GET <BAMBOO-SERVER_URL>/rest/api/latest/search/jobs/<PLAN-KEY> \
     | jq -r '.searchResults[] | select(.searchEntity.key == "TESTGRADLE-TG-TES") | "Stage name: " + .searchEntity.stageName'
```

Please let me know if that is an option for you.


author: Fran?ois Guillot
body: > Please let me know if that is an option for you.

Unfortunately no, as the script I'm writing is not for me, and I don't have control over the keys consumers will be using. I'm going to wait [BAM-20620](https://jira.atlassian.com/browse/BAM-20620?_ga=2.130218447.1735828556.1568627390-713657340.1567151201)

Thanks [@Daniel Santos](/t5/user/viewprofilepage/user-id/502125)


author: Daniel Santos
body: Maybe you don't need to identify the stage and can come with a different approach to solve your issue. I don't know why you need the stage name. If you share the problem you are trying to solve we may find a different solution.  

If you think there is no other solution, then the only option is to wait for [BAM-20620](https://jira.atlassian.com/browse/BAM-20620?_ga=2.128603852.1577641105.1568632639-1655730220.1552917293).

=\]