Search code examples
google-cloud-platformterraformyamlgoogle-cloud-buildterraform-provider-gcp

Prevent Terraform to add newlines in Cloud Build trigger inline steps


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.


Solution

  • 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.