英文:
Prevent Terraform to add newlines in Cloud Build trigger inline steps
问题
我已经在Terraform中构建了一个Cloud Build管道,用于执行带有多个参数的gcloud run deploy
命令。
我在Terraform块内定义了yaml管道,因为我不想与正在开发应用程序的开发团队共享构建详细信息。
由于某些原因,Terraform添加了一些换行符,这将破坏yaml。换句话说,这个块:
step {
name = "gcr.io/cloud-builders/gcloud-slim"
args = [
"run",
"deploy",
"<lot of args>",
"--set-env-vars=WORDPRESS_CONFIG_EXTRA='define(\"WP_STATELESS_MEDIA_MODE\", \"stateless\"); define(\"WP_STATELESS_MEDIA_BUCKET\", \"$${_WP_MEDIA_BUCKET}\");''"
]
}
变成了这样:
- name: gcr.io/cloud-builders/gcloud-slim
args:
- run
- deploy
- <lot of args>
- >-
--set-env-vars=WORDPRESS_CONFIG_EXTRA='define("WP_STATELESS_MEDIA_MODE",
"stateless"); define("WP_STATELESS_MEDIA_BUCKET",
"${_WP_MEDIA_BUCKET}");'
而且这些硬换行符(在每个逗号后发生,这不可能是巧合)显然会破坏yaml。
我在想是否有可能避免这种情况。
英文:
I've built a Cloud Build pipeline in terraform to execute gcloud run deploy with several args.
I defined the yaml pipeline inline inside the terraform block because i don't want to share the build details to the dev team working on the app.
For some reasons, terraform adds some newlines that will break the yaml. In other words, this block:
step {
name = "gcr.io/cloud-builders/gcloud-slim"
args = [
"run",
"deploy",
"<lot of args>",
"--set-env-vars=WORDPRESS_CONFIG_EXTRA='define(\"WP_STATELESS_MEDIA_MODE\", \"stateless\"); define(\"WP_STATELESS_MEDIA_BUCKET\", \"$${_WP_MEDIA_BUCKET}\");'"
]
}
become this:
- name: gcr.io/cloud-builders/gcloud-slim
args:
- run
- deploy
- <lot of args>
- >-
--set-env-vars=WORDPRESS_CONFIG_EXTRA='define("WP_STATELESS_MEDIA_MODE",
"stateless"); define("WP_STATELESS_MEDIA_BUCKET",
"${_WP_MEDIA_BUCKET}");'
and these hard newlines (which happens at every comma, this cannot be a coincidence) obviously break the yaml.
I was wondering if perhaps there is a way to avoid that.
答案1
得分: 1
这原来我在之前的评论中完全错了:逗号 是 一个特殊字符,即使它在引用的字符串中,因为它是gcloud CLI中列表和字典的分隔符。
为了解决这个问题,我需要告诉gcloud使用另一个字符作为分隔符(使用这个前缀 ^DELIM^
在有问题的字符串上)。
因此:
"--set-env-vars=^:^WORDPRESS_CONFIG_EXTRA='define(\"WP_STATELESS_MEDIA_MODE\", \"stateless\"); define(\"WP_STATELESS_MEDIA_BUCKET\", \"$${_WP_MEDIA_BUCKET}\");'"
这阻止了字符串的解析,并生成了一个有效的Cloud Build输出yaml文件。
英文:
It turned out I was totally wrong in my previous comment: the comma is a special character, even if it's in quoted string, as it's the delimiter for lists and dictionaries in gcloud CLI.
To solve the issue I needed to tell gcloud to use another character as delimiter (the ':', which was unused) using this prefix ^DELIM^
on the faulty string.
Thus:
"--set-env-vars=^:^WORDPRESS_CONFIG_EXTRA='define(\"WP_STATELESS_MEDIA_MODE\", \"stateless\"); define(\"WP_STATELESS_MEDIA_BUCKET\", \"$${_WP_MEDIA_BUCKET}\");'"
this prevented the parsing of the string and makes a valid output yaml file for Cloud Build.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论