(bitbucket-pipelines.)yml file has weird indentation?

Viewed 251

Why is every section in this file indented by 2 except the step

  image: atlassian/default-image:2
  pipelines:
    default:
    - step:
        deployment: production
        script:
        - git submodule update --recursive --init
        - zip -r $FILENAME . -x bitbucket-pipelines.yml *.git*
        - pipe: atlassian/bitbucket-upload-file:0.1.6
          variables:
            BITBUCKET_USERNAME: $BITBUCKET_USERNAME
            BITBUCKET_APP_PASSWORD: $BITBUCKET_APP_PASSWORD
            FILENAME: $FILENAME

And gives an error if changed to two?

https://bitbucket-pipelines.prod.public.atl-paas.net/validator

This topic seems to say because YAML does not consider - to be the first character? But after pipe is the same sequence with only two?

https://community.atlassian.com/t5/Bitbucket-questions/Is-it-intentional-that-bitbucket-pipelinese-yml-indentation/qaq-p/582084

1 Answers

You will usually see two things in YAML

  1. Sequences — in other programming languages, this is commonly referred as arrays, lists, ...:
    - apple
    - banana
    - pear 
    
  2. Mappings — in other programming languages, this is commonly referred as objects, hashes, dictionaries, associative arrays, ...:
    question: how do I foobar?
    body: Can you help?
    tag: yaml
    

So if you want to store a mapping of mapping you would do:

pier1:
  boat: yes
  in_activity: yes
  comments: needs some painting in 2023
## The mapping above this comment refers to the pier1, 
## as it is indented one level below the key `pier1`

pier2:
  boat: no
  in_activity: no
  comments: currently inactive, needs urgent repair
## The mapping above this comment refers to the pier2,
## as it is indented one level below the key `pier2`

While, if you store a list in a mapping, you could go without the indent, indeed:

fruits:
- apple
- banana
- pear

Is strictly equal to

fruits:
  - apple
  - banana
  - pear

But, if you are trying to indent the first step of your pipeline only, like so:

pipelines:
  default:
    - step:
      deployment: production

You end up with an valid YAML, but, a YAML that does not have the same meaning anymore.

When your original yaml means: I do have a default pipeline with a list of actions, the first action being a step that consists of a deployment to production.
The resulting incorrectly indented YAML means: I do have a default pipeline with a list of actions, the first action having two properties, the first property being an empty step, and the second property is that it is a deployment to production.

So, here, really, the deplyement key that was a property of the step mapping became a property of the first element of the list default!

To indent all this as you would like, you will have to go:

pipelines:
  default:
    - step:
        deployment: production
##      ^-- This property is now further indented too

So, you end up with the YAML:

image: atlassian/default-image:2
pipelines:
  default:
    - step:
        deployment: production
        script:
          - git submodule update --recursive --init
          - zip -r $FILENAME . -x bitbucket-pipelines.yml *.git*
          - pipe: atlassian/bitbucket-upload-file:0.1.6
            variables:
              BITBUCKET_USERNAME: $BITBUCKET_USERNAME
              BITBUCKET_APP_PASSWORD: $BITBUCKET_APP_PASSWORD
              FILENAME: $FILENAME
Related