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

[Bitbucket Pipeline] Using a different environmental variable based on branch? Edited

Alex Wong Mar 03, 2019

If I push to `branch-A`, I want to run tests using `branch-A` envs.

If I push to `branch-B`, I want to run tests using `branch-B` envs.

 

How can I do that without creating new `steps` just to change the environmental variable?

2 answers

1 accepted

0 votes
Answer accepted
Philip Hodder Atlassian Team Mar 04, 2019

@Alex Wong, if you only need the variable variation for you deployments, you should be able to use Deployment Variables to do that. They'll let you define different values for the same key on different deployment steps.

We have documentation about it here: https://confluence.atlassian.com/bitbucket/variables-in-pipelines-794502608.html

An example bitbucket-pipelines.yml would be:

pipelines:
branches:
develop:
- step:
name: Deploy to Test
deployment: test
script:
- echo $MY_ENV # Would print www.mysite.com/develop
staging:
- step:
name: Deploy to Staging
deployment: staging
script:
- echo $MY_ENV # Would print www.mysite.com/staging
production:
- step:
name: Deploy to Production
deployment: production
script:
- echo $MY_ENV # Would print www.mysite.com/production

The variables are configured in the Repository Settings -> Pipelines -> Deployments section, per environment.

Currently Pipelines only supports those 3 deployment environments. However, we are currently working on extending this to more environments. You can follow the progress of this feature here: https://bitbucket.org/site/master/issues/15362/enhance-deployments-to-support-flexible

Thanks,

Phil

Alex Wong Mar 04, 2019

Thank you Phil, this is exactly what I was looking for.

 

Currently Pipelines only supports those 3 deployment environments. However, we are currently working on extending this to more environments.

I am looking forward to this because I have more than 3 deployment environments. I have watched it.

 

Thanks again!

0 votes
Martyn Winsen Atlassian Team Mar 04, 2019

Hi Alex,

I am not on the Bitbucket Team, but I will try to help.

I know you'd like to avoid creating additional steps, but have you tried using mutliple, smaller steps so that you can re-use all of the parts that are common?

https://bitbucket.org/site/master/issues/12750/allow-multiple-steps

If this doesn't meet your needs, feel free to create an issue in the public issue tracker for Bitbucket: https://bitbucket.org/site/master/issues?status=new&status=open&component=Pipelines 

I know that the team listen to Customer feedback to improve our products.

I hope that helps!

Alex Wong Mar 04, 2019

Hi Martyn,

Thank you for the response. I tried dividing it into smaller steps as you suggested.

 

Test

Prerequisite

- Repository Env already has `MY_ENV=hello`

 

branch A

- step1: MY_ENV=world

- step2: echo $MY_ENV (result: `hello`)

 

Conclusion

Basically on a new step, whatever ENV I set in the previous step gets reset. I will create an issue in the public issue tracker.

Like Martyn Winsen likes this
Martyn Winsen Atlassian Team Mar 04, 2019

Hi Alex,

have you considered something like this...

 

Prerequsite

- Repository ENV already has `MY_ENV_A=hello' and 'MY_ENV_B=world`

 

branch A

- common steps

- unique step: echo $MY_ENV_A

- common steps

 

branch B

- common steps

- unique step: echo $MY_ENV_B

- common steps

Alex Wong Mar 04, 2019 • edited

Hi Martyn

 

Sorry not being clear. 

My env is like the following

KEY: MY_ENV

VALUE: 'www.mysite.com/staging'

 

Depending on the branch I want to replace the key `MY_ENV` with a different value, not a different key.

If I deploy to `develop`, `MY_ENV` should be `www.mysite.com/develop`.

If I deploy to `staging`, `MY_ENV` should be `www.mysite.com/staging`

The `MY_ENV` is a process env inside my server files.

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Bitbucket Pipelines

Building a Bitbucket Pipe as a casual coder

...ipe.sh :  #!/bin/bash source "$(dirname "$0")/common.sh" enable_debug extra_args="" if [[ "${DEBUG}" == "true" ]]; then extra_args="--verbose" fi # mandatory variables R...

1,814 views 1 19
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