Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 21:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

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

In Bitbucket Pipelines, is it possible to use a variable from within another variable?

Anthony -Shadow- Elmore September 11, 2019

I am trying to set up bitbucket pipelines and one thing I want to do is be able to create variables from values of other variables.

I've tried several different ways of specifying syntax, and the repo level variable renders whatever the value is at a literal string and doesn't seem support variable expansion.  The documentation doesn't mention this at all.

As an example, I am setting up account level variables for the default ports our microservices are  running on.  This allows us to centralize where we define these and reference from so values aren't duplicated across different repos.

So, at the account level, I have values like:
SERVICE_1_PORT=8080
SERVICE_2_PORT=8081
SERVICE_3_PORT=8082

I'd like to have a generic template for a bitbucket pipelines file that can be reused across different repos so, for instance, in the SERVICE_1 repo, I can have a repo variable like:
THIS_SERVICE_PORT=$SERVICE_1_PORT

and then, for SERVICE_2, the repo level variable would become
THIS_SERVICE_PORT=$SERVICE_2_PORT

That way, they both could use the same pipelines template that simply declares something like

- export SERVICE_PORT=$THIS_SERVICE_PORT


Instead of having two *very similar, yet different* pipeline files that only differ in that one has

- export SERVICE_PORT=$SERVICE_1_PORT


and the other has

- export SERVICE_PORT=$SERVICE_2_PORT

and so on...

 

Another thing I've done in the past with other CI/CD systems is have a variable defined as something like:


CREDENTIAL_STRING=${username}:${password}@${server}


where the ${username}, ${password} , and ${server} values can be scoped per environment or project groups, or resources, or whatever.

1 answer

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

Hi @Anthony -Shadow- Elmore

The issue you are facing happens due to the fact that account variables are not evaluated when a repository variable is declared mentioning the first one.

I've opened an improvement request to cover this scenario:

The issue can be temporarily avoided by using the eval command in your pipelines file.

The variable declaration would be something like:

  • Account variable
    SERVICE_1_PORT=8080
  • Repository variable
    THIS_SERVICE_PORT=$SERVICE_1_PORT
  • Pipelines declaration
    - eval export SERVICE_PORT=$THIS_SERVICE_PORT

When testing this I also noticed that the account variables were not suggested for autocompletion. I've opened another improvement suggestion to expand the autocompletion to those variables as well:

I hope that helps you to move forward.

Anthony -Shadow- Elmore September 16, 2019

Awesome, thank you!

I have another workaround I came up with using bash parameter expansion, only because I didn't want to use eval, but it's not ideal for a long term solution.

I'll keep an eye on that ticket.

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

That is great!
I'm glad you already found a way to move on.

Please vote on that ticket to increase the visibility of it among our developers.
Have a good one! =]

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Upcoming Bitbucket Events