bitbucketcicdbitbucket-pipelines

bitbucket-pipeline and anchors - Steps are not executed


I have the following yaml file

# Define anchors for reuse
definitions:
  steps:
    - step: &setup_staging
        name: Setup staging deployment
        script:
          - echo "HelLo 1"

    - step: &terraform_plan
        name: Execute terraform init and plan
        script:
          - echo "HelLo 2"
    
infrastructure_plan_staging: &infrastructure_plan_staging
 - step: *setup_staging
 - step: *terraform_plan
 
pipelines:
  default:
    - <<: *infrastructure_plan_staging

Only the setup_staging step is executed, and I'm not sure why. I have also tried

infrastructure_plan_staging: &infrastructure_plan_staging
 <<: *setup_staging
 <<: *terraform_plan

Which layouts the same result. I think the issue is with my anchors definition, but I cannot find the exact reasons.

What I want to achieve is 1) Export dev variable in step_1 and 2) Use these variable in step_2.

What do I miss?


Solution

  • I am unsure how the YAML merge operator is working there, and can't test it now, but I'd go for.

    # Define anchors for reuse
    definitions:
      yaml-anchors:
        - &setup_staging_step
            name: Setup staging deployment
            script:
              - echo "HelLo 1"
    
        - &terraform_plan_step
            name: Execute terraform init and plan
            script:
              - echo "HelLo 2"
    
        - &infrastructure_plan_staging_pipeline
          - step: *setup_staging_step
          - step: *terraform_plan_step
    
    pipelines:
      default: *infrastructure_plan_staging_pipeline
    
    

    Note Bitbucket Pipelines --the application-- does not care about where you put anchor definitions nor their key, but might be concerned about a top-level "infrastructure_plan_staging" key outside the definitions block.

    Also note, when in doubt, you can locally inspect the resulting pipeline loading the file with your favorite tool, e.g. in python:

    import yaml
    
    pipeline = yaml.safe_load(open("bitbucket-pipelines.yml"))
    print(pipeline["pipelines"]["default"])