(bitbucket-pipelines.)yml 文件有奇怪的缩进?

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

为什么此文件中的每个部分都缩进 2,除了步骤

  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

如果改成两个就报错?

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

这个题目好像是说因为YAML不认为-是第一个字符?但是pipe之后是同一个序列只有两个?

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

你通常会在 YAML 中看到两件事

  1. 序列 — 在其他编程语言中,这通常称为数组、列表……:
    - apple
    - banana
    - pear 
    
  2. 映射——在其他编程语言中,这通常被称为对象、散列、字典、关联数组……:
    question: how do I foobar?
    body: Can you help?
    tag: yaml
    

所以如果你想存储映射的映射你会这样做:

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`

虽然,如果您将列表存储在映射中,则可以不用缩进,确实:

fruits:
- apple
- banana
- pear

严格等于

fruits:
  - apple
  - banana
  - pear

但是,如果您只想缩进管道的第一步,就像这样:

pipelines:
  default:
    - step:
      deployment: production

您最终得到了一个有效的 YAML,但是,一个不再具有相同含义的 YAML

当您的原始 yaml 表示:我确实有一个带有操作列表的默认管道,第一个操作是包含部署到生产环境的步骤.
由此产生的错误缩进的 YAML 意味着:我确实有一个带有操作列表的默认管道,第一个操作有两个属性,第一个 属性 是一个空步骤,而第二个 属性 是对生产环境的部署 .

所以,在这里,作为步骤映射 属性 的 deplyement 键实际上变成了列表 default 的第一个元素的 属性!

要按照您的意愿缩进所有这些内容,您必须执行以下操作:

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

因此,您最终得到了 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