Google cloud platform 无法将true/false设置为环境变量';云函数的s值

Google cloud platform 无法将true/false设置为环境变量';云函数的s值,google-cloud-platform,google-cloud-functions,google-deployment-manager,Google Cloud Platform,Google Cloud Functions,Google Deployment Manager,我正在编写一个部署管理器脚本,它创建一个云函数并设置一些环境变量 除了部署管理器无法正确识别我的一个属性/变量之外,一切都很正常。我不断地犯错误 我有一个属性是本地的,我从CMD行提供 它的值必须是false/true,否则我也可以接受yes/no 在模式文件中,如果我将属性指定为boolean,并将值提供为false/true,那么部署将开始,只有云功能组件会失败并出现错误。我已在下面将错误指定为error#1 如果我将属性指定为string,并将值提供为false/true,则部署将启动,但

我正在编写一个部署管理器脚本,它创建一个云函数并设置一些环境变量

除了部署管理器无法正确识别我的一个属性/变量之外,一切都很正常。我不断地犯错误

我有一个属性
是本地的
,我从CMD行提供

它的值必须是false/true,否则我也可以接受yes/no

在模式文件中,如果我将属性指定为
boolean
,并将值提供为
false/true
,那么部署将开始,只有云功能组件会失败并出现错误。我已在下面将错误指定为
error#1

如果我将属性指定为
string
,并将值提供为
false/true
,则部署将启动,但立即失败并出现错误。我已在下面将错误指定为
error#2

main.jinja

{% set PROJECT_NAME = env['project'] %}
{% set CODE_BUCKET = properties['code-bucket'] %}
{% set IS_LOCAL = properties['is-local'] %}

resources:
- name: create-cf
  type: create_cloud_function.jinja
  properties:
    name: test-cf
    project: {{ PROJECT_NAME }}
    region: europe-west1
    bucket: {{ CODE_BUCKET }}
    runtime: nodejs10
    entryPoint: test
    topic: test
    environmentVariables: { 'CODE_BUCKET': {{ CODE_BUCKET }}, 'IS_LOCAL': {{IS_LOCAL}} }
imports:
- path: create_cloud_function.jinja

required:
- code-bucket
- is-local

properties:
  code-bucket:
    type: string
    description: Name of the code bucket to host the code for Cloud Function.
  is-local:
    type: boolean
    description: Will Cloud Function run locally or in cloud.
{% set codeFolder = properties['name'] %}
{% set environmentVariables = properties['environmentVariables'] %}

resources:
#- type: cloudfunctions.v1.function
- type: gcp-types/cloudfunctions-v1:projects.locations.functions
  name: {{ properties['name'] }}
  properties:
    parent: projects/{{ properties['project'] }}/locations/{{ properties['region'] }}
    location: {{ properties['region'] }}
    function: {{ properties['name'] }}
    sourceArchiveUrl: gs://$(ref.{{ properties['bucket'] }}.name)/{{ codeFolder }}.zip
    entryPoint: {{ properties['entryPoint'] }}
    runtime: {{properties['runtime']}}
    eventTrigger:
      resource: $(ref.{{ properties['topic'] }}.name)
      eventType: providers/cloud.pubsub/eventTypes/topic.publish
    environmentVariables:
      {% for key, value in environmentVariables.items() %}
        {{ key }} : {{ value }}
      {% endfor %}
main.jinja.schema

{% set PROJECT_NAME = env['project'] %}
{% set CODE_BUCKET = properties['code-bucket'] %}
{% set IS_LOCAL = properties['is-local'] %}

resources:
- name: create-cf
  type: create_cloud_function.jinja
  properties:
    name: test-cf
    project: {{ PROJECT_NAME }}
    region: europe-west1
    bucket: {{ CODE_BUCKET }}
    runtime: nodejs10
    entryPoint: test
    topic: test
    environmentVariables: { 'CODE_BUCKET': {{ CODE_BUCKET }}, 'IS_LOCAL': {{IS_LOCAL}} }
imports:
- path: create_cloud_function.jinja

required:
- code-bucket
- is-local

