Parametrized anchors in pipelines
Asked by Arturs Sosins:

I could reuse lots of pipeline code if I could parametrize it even if through ENV variable.

So I could define an anchor like

```
definitions:
  steps: 
    - step: &Test
      name: Test
      script: echo $MySetting
```

And then I could reuse it with my setting

```
pipelines:
  branches:
    master:
      - parallel:
        - step:
          <<: *Test
          before-script: MySetting=Test1
        - step:
          <<: *Test
          before-script: MySetting=Test2
```

Is something like this available?


Answers:
author: Halyna Berezovska
body: [@Arturs Sosins](/t5/user/viewprofilepage/user-id/2263488) hello! Look at one of the examples how you can define your steps [https://bitbucket.org/atlassian/aws-elasticbeanstalk-deploy/src/0545b1d8703a8ebe471cc3bf772cd56796f0e91f/bitbucket-pipelines.yml#lines-102](http://Bitbucket%20Pipelines steps defining) .

With definitions keyword usually services are defined, I don't find official doc how you can define steps like this and sometimes it might fail for you.

Use the structure as you can see in the example above.

Regards, Galyna

comments: author: Arturs Sosins
body: Hello,  
so I think this way you can define multiple steps per branch  
but it would not allow me to parametrize the same step in different branches, would it?


author: Halyna Berezovska
body: [@Arturs Sosins](/t5/user/viewprofilepage/user-id/2263488) yes, you can define those steps you define before in any branch following guide for pipeline configuration. You could run also as many time as you need your step , referring like this

```
branches:
  master:
  - <<: *test1
  - <<: *test2
  - <<: *test1
  qa-*:
  - <<: *test-integration
  - <<: *test1
```

You can have different workflows.


author: Arturs Sosins
body: Sorry, still don't get it  
It is not about the branches, but about the ability to change the behavior of step based on outside variable  
So for example I want test1 step to behave a bit differently on the master branch than qa branch.  
I know I can use the branch variable for this case  
But what if I want to do the same on the manual custom pipeline, there is no way to define variable outside of step, is it?


author: Halyna Berezovska
body: [@Arturs Sosins](/t5/user/viewprofilepage/user-id/2263488) I've got your point. I think we do not have such ability right now, if you want test to execute different workflows.

Such tests could be executed in different ways on different branches only if you, for example create several deployments and deployment variables and use different deployments for master and qa.

But if it goes deeper to logic of test workflow and cannot be customized just via deployment variables, it will not work.

For example let your test depends on deployment variable ENVIRONMENT and this variable is different in different deployment environments, for example, ENVIRONMENT can be 'production', 'qa'. And based on this decision your step is taking actions depending on this variable.

BUT if your test on one branch should execute unit tests, for example and on another branch do some integration tests and needs specific actions for that, I guess, you cannot unify such workflow for different branches.

Or do some thing like this:

if ENVIRONMENT = 'prod' then ...

if ENVIRONMENT = 'qa' then....

But this solution would be silly and it is better to create different steps.

Just keep in mind that you have only ability to have flexible step only due to different variables customized (deployment, repository variables). I believe this will help you to write a proper pipeline.


Tags: bitbucket-pipelines