Replace single step in bitbucket pipeline

Viewed 148

I am trying to build rather complex Bitbucket Pipeline while following dry principles.

So far I defined multiple custom steps in definitions section:

definitions:
  steps:
    - step: &foo
        name: foo
        script:
          ...
    - step: &bar
        name: bar
        script:
          ...

Now, I have two custom pipelines that are almost identical except for one artifact/variable.

pipelines:
    custom:
      action-a:
        - step:
           name: action a
           script:
             - echo "1" > .value
           artifacts:
             - .value
        - step: *bar
        - step: *foo
      action-b:
        - step:
           name: action b
           script:
             - echo "2" > .value
           artifacts:
             - .value
        - step: *bar
        - step: *foo

Everything after step generating .value artifact is identical (and way more complex than just 2 steps). Would it be possible to define a single pipeline and somehow inject this artifact with anchors?

1 Answers

With bitbucket pipelines, you can now use Deployment Variables.

These are found at Repository Settings > Deployments

You can create a step

- step: &foo
    name: Deployment Step
    script: 
      - echo ${DEPLOYMENT_VAR}

Then in your pipelines definition, call that step and pass in your deployment name, here's an example using branch specific deployments.

pipelines: 
  branches:
    develop: 
      - step:
        <<: *foo
        name: 'foo (develop)'
        deployment: foo-dev
    main: 
      - step:
        <<: *foo
        name: 'foo (main)'
        deployment: foo-main

Then if your Deployment Settings look like this: enter image description here

When pushing to the develop branch, your pipeline step would echo dev and your main branch would echo main

see: https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/#:~:text=override%20team%20variables.-,Deployment%20variables,-You%20can%20also

Related