properties:
  code-bucket:
    type: string
    description: Name of the code bucket to host the code for Cloud Function.
  is-local:
    type: boolean
    description: Will Cloud Function run locally or in cloud.
{% set codeFolder = properties['name'] %}
{% set environmentVariables = properties['environmentVariables'] %}

resources:
#- type: cloudfunctions.v1.function
- type: gcp-types/cloudfunctions-v1:projects.locations.functions
  name: {{ properties['name'] }}
  properties:
    parent: projects/{{ properties['project'] }}/locations/{{ properties['region'] }}
    location: {{ properties['region'] }}
    function: {{ properties['name'] }}
    sourceArchiveUrl: gs://$(ref.{{ properties['bucket'] }}.name)/{{ codeFolder }}.zip
    entryPoint: {{ properties['entryPoint'] }}
    runtime: {{properties['runtime']}}
    eventTrigger:
      resource: $(ref.{{ properties['topic'] }}.name)
      eventType: providers/cloud.pubsub/eventTypes/topic.publish
    environmentVariables:
      {% for key, value in environmentVariables.items() %}
        {{ key }} : {{ value }}
      {% endfor %}
创建云函数。jinja

{% set PROJECT_NAME = env['project'] %}
{% set CODE_BUCKET = properties['code-bucket'] %}
{% set IS_LOCAL = properties['is-local'] %}

resources:
- name: create-cf
  type: create_cloud_function.jinja
  properties:
    name: test-cf
    project: {{ PROJECT_NAME }}
    region: europe-west1
    bucket: {{ CODE_BUCKET }}
    runtime: nodejs10
    entryPoint: test
    topic: test
    environmentVariables: { 'CODE_BUCKET': {{ CODE_BUCKET }}, 'IS_LOCAL': {{IS_LOCAL}} }
imports:
- path: create_cloud_function.jinja

required:
- code-bucket
- is-local

properties:
  code-bucket:
    type: string
    description: Name of the code bucket to host the code for Cloud Function.
  is-local:
    type: boolean
    description: Will Cloud Function run locally or in cloud.
{% set codeFolder = properties['name'] %}
{% set environmentVariables = properties['environmentVariables'] %}

resources:
#- type: cloudfunctions.v1.function
- type: gcp-types/cloudfunctions-v1:projects.locations.functions
  name: {{ properties['name'] }}
  properties:
    parent: projects/{{ properties['project'] }}/locations/{{ properties['region'] }}
    location: {{ properties['region'] }}
    function: {{ properties['name'] }}
    sourceArchiveUrl: gs://$(ref.{{ properties['bucket'] }}.name)/{{ codeFolder }}.zip
    entryPoint: {{ properties['entryPoint'] }}
    runtime: {{properties['runtime']}}
    eventTrigger:
      resource: $(ref.{{ properties['topic'] }}.name)
      eventType: providers/cloud.pubsub/eventTypes/topic.publish
    environmentVariables:
      {% for key, value in environmentVariables.items() %}
        {{ key }} : {{ value }}
      {% endfor %}
部署管理器CMD

gcloud deployment-manager deployments create setup --template main.jinja --properties code-bucket:something-random-test-code-bucket,is-local:false
错误#1:-当架构文件中的属性类型为布尔值时

{"ResourceType":"gcp-types/cloudfunctions-v1:projects.locations.functions","ResourceErrorCode":"400","ResourceErrorMessage":{"code":400,"message":"Invalid value at 'function.environment_variables[1].value' (TYPE_STRING), false","status":"INVALID_ARGUMENT","details":[{"@type":"type.googleapis.com/google.rpc.BadRequest","fieldViolations":[{"field":"function.environment_variables[1].value","description":"Invalid value at 'function.environment_variables[1].value' (TYPE_STRING), false"}]}],"statusMessage":"Bad Request","requestPath":"https://cloudfunctions.googleapis.com/v1/projects/someproject/locations/europe-west1/functions","httpMethod":"POST"}}
errors:
- code: MANIFEST_EXPANSION_USER_ERROR
  location: /deployments/setup/manifests/manifest-1571821997285
  message: |-
    Manifest expansion encountered the following errors: Invalid properties for 'main.jinja':
    True is not of type 'string' at ['is-local']
     Resource: main-jinja Resource: config
