如何修复CodeBuild中DOWNLOAD_SOURCE阶段的YAML_FILE_ERROR错误
AWS CodeBuild DOWNLOAD_SOURCE阶段YAML_FILE_ERROR错误解决
问题现象
构建时DOWNLOAD_SOURCE阶段失败,报错信息如下:
Running on CodeBuild On-demand Waiting for agent ping Waiting for DOWNLOAD_SOURCE Phase is DOWNLOAD_SOURCE CODEBUILD_SRC_DIR=/codebuild/output/src2136787222/src YAML location is /codebuild/output/src2136787222/src/buildspec.yml DOWNLOAD_SOURCE State: FAILED Phase context status code: YAML_FILE_ERROR Message: did not find expected key at line 15
问题原因
查看提供的buildspec.yml文件,第15行的post_build存在缩进错误:它前面多了一个空格,无法与同级的install、build保持一致层级,YAML解析器无法识别该键的正确位置,从而抛出错误。
错误片段示例:
build: commands: - echo Building frontend - npm run build post_build: # 此处缩进3个空格,与install、build的2个空格缩进不一致 commands:
修正方案
将post_build的缩进调整为2个空格,与install、build保持一致。修正后的完整buildspec.yml如下:
version: 0.2 phases: install: runtime-versions: nodejs: 16 commands: - echo Entering dashboardclient directory - cd dashboardclient - echo Installing frontend dependencies - npm install build: commands: - echo Building frontend - npm run build post_build: # 修正为2个空格缩进 commands: - echo Frontend build complete - echo Listing contents of dashboardclient directory - ls -la # Check current directory - ls -la dashboardclient # Check contents of dashboardclient directory - echo Listing contents of build directory - ls -la dashboardclient/dist # Check contents of build directory artifacts: files: - '**/*' base-directory: 'dashboardclient/dist'
关键说明
YAML是严格依赖缩进的配置语言,所有同级配置项必须使用相同数量的空格缩进(通常为2个),层级错误会直接导致解析失败。调整缩进后重新触发构建即可解决该问题。
内容的提问来源于stack exchange,提问作者Mitema Emmanuel
相关产品推荐
相关产品推荐

