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

Is there a way to have local variables for bitbucket pipelines within a step?

Paul Helter July 14, 2022

I'm wondering if there is a way to do:

```yaml

definitions:
  scripts:
    - script: &echo_script
        - echo ${MESSAGE}
pipelines:
  default:
    - step: &my_step1
          name: Test1
          local_variables:
            - MESSAGE="I want my bitbucket-pipelines.yaml to Don't-Repeat-Yourself (DRY)"
          script:
             <<: *echo_script
    - step: &my_step2
          name: Test2
          local_variables:
            - MESSAGE="This would allow me to do this"
          script:
             <<: *echo_script
```

Expected output for a run is:
```
Test1 : "I want my bitbucket-pipelines.yaml to Don't-Repeat-Yourself (DRY)"
Test2 : "This would allow me to do this"
```
Thanks

1 answer

0 votes
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 19, 2022

Hello @Paul Helter ,

Thank you for reaching out to Atlassian Community!

I'm afraid that at this moment it's not possible to define environment variables at step level. We already have a feature request to implement that functionality, though : 

I would suggest you to add your vote there, since this helps both developers and product managers to understand the interest. Also, make sure you add yourself as a watcher in case you want to receive first-hand updates from that ticket. Please note that all features are implemented with this policy in mind.

While this feature is not implemented, a workaround-ishy way of having different values for the same variable is to define different deployment environments , create the deployment variables in each deployment environment, and then assign a different deployment to each step, like the below example : 

pipelines:

  default:

    - step:

          name: Deploy to Test

          deployment: env1

          script:

             - echo $DEPLOYMENT_VARIABLE

    - step:

          name: Deploy to Test

          deployment: env2

          script:

             - echo $DEPLOYMENT_VARIABLE

So, although the environment variable name is the same, you can set different values to it for each deployment environment.

Hope that helps! Let me know if you have any questions.

Thank you, @Paul Helter .

Kind regards,

Patrik S

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events