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

Is there an environment variable shared by all jobs of a given stage in Bamboo?

François Guillot September 10, 2019

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 ?

1 answer

0 votes
Daniel Santos
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 11, 2019

Hi @François Guillot,

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


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

François Guillot September 12, 2019

@Daniel Santos 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

François Guillot September 12, 2019

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

François Guillot September 12, 2019

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.

Daniel Santos
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 13, 2019

Hi @François Guillot

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.

François Guillot September 16, 2019

Hi @Daniel Santos 

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 ?

Daniel Santos
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 16, 2019

Hi @François Guillot

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

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.

François Guillot September 16, 2019

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

Daniel Santos
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 16, 2019

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.

François Guillot September 16, 2019

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

 

Thanks @Daniel Santos 

Daniel Santos
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 17, 2019

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.

=]

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events