【问题标题】:(bitbucket-pipelines.)yml file has weird indentation?(bitbucket-pipelines.)yml 文件有奇怪的缩进?
【发布时间】:2022-02-06 17:30:11
【问题描述】:

为什么这个文件中的每个部分都缩进了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不认为-是第一个字符?但是在管道之后是相同的序列只有两个?

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

【问题讨论】:

    标签: yaml bitbucket bitbucket-pipelines


    【解决方案1】:

    你通常会在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-14
      • 2019-11-30
      • 1970-01-01
      • 1970-01-01
      • 2020-03-13
      • 2011-05-29
      • 2016-08-15
      • 1970-01-01
      相关资源
      最近更新 更多