pipeline: how to build for multiple environments?
Asked by Admin Admin:

hello,

I understand that "deployment" allows the env specific variable values.

But what if I need to have an env specific variable value ++during++ the build?

e,g in "test", i need PORT=1000, but in "dev", PORT=2000.

I currently solved this by adding env specific vars, e.g PORT=$TEST_PORT, but the code is obviously will be quickly unmanageble


Answers:
author: Patrik S
body: Hello [@Admin Admin](/t5/user/viewprofilepage/user-id/4720087) ,

In order to have different values for the same variable name, you can make use of the **deployment variables**.

You can create/manage deployment variables going to **Repository settings** \>**Pipelines** \>**Deployments**.

**Note:**Deployment variables override both workspace and repository variables, and are unique to each environment.

In the example you provided, where you have the environments **dev** and **test**, once you create those environments in the repository setting, you can configure a variable named PORT in each of them, but having different values.

Then, on your YML file, you would have to separate the scripts for each environment in two different build steps, and assign the corresponding deployment environment to each step as the below example :

```
- step:
    name: My TEST env step
    deployment: test
    script:
      - echo $PORT # print value of the variable PORT configured for TEST deployment environment
- step: 
    name: My DEV env step
    deployment: dev
    script:
      - echo $PORT # print value of the variable PORT configured for DEV deployment environment

```

However, if your intention was to use just one step, and have different values for the same variable name, I'm afraid this is not currently possible, and in this case you would indeed have to use two variables(one for each environment) like $TEST_PORT and $DEV_PORT.

Hope that helps to address your questions.

Thanks, [@Admin Admin](/t5/user/viewprofilepage/user-id/4720087)

Patrik S

comments: author: andrew zyman
body: Pateick,

Thank you.

The thinking is to have a general build step, that takes different values based On the branch type.

So that i dont have to Repeat the build code in several places.


author: Patrik S
body: Hello [@andrew zyman](/t5/user/viewprofilepage/user-id/4723788) ,

In case you are using the **branch** kind of pipelines, one option would be to write a script to set the environment variables at build time based on the name of the Branch who triggered the pipeline.  

The script below makes usage of the $BITBUCKET_BRANCH default environment variable to check what is the name of the branch that triggered the build, and setup a PORT environment variable if the name matches one of the patterns dev.\* or test.\* .

```
if [ -z "$BITBUCKET_BRANCH" ]
then
 echo "This pipeline is not using branches"
 exit
fi
echo "Executing pipeline for branch: $BITBUCKET_BRANCH"

if echo "$BITBUCKET_BRANCH" | grep -q "dev.*"; then
  echo "Dev branch"
  export PORT=$PORT_DEV
elif echo "$BITBUCKET_BRANCH" | grep -q "test.*"; then
  echo "Test branch"
  export PORT=$PORT_TEST
else
  echo "No matching option for branch name $BITBUCKET_BRANCH"
fi
```

The script above is creating an generic env variable called PORT with the content of a repository variable , in this case either $PORT_DEV or $PORT_TEST , so your further steps in the build can always use $PORT.

Then you could run the script on your YML file like below:  

```
pipelines:
 branches :
  '**' :
    - step:
      script:
       - chmod 700 env_script.sh #grant execute permissions to the script
       - source ./env_script.sh #run the script to set env variables
       - echo $PORT #print the env variable
```

Thanks, [@andrew zyman](/t5/user/viewprofilepage/user-id/4723788) .  
Patrik S.


author: Admin Admin
body: [@Patrik S](/t5/user/viewprofilepage/user-id/4603309)

thank you. A few clarifications:

Is it correct to understand that "\*\*" == "default" in branch-based pipelines?


author: Patrik S
body: Hello [@Admin Admin](/t5/user/viewprofilepage/user-id/4720087)

The "default" keyword is also considered a branch-based trigger, so your assumption is correct.

'\*\*' will match all branches.

For more details about pipeline triggers you can refer to the following documentation:

* <https://support.atlassian.com/bitbucket-cloud/docs/pipeline-triggers/>


Tags: bitbucket-cloud

build

cloud