Cloud Build报错:cloudbuild.yaml第2行未找到预期键,请求排查
Cloud Build YAML解析失败的原因及修复方案
语法错误原因
你遇到的yaml: 第2行未找到预期键错误,大概率是以下两个问题导致:
- HTML转义字符破坏语法:代码中
echo "***********************"里的"是HTML双引号转义字符,YAML解析器无法识别,会干扰语法结构解析。 - 缩进格式不规范:YAML严格要求用空格缩进,若代码中混入了制表符(Tab键),会触发解析错误(错误提示指向第2行,可能是后续行的格式问题引发的连锁报错)。
修复后的Cloud Build YAML代码
steps: - name: 'gcr.io/cloud-builders/git' id: 'Clone the repository' args: ['clone', 'https://github.com/giovannidemaria/MyK8sInfrastructure_terraform.git', './myrepo'] - id: 'files' entrypoint: 'sh' args: - '-c' - | echo "***********************" ls echo "***********************" - id: 'tf init' name: 'hashicorp/terraform:1.0.0' entrypoint: 'sh' args: - '-c' - | cd ./myrepo/code terraform init # [START tf-plan] - id: 'tf plan' name: 'hashicorp/terraform:1.0.0' entrypoint: 'sh' args: - '-c' - | cd ./myrepo/code terraform plan -out=tfplan # [END tf-plan] # [START tf-apply] - id: 'tf apply' name: 'hashicorp/terraform:1.0.0' entrypoint: 'sh' args: - '-c' - | cd ./myrepo/code terraform apply tfplan # [END tf-apply]
额外优化建议
如果你的Cloud Build是直接从GitHub仓库触发的构建,无需手动执行clone操作——Cloud Build会自动将触发构建的代码拉取到工作目录/workspace下,保留clone步骤只会增加构建耗时,可直接删除该步骤,后续操作直接使用工作目录内的代码即可。
内容的提问来源于stack exchange,提问作者Giovanni de Maria
相关产品推荐
相关产品推荐

