You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

bitbucket-pipelines.yml合并时报YAML语法错误:预期映射却找到标量如何解决?

排查Bitbucket Pipelines YAML语法错误:预期映射却找到标量

错误信息

在bitbucket-pipelines.yml的[第3行,第13列]存在YAML语法错误。合并时预期映射或映射列表,却找到标量

简化后的配置代码

definitions:
  steps:
    - step: &prep_base_images
      name: Docker compose build base images
      image: docker:latest
      deployment: test
      script:
        - echo "hello world"

# Actual pipelines
# ==============================================================================
pipelines:
  branches:
    BLAH-137-backend-and-cicd-update:
      - step:
          <<: *prep_base_images
          deployment: development

问题原因

错误出在第3行的锚点写法上:- step: &prep_base_images 不符合YAML语法规范。这里的step是键,它的值应该是包含name/image等配置的映射块,但你把锚点符号&直接放在step:后面,导致YAML解析器将&prep_base_images识别为标量值,而非预期的映射,从而触发语法错误。

尽管Bitbucket在线验证工具误判为有效,但实际执行时YAML解析器会严格校验结构。

解决方案

修正锚点位置,将其应用到整个step配置的映射上,而非step:键的后面。两种可行写法如下:

写法1:保留step:键的标准结构

definitions:
  steps:
    - &prep_base_images
      step:
        name: Docker compose build base images
        image: docker:latest
        deployment: test
        script:
          - echo "hello world"

pipelines:
  branches:
    BLAH-137-backend-and-cicd-update:
      - step:
          <<: *prep_base_images
          deployment: development

写法2:简化step定义(推荐)

直接将step配置映射作为锚点,省略definitions中冗余的step:键:

definitions:
  steps:
    - &prep_base_images
      name: Docker compose build base images
      image: docker:latest
      deployment: test
      script:
        - echo "hello world"

pipelines:
  branches:
    BLAH-137-backend-and-cicd-update:
      - step:
          <<: *prep_base_images
          deployment: development

两种写法都能让YAML解析器正确识别结构,合并锚点配置时不会再抛出语法错误。

内容的提问来源于stack exchange,提问作者Dannyboy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 19:45:18