错误#2:-当架构文件中的属性类型为字符串时

{"ResourceType":"gcp-types/cloudfunctions-v1:projects.locations.functions","ResourceErrorCode":"400","ResourceErrorMessage":{"code":400,"message":"Invalid value at 'function.environment_variables[1].value' (TYPE_STRING), false","status":"INVALID_ARGUMENT","details":[{"@type":"type.googleapis.com/google.rpc.BadRequest","fieldViolations":[{"field":"function.environment_variables[1].value","description":"Invalid value at 'function.environment_variables[1].value' (TYPE_STRING), false"}]}],"statusMessage":"Bad Request","requestPath":"https://cloudfunctions.googleapis.com/v1/projects/someproject/locations/europe-west1/functions","httpMethod":"POST"}}
errors:
- code: MANIFEST_EXPANSION_USER_ERROR
  location: /deployments/setup/manifests/manifest-1571821997285
  message: |-
    Manifest expansion encountered the following errors: Invalid properties for 'main.jinja':
    True is not of type 'string' at ['is-local']
     Resource: main-jinja Resource: config

我不熟悉jinja,但据我的理解,环境变量只能是字符串

说到这里,读取错误#1我会得出结论,实际上,var类型必须是string

然后,在第二个错误中,我们可以清楚地看到您试图将布尔值放入字符串中


因此,是的,您必须将
true
/
false
作为字符串使用。

根据关于的文档,您应该使用以下语法将环境变量添加到模板中:

{{env[“部署”]}#Jinja

它们显示了以下示例:

-类型:compute.v1.instance
名称:vm-{env[“部署”]}
特性:
machineType:zones/us-central1-a/machineType/f1 micro
服务帐户:
-电子邮件:{{env['project_number']}-compute@developer.gserviceaccount.com
范围:
-…

假设您从CMD行提供的
值为local
,并根据:

布尔值不区分大小写,因此TRUE、TRUE和TRUE的处理方式相同

要指定多个属性,请提供逗号分隔的键:值对。按什么顺序指定对并不重要。例如:

`gcloud部署管理器部署创建我的igm

--template vm_template.jinja 

--properties zone:us-central1-a,machineType:n1-standard-1,image:debian-9`

对于
is local
参数,应使用
TRUE、TRUE或TRUE

您可以在jinja文件本身中将该值定义为字符串。有关详细信息,请参阅,其中提供了可以使用的不同方法

在您的情况下,您可以编辑
create\u cloud\u function.jinja
文件并更改:

environmentVariables: {% for key, value in environmentVariables.items() %} {{ key }} : {{ value }} 环境变量: {%为键,值位于environmentVariables.items()%} {{key}}:{{value} 致:

环境变量: {%为键,值位于environmentVariables.items()%} {{key}}:{{value}字符串}
一旦清单完全展开,就应该将该值视为一个字符串,用于对云函数API的API调用

最终,我要做的是从命令行传递
is_LOCAL:“false”
,并在我的jinja文件中传递
{key}:{value}
'true'
'true'
'yes'
,0,
“0”
'0'
,但似乎没有任何效果。。。它似乎将所有内容都检测为布尔值,并给出了错误#2如果您尝试“false”,它会准确地显示
True不是['is-local']
的“string”类型?我的意思是,我尝试了所有可能的真/假、是/否、0/1组合,但它们都失败了,错误是它们不是string类型。。。不知道如何解决这个问题。。。如果我将属性设置为布尔值,它仍然会失败,如果它是字符串,它也会失败。对我来说似乎很奇怪…使用双引号应该在jinja中起作用。我怀疑gcloud命令没有将双引号传递给jinja temlplate。如果您编辑main.jinja模板并对值使用双引号(例如“true”),您是否仍然会遇到相同的错误?1)我说过在为云函数设置环境变量时存在问题,而在我的jinja脚本中使用环境变量时没有问题。2) 正如我在问题中所解释的那样,如果我将属性类型设置为Boolean并提供任何形式的true,那么它将不起作用。如果我将属性类型更改为string并提供任何形式的true,它也不起作用…似乎不起作用。现在我在main.jinja.schema中将
is_local
作为布尔值&根据您的建议更新了代码,但得到了相同的错误#1。