Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Using deployment variables in other steps

Neil Nand October 15, 2019

Hello,

I've split my deployment into two steps, one step for building + deploying code and several other steps, which run in parallel, to run some commands needed for deployments against different databases.

What I'd like to do is set some environmental variables at the deployment level and use them as environmental variables in steps that are not part of the deployment as we unfortunately cannot have multiple steps in a single deployment. How would I go about this?

Tl;Dr
I'd like to use environmental variables set at a deployment level in steps not part of that deployment.

1 answer

1 accepted

1 vote
Answer accepted
Daniel Santos
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
October 16, 2019

Hi @Neil Nand

I'm not sure if I really got what is your intent with this configuration.

  • Can you share a little bit more on what problem you are trying to solve?
  • How the steps that would consume the deployment variables would be triggered?
  • Why repository variables are not an option for you?

Sorry for the questions, I just want to understand better your scenario to see if I can come with an option for you to move forward.

Neil Nand October 20, 2019

Hello @Daniel Santos and thanks for the reply.

So the problems I'm trying to solve are:

* Current I'm using repository variables and an export command like the following but as these environmental variables are deployment environment specific it feel like it should be kept there and so I don't have such a large list of environmental variables that are both generic and for each deployment environment.

export DATABASE_HOST=$TEST_DATABASE_HOST

 * That export code above is needed in many steps which is making the bitbucket-pipelines.yml file quite long and I'm looking for ways to trim it down.

* Currently these variables are triggered automatically as part of a branch pipeline. In this pipeline unit tests are run, then code deployed to server then the parallel steps to run commands are run on that environments DB and this is where I'm looking to use the deployment environmental variables.

Currently the setup I've got using the repository environmental variables works but I'm looking for ways to tidy this all up a little.

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

Hi @Neil Nand

Thank you for sharing! Now I have a better perspective of what you are trying to achieve.

Variables from deployments are limited to their step as you already noticed, but there is a way to transfer them via artifacts.

In the following example, the variables VAR1 and VAR2 are declared in the deployment section:

pipelines:
default:
- step:
script:
- echo "I'm building"
- step:
name: Deployment step
deployment: ci-test
script:
- echo "export VAR1=\"$VAR1\"" >> variables
- echo "export VAR2=\"$VAR2\"" >> variables
artifacts:
- variables
- parallel:
- step:
script:
- . variables
- echo "$VAR1"
- step:
script:
- . variables
- echo "$VAR2"

I'm not sure if it helps a lot, but at least you don't need to declare them again on each parallel step, just load. 

There is a feature request to add environment variables to bitbucket-pipelines.yml. If you think this would be a good thing in your case, please add yourself as a watcher and vote on this one.

I hope that helps. 

Like Neil_Nand likes this
Neil Nand November 6, 2019

Thanks for the response @Daniel Santos and sorry it has taken me so long to get back to you your suggestion has worked.

Like Daniel Santos likes this
rafael.moraes February 19, 2020

This workaround seems a little bit messy to me. Is there any good reason why two steps could not share same deployment configuration?

Neil_Nand
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 19, 2020

@rafael.moraes as far as I'm aware it's purely because Bitbucket pipelines doesn't support it.

Like Daniel Santos likes this
rafael.moraes February 19, 2020

@Neil Nand  thanks, would be great as a new feature and it doesn't seems too complex to implement that. Makes sense to reuse variables in multiple steps.

Like # people like this
Daniel Santos
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 20, 2020

Hey @rafael.moraes,
yes, it would be great if we have this feature.
We have a feature request that should cover this need. The one shared in my comment above.

Thank you @Neil Nand for helping here =]

Nikolaj Dam Larsen
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 16, 2020

Yeah I have a similar issue. I've separated the build and deployment steps, but the build step is depending on the environment variables since these variables are compiled into the build.

I could have both steps be deployment steps, but that just messes up the Deployments page, since it would include both the build and the actual deployment step. Alternatively I could do as suggested, and make the build step the "deployment" step and pass on the variables as an artifact, but then I can't Redeploy the actual deployment step. The last option is to just merge the two steps together as one, but this just messes up the build script, since I still need the option to run the build step independently on each pull request.

There's basically no good option available, which is unfortunate, because I reckon it wouldn't be too much of a hassle to add something like this:

step:
name: Build
environment: Staging

step:
name: Deployment
deployment: Staging


So the build step would use the deployment environment, without being an actual deployment to that environment. 

Like Eric Broyles likes this
Eric Broyles
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 28, 2022

@Daniel Santos It would be excellent to have the capability that @Nikolaj Dam Larsen  mentioned: the "environment" keyword which would have values that match the available deployment environments but would not be a deployment step.

Currently we have to export variables from the deployment step into a shell file that is passed as an artifact to other steps, but as Nikolaj aptly pointed out, that is messy and has limitations (e.g. redeployment basically does nothing). The suggestion of adding the environment keyword is elegant.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events