Validating a bitbucket pipeline.
Asked by Naveen Gabriel:

```
    

    - step: &deploy

        name: Deploy to $DEPLOYMENT_ENV server

        clone:

          enabled: false

        script:

          - pipe: atlassian/ssh-run:0.6.0

            variables:

              SSH_USER: $DEPLOYMENT_SSH_USER

              SERVER: $DEPLOYMENT_SERVER_ADDR

              COMMAND: >

              #script here

 

pipelines:

  branches:

    development:

      - step: *deploy

        deployment: dev

        variables:

          DEPLOYMENT_ENV: "dev"

          DEPLOYMENT_SSH_USER: $DEV_SSH_USER

          DEPLOYMENT_SERVER_ADDR: $DEV_SERVER_ADDR

    'test/*':

      - step: *deploy

        deployment: staging

        trigger: manual

        variables:

          DEPLOYMENT_ENV: "staging"

          DEPLOYMENT_SSH_USER: $STAGING_SSH_USER

          DEPLOYMENT_SERVER_ADDR: $STAGING_SERVER_ADDR
```

<br />

<br />

Is this the correct usage of variables ?   
I have set up the variables in repository variables and in environment.   

My intention is to use same deploy step with different set of variables for different environment.  


Answers:
author: Patrik S
body: Hello [@Naveen Gabriel](/t5/user/viewprofilepage/user-id/5517480) and welcome to the Community!

The YAML syntax you have used is slightly wrong. Variables can only be used in pipes and in the script section of the step. It's not possible to use variables as part of the step definition, neither setting the variable value at the YAML file directly.

To create and set values to variables, you need to do it from the workspace, repository or deployment settings. Then those variables will be available in the script section of your step :

```
pipelines:
  default:
    - step:
          script:
            - <script>
    - step:
          name: Deploy to Test
          deployment: Test
          script:
             - echo $DEPLOYMENT_VARIABLE
```

For more details and example, the [Pipelines - Variables and secrets](https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/) documentation might be of help.

Thank you, [@Naveen Gabriel](/t5/user/viewprofilepage/user-id/5517480) !

Patrik S

comments: author: Naveen Gabriel
body: Hi [@Patrik S](/t5/user/viewprofilepage/user-id/4603309) thanks for responding.

But then would it mean I have to repeat deploy step for each environment?  
I want to have one script of deployment which should be common for each environment.   

<br />


author: Naveen Gabriel
body: I got a way around it. But still would like you to take a look at it and suggest if I can do anything better on this.

```
?
```

```
- step: &deploy

        name: Deploy to development server

        deployment : dev

        trigger: automatic

        clone:

          enabled: false

        script: &deploy-script

          - pipe: atlassian/ssh-run:0.6.0

            variables: &deploy-variables

              SSH_USER: $DEV_SSH_USER

              SERVER: $DEV_SERVER_ADDR

              COMMAND: >

               #script
```

```
 

branches:

    development:

      - step: *deploy

    test:

      - step: 

          <<: *deploy

          deployment: staging

          name: Deploy to staging server for testing

          script:

          - <<: *deploy-script

            variables:

              <<: *deploy-variables

              SSH_USER: $STAGING_SSH_USER

              SERVER: $STAGING_SERVER_ADDR

      

    main: 

      - step: *build-test-sonarcloud

      - step: *check-quality-gate-sonarcloud

        fail-fast: false

      

      - step:

          <<: *deploy

          deployment: staging

          trigger: manual

          name: Deploy to staging server for smoke test

          script:

            - <<: *deploy-script

              variables:

                <<: *deploy-variables

                SSH_USER: $STAGING_SSH_USER

                SERVER: $STAGING_SERVER_ADDR
```


Tags: bitbucket-cloud

bitbucket-pipelines

cloud

pipeline