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

Validating a bitbucket pipeline.

Naveen Gabriel August 21, 2024
    

    - 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


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. 
       

       

1 answer

0 votes
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 23, 2024

Hello @Naveen Gabriel 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 documentation might be of help.

Thank you, @Naveen Gabriel !

Patrik S

Naveen Gabriel August 25, 2024

Hi @Patrik S  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. 


Naveen Gabriel August 26, 2024

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
 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events