部署到Azure Kubernetes时Manifest解析报错求助
问题概述
使用GitHub Action部署应用到Azure Kubernetes集群时,Azure/k8s-deploy@v4执行报错,提示YAML解析失败:
error: error parsing /tmp/manifest.yml: error converting YAML to JSON: yaml: line 25: mapping values are not allowed in this context
已排查确认:Service和Ingress资源可正常配置,问题出在Deployment的Manifest,且与image字段直接相关——替换为Azure官方示例镜像后部署正常。
关键配置与错误日志
GitHub Action部署任务片段
deploy: name: Deploy to kubernetes runs-on: ubuntu-latest needs: docker steps: # 省略其他步骤 - name: Deploy to Azure uses: Azure/k8s-deploy@v4 with: manifests: manifests/test/manifest.yml images: ghcr.io/${{ env.image_name }}:${{ needs.release-on-push.outputs.version }} pull-images: false annotate-namespace: false
Deployment Manifest片段
apiVersion: apps/v1 kind: Deployment # 省略其他配置 spec: template: spec: containers: - name: localizer image: ghcr.io/urltorepo # 省略其他容器配置
核心错误日志
Run Azure/k8s-deploy@v4 with: images: ghcr.io/***/localizer: # 省略其他参数 Deploying manifests /opt/hostedtoolcache/kubectl/1.27.1/x64/kubectl apply -f /tmp/manifest.yml --namespace default service/localizer unchanged ingress.networking.k8s.io/localizer configured error: error parsing /tmp/manifest.yml: error converting YAML to JSON: yaml: line 25: mapping values are not allowed in this context
错误原因分析
从日志可见,images参数的值为ghcr.io/***/localizer:——标签部分为空,说明needs.release-on-push.outputs.version未输出有效内容。Azure/k8s-deploy工具在替换Manifest中的image字段时,生成了格式非法的YAML(比如image: ghcr.io/***/localizer:,冒号后无有效值且后续YAML结构被破坏),导致kubectl解析失败。
解决方案
检查依赖任务的版本输出
确认release-on-push任务是否正确生成并输出version值,该任务需确保能返回有效的镜像标签(如Git commit哈希、语义化版本号等)。添加默认标签容错
若version可能为空,给镜像标签设置默认值,避免出现无标签的镜像地址:images: ghcr.io/${{ env.image_name }}:${{ needs.release-on-push.outputs.version || 'latest' }}规范Manifest中的image字段
给Manifest里的image字段添加占位标签,让替换工具能更稳定地处理:image: ghcr.io/urltorepo:latest临时验证测试
手动指定有效镜像标签,确认部署流程本身无问题:images: ghcr.io/${{ env.image_name }}:v1.0.0
验证
修改配置后重新触发GitHub Action,查看日志是否显示Deployment成功创建/更新,无YAML解析错误。
内容的提问来源于stack exchange,提问作者Akusas

