简体   繁体   English

GCP Cloud Function 环境变量未设置

[英]GCP Cloud Function environment variable becomes unset

I am using Node.js 14 and I am trying to set a cloud function's name as one of its labels for billing purposes (I want to be able to display Cloud Function costs by function using Billing Export and BigQuery).我正在使用Node.js 14并且我正在尝试将云函数的名称设置为其标签之一以用于计费目的(我希望能够使用 Billing Export 和 BigQuery 显示云 Function 成本 function)。 According to docs , the environment variable K_SERVICE will hold the function's name, and I can set labels like so:根据docs ,环境变量K_SERVICE将保存函数的名称,我可以像这样设置标签:

export const myFunc = functions
  .runWith({
    labels: {
      'project-id': process.env.GCLOUD_PROJECT ?? 'not-found',
      'function-name': process.env.K_SERVICE ?? 'not-found',
    },
  })
  .https.onRequest((request, response) => {
      response.json({ fname: process.env.K_SERVICE ?? 'not-found' });
  });

When I deploy this code, it will fail with the following error:当我部署此代码时,它将失败并出现以下错误:

Detailed stack trace: Error: Invalid labels: myFunc.详细堆栈跟踪:错误:无效标签:myFunc。 Label values can only contain lowercase letters, international characters, numbers, _ or -. Label 值只能包含小写字母、国际字符、数字、_ 或 -。

I need my functions to use camelCase naming, so I can't just change the name to use all lower case letters.我需要我的函数使用驼峰式命名,所以我不能只更改名称以使用所有小写字母。 So I update my code as follows:所以我更新我的代码如下:

export const myFunc = functions
  .runWith({
    labels: {
      'project-id': (process.env.GCLOUD_PROJECT ?? 'not-found').toLowerCase(),
      'function-name': (process.env.K_SERVICE ?? 'not-found').toLowerCase(),
      'control-label': 'MY-VAL'.toLowerCase(),
    },
  })
  .https.onRequest((request, response) => {
      response.json({ fname: (process.env.K_SERVICE ?? 'not-found').toLowerCase() });
  });

Now when I deploy this version, it succeeds to deploy, but after doing a function describe using gcloud functions describe myFunc I get this in the labels section:现在当我部署这个版本时,它部署成功,但是在执行 function describe using gcloud functions describe myFunc之后,我在标签部分得到了这个:

labels:
  control-label: my-val
  deployment-tool: cli-firebase
  function-name: not-found
  project-id: my-project-id

Notice several things here:这里注意几件事:

  • My control-label label works as expected, showing the value in all lower case (so functions like .toLowerCase() work inside the .runWith() method)我的control-label label 按预期工作,以所有小写形式显示值(因此.toLowerCase()等函数在.runWith()方法中工作)
  • My project-id label also works as expected, and although its original value is already in lower case, it is fetching the environment variable properly and displays it's value correctly even after applying the .toLowerCase() function.我的project-id label 也按预期工作,虽然它的原始值已经是小写,但它正在正确获取环境变量并正确显示它的值,即使在应用.toLowerCase() function 之后也是如此。
  • Notably, and the purpose of this question: my function-name label returns as not-found , which is my default value if the environment variable were not set.值得注意的是,这个问题的目的是:我的function-name label 返回为not-found ,如果未设置环境变量,这是我的默认值。

But I had previously verified that the environment variable K_SERVICE is indeed set, because if I don't apply the .toLowerCase() on it, the deploy will fail saying the value (properly logging its value) has upper case letters.但我之前已经验证了环境变量K_SERVICE确实已设置,因为如果我不在其上应用.toLowerCase() ,部署将失败说值(正确记录其值)具有大写字母。

Also note that when I call the function and return the value as part of the .onRequest() method, it will correctly return the expected value myfunc all lowercase and matching the function's name.另请注意,当我调用 function 并将该值作为.onRequest()方法的一部分返回时,它将正确返回预期值myfunc所有小写并匹配函数名称。

So what's going on here?那么这是怎么回事? Why would the value of the environment variable change after having the .toLowerCase() method applied inside the .runWith() method?为什么在.toLowerCase()方法中应用.runWith() () 方法后环境变量的值会发生变化?

According to the documentation the behavior is normal:根据文档,行为是正常的:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM