GCP Cloud Build YAML能否重复使用步骤并传入不同文件名参数?
GCP Cloud Build重复使用gke-deploy步骤报错解决
问题描述
想要在Cloud Build YAML中重复使用gcr.io/cloud-builders/gke-deploy步骤,仅通过修改--filename参数部署不同的K8s资源,但第二个步骤执行时报错:
Error: failed to prepare deployment: failed to save suggested configuration files to "output/suggested": output directory "output/suggested" exists and is not empty
原配置代码:
- name: 'gcr.io/cloud-builders/gke-deploy' args: - run - '--filename=kubernetes-job' - '--image=asia-south1-docker.pkg.dev/product-staging-apps/test-stg-repo/staging-app:$SHORT_SHA' - '--location=asia-south1' - '--cluster=stg-gke-cluster' id: 'job' - name: 'gcr.io/cloud-builders/gke-deploy' args: - run - '--filename=kubernetes-app' - '--image=asia-south1-docker.pkg.dev/product-staging-apps/test-stg-repo/staging-app:$SHORT_SHA' - '--location=asia-south1' - '--cluster=stg-gke-cluster' waitFor: ['job']
报错原因
gke-deploy run命令默认会在当前工作目录下生成output/suggested目录,用于保存建议的K8s配置文件。第一个步骤执行后该目录已存在,第二个步骤尝试创建时因目录非空而报错。
解决方法
方法1:为每个步骤指定独立输出目录
通过--output-dir参数给每个gke-deploy步骤指定不同的输出目录,避免目录冲突:
- name: 'gcr.io/cloud-builders/gke-deploy' args: - run - '--filename=kubernetes-job' - '--image=asia-south1-docker.pkg.dev/product-staging-apps/test-stg-repo/staging-app:$SHORT_SHA' - '--location=asia-south1' - '--cluster=stg-gke-cluster' - '--output-dir=output/job-suggested' id: 'job' - name: 'gcr.io/cloud-builders/gke-deploy' args: - run - '--filename=kubernetes-app' - '--image=asia-south1-docker.pkg.dev/product-staging-apps/test-stg-repo/staging-app:$SHORT_SHA' - '--location=asia-south1' - '--cluster=stg-gke-cluster' - '--output-dir=output/app-suggested' waitFor: ['job']
方法2:跳过生成建议配置文件
如果不需要保留建议的配置文件,直接添加--skip-suggest参数,命令将不再创建output/suggested目录:
- name: 'gcr.io/cloud-builders/gke-deploy' args: - run - '--filename=kubernetes-job' - '--image=asia-south1-docker.pkg.dev/product-staging-apps/test-stg-repo/staging-app:$SHORT_SHA' - '--location=asia-south1' - '--cluster=stg-gke-cluster' - '--skip-suggest' id: 'job' - name: 'gcr.io/cloud-builders/gke-deploy' args: - run - '--filename=kubernetes-app' - '--image=asia-south1-docker.pkg.dev/product-staging-apps/test-stg-repo/staging-app:$SHORT_SHA' - '--location=asia-south1' - '--cluster=stg-gke-cluster' - '--skip-suggest' waitFor: ['job']
方法3:执行第二个步骤前清理目录
在第二个gke-deploy步骤前,添加一个清理目录的步骤,删除已存在的output/suggested:
- name: 'gcr.io/cloud-builders/gke-deploy' args: - run - '--filename=kubernetes-job' - '--image=asia-south1-docker.pkg.dev/product-staging-apps/test-stg-repo/staging-app:$SHORT_SHA' - '--location=asia-south1' - '--cluster=stg-gke-cluster' id: 'job' - name: 'gcr.io/cloud-builders/rm' args: ['-rf', 'output/suggested'] waitFor: ['job'] - name: 'gcr.io/cloud-builders/gke-deploy' args: - run - '--filename=kubernetes-app' - '--image=asia-south1-docker.pkg.dev/product-staging-apps/test-stg-repo/staging-app:$SHORT_SHA' - '--location=asia-south1' - '--cluster=stg-gke-cluster' waitFor: ['rm']
内容的提问来源于stack exchange,提问作者Naveen
相关产品推荐
相关产品推荐

