阻止Terraform在Cloud Build触发器内联步骤中添加换行符。

huangapple go评论88阅读模式
英文:

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=&#39;define(\&quot;WP_STATELESS_MEDIA_MODE\&quot;, \&quot;stateless\&quot;); define(\&quot;WP_STATELESS_MEDIA_BUCKET\&quot;, \&quot;$${_WP_MEDIA_BUCKET}\&quot;);&#39;'"
    ]
}

变成了这样:

- name: gcr.io/cloud-builders/gcloud-slim
    args:
      - run
      - deploy
      - <lot of args>
      - >-
        --set-env-vars=WORDPRESS_CONFIG_EXTRA=&#39;define("WP_STATELESS_MEDIA_MODE",
        "stateless"); define("WP_STATELESS_MEDIA_BUCKET",
        "${_WP_MEDIA_BUCKET}");&#39;        

而且这些硬换行符(在每个逗号后发生,这不可能是巧合)显然会破坏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 = &quot;gcr.io/cloud-builders/gcloud-slim&quot;
  args = [
    &quot;run&quot;,
    &quot;deploy&quot;,
    &quot;&lt;lot of args&gt;&quot;,
    &quot;--set-env-vars=WORDPRESS_CONFIG_EXTRA=&#39;define(\&quot;WP_STATELESS_MEDIA_MODE\&quot;, \&quot;stateless\&quot;); define(\&quot;WP_STATELESS_MEDIA_BUCKET\&quot;, \&quot;$${_WP_MEDIA_BUCKET}\&quot;);&#39;&quot;
    ]
}

become this:

- name: gcr.io/cloud-builders/gcloud-slim
    args:
      - run
      - deploy
      - &lt;lot of args&gt;
      - &gt;-
        --set-env-vars=WORDPRESS_CONFIG_EXTRA=&#39;define(&quot;WP_STATELESS_MEDIA_MODE&quot;,
        &quot;stateless&quot;); define(&quot;WP_STATELESS_MEDIA_BUCKET&quot;,
        &quot;${_WP_MEDIA_BUCKET}&quot;);&#39;

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:

&quot;--set-env-vars=^:^WORDPRESS_CONFIG_EXTRA=&#39;define(\&quot;WP_STATELESS_MEDIA_MODE\&quot;, \&quot;stateless\&quot;); define(\&quot;WP_STATELESS_MEDIA_BUCKET\&quot;, \&quot;$${_WP_MEDIA_BUCKET}\&quot;);&#39;&quot;

this prevented the parsing of the string and makes a valid output yaml file for Cloud Build.

huangapple
  • 本文由 发表于 2023年6月15日 06:38:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定