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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Using deployment variables in other steps


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?

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

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.

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.


 * 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.

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:

- step:
- echo "I'm building"
- step:
name: Deployment step
deployment: ci-test
- echo "export VAR1=\"$VAR1\"" >> variables
- echo "export VAR2=\"$VAR2\"" >> variables
- variables
- parallel:
- step:
- . variables
- echo "$VAR1"
- step:
- . 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

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

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

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

Like Daniel Santos likes this

@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

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 =]

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:

name: Build
environment: Staging

name: Deployment
deployment: Staging

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Bitbucket Pipelines

Bitbucket Pipelines Runners is now in open beta

We are excited to announce the open beta program for self-hosted runners. Bitbucket Pipelines Runners is available to everyone. Please try it and let us know your feedback. If you have any issue...

3,767 views 64 20
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you