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

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

alexwonguhuru March 3, 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?

3 answers

1 accepted

0 votes
Answer accepted
Philip Hodder
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 4, 2019

@alexwonguhuru, 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

alexwonguhuru March 4, 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!

10 votes
Matt Robinson October 21, 2020

I would like to add my own take on this as a had to deal with a variation of this. I needed to have environment variables for branches which deployments doesn't quite cover in that you can only set a deployment target for a single step. 

I realise this isn't exactly what you are after, but I would like to add it for others who may end here, but this was my solution

definitions:
  steps:
    - step&set-environment
        nameSet Environment
        artifacts:
          - environment.sh

    - step&build
        nameBuild
        script:
          - source environment.sh
          - build.py $ENVIRONMENT
              - step&test
        nameTest
        script:
          - source environment.sh
          - test.py $ENVIRONMENT

    - step&deploy
        nameDeploy
        script:
          - source environment.sh
          - deploy.py $ENVIRONMENT

pipelines:
  development:
    - step:
 
        <<*set-environment
        script:
          - echo export ENVIRONMENT=development >> environment.sh
    - step*build
    - step*test
    - step:
        <<*deploy
        deploymentDevelopment

  staging:
    - step:
 
        <<*set-environment
        script:
          - echo export ENVIRONMENT=staging >> environment.sh
    - step*build
    - step*test
    - step:
        <<*deploy
        deploymentStaging

  production:
    - step:
 
        <<*set-environment
        script:
          - echo export ENVIRONMENT=staging >> environment.sh
    - step*build
    - step*test
    - step:
        <<*deploy
        deploymentProduction
Nathan Thomas November 16, 2020

This is a great workaround. I'd just gotten to this blocker with deployment when I came across this!

Like Isaac Ose likes this
stefan.cross January 13, 2021

A good idea, but be careful if thinking of using with credentials as will leave them hanging around as an artifact... So best not used for sensitive details 

Like # people like this
0 votes
Marty
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 4, 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!

alexwonguhuru March 4, 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 # people like this
Marty
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 4, 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

alexwonguhuru March 4, 2019

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.

Like Nathan Thomas likes this
rafael.santos August 3, 2020

you can use Deployment variables. 

Enter in repository settings >  deployments and enter your variable.

But, this function just works when you set:

 - step: 
deployment: Test # deployment tag
name: deploy on test env
script:
- echo $ENV

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events