Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Help with YAML anchors for parallel steps in bitbucket pipeline

Manish Kumar September 10, 2019

Hello,

Could someone please guide me on how to use YAML anchors for parallel steps to re-use sections of pipelines in branches?

I have provided a sample bitbucket-pipelines.yml below based on my scenario. As you can see, I have to repeat same steps for feature & master branches. 

I know how to write the anchors for nromal sequential steps and I had initially referred to https://bitbucket.org/sonarsource/sample-nodejs-project/src/master/bitbucket-pipelines.yml for YAML anchors but don't know how to implement it for parallel steps.

Any help will be much appreciated. Thanks.

Sample bitbucket pipeline yml file - 

I have tried formatting the yml file. 


image: node:10.14

clone:
depth: full

definitions:
services:
docker:
memory: 2048
caches:
sonar: ~/.sonar/cache

pipelines:
branches:
feature-abc:
- step:
caches:
- node
- sonar
script:
- cd demo-proj
- npm install
artifacts:
- demo-proj/node_modules/**
- parallel:
- step:
name: Execute unit tests for xyz
script:
- cd demo-proj
- npm run test "xyz-directory" --codeCoverage=true --progress=true --watch=false
artifacts:
- demo-proj/coverage/**
- step:
name: Execute unit tests for abc
script:
- cd demo-proj
- npm run test "abc-directory" --codeCoverage=true --progress=true --watch=false
artifacts:
- demo-proj/coverage/**
- step:
name: Run SonarCloud Analysis
script:
- cd demo-proj
- pipe: sonarsource/sonarcloud-scan:0.1.5
variables:
SONAR_TOKEN: ${SONAR_TOKEN}
EXTRA_ARGS: '-Dsonar.sources="src"'
master:
- step:
caches:
- node
- sonar
script:
- cd demo-proj
- npm install
artifacts:
- demo-proj/node_modules/**
- parallel:
- step:
name: Execute unit tests for xyz
script:
- cd demo-proj
- npm run test "xyz-directory" --codeCoverage=true --progress=true --watch=false
artifacts:
- demo-proj/coverage/**
- step:
name: Execute unit tests for abc
script:
- cd demo-proj
- npm run test "abc-directory" --codeCoverage=true --progress=true --watch=false
artifacts:
- demo-proj/coverage/**
- step:
name: Run SonarCloud Analysis
script:
- cd demo-proj
- pipe: sonarsource/sonarcloud-scan:0.1.5
variables:
SONAR_TOKEN: ${SONAR_TOKEN}
EXTRA_ARGS: '-Dsonar.sources="src"'

 

1 answer

1 accepted

2 votes
Answer accepted
Daniel Santos
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 11, 2019

Hi @Manish Kumar,

I built the following example that should speak by itself:

definitions:
  step_def1: &myCommonStep1
    name: Step1 name
    script:
      - echo "command 1"
      - echo "command 2"

 
step_def2: &myCommonStep2
    name: Step2 name
    script:
      - echo "command 3"
      - echo "command 4"


pipelines:
  default:
    - parallel:
        - step: *myCommonStep1
        - step:
            <<: *myCommonStep2
            name: Step renamed

    - step: *myCommonStep2

(i) The step renamed has only the intention to show a different way to declare the step inside the parallel section.

Please let me know if you face any issues to reproduce this configuration.

Manish Kumar September 11, 2019

Thanks @Daniel Santos .

Like Daniel Santos likes this
Daniel Santos
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 12, 2019

You are welcome @Manish Kumar!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events