是否存在用于访问给定已部署云功能的版本号的变量 [英] Is there a variable for accessing the version number for a given deployed cloud function
问题描述
GCP在控制台中显示已部署的云功能的版本号.目前,没有系统环境变量,其中包含有关以下内容的信息:部署-没有版本,也没有部署日期.
GCP displays the version number of a deployed cloud function in the console. At the moment there isn't a system environment variable which contains information about the deployment - there is no version, nor deployment date.
鉴于版本更新需要相当长的时间来更新(30秒以上)并进行传播,因此使用这些版本信息将非常有用.
Given that version updates take considerable time to update (30 seconds +) and propagate, such version information would be useful to wield.
推荐答案
最近发布的nodejs10运行时环境现在包括一个
正式记录的环境变量
K_REVISION
包含云功能的部署版本.
The recently-released nodejs10 runtime environment now includes an
officially documented environment variable
K_REVISION
that contains the deployment version of a cloud function.
从检查来看,似乎还存在python37和较旧的nodejs8环境
包含一个非官方的环境变量X_GOOGLE_FUNCTION_VERSION
恰好包含部署版本.
From inspection, it also seems that the python37 and older nodejs8 environments
include an unofficial environment variable X_GOOGLE_FUNCTION_VERSION
that happens to contain the deployment version.
此代码段适用于nodejs10,非官方适用于nodejs8:
This snippet works on nodejs10 and unofficially works on nodejs8:
exports.helloVersion = (req, res) => {
console.log(process.env);
const version = process.env.K_REVISION || process.env.X_GOOGLE_FUNCTION_VERSION || "UNKNOWN";
console.log(`Running version ${version}`);
res.status(200).send(`Running version ${version}\n`)
};
部署和测试:
$ gcloud functions deploy helloVersion --runtime nodejs8 --trigger-http
versionId: '8'
$ curl https://us-central1-myproject.cloudfunctions.net/helloVersion
Running version 8
$ gcloud functions deploy helloVersion --runtime nodejs10 --trigger-http
versionId: '9'
$ curl https://us-central1-myproject.cloudfunctions.net/helloVersion
Running version 9
当然,nodejs10上的K_REVISION
环境变量可能是
考虑到官方文档中提到的方法.
X_GOOGLE_FUNCTION_VERSION
环境变量不是正式的
提到,所以依靠它来做一些重要的事情可能不是一个好主意,
但我发现机会性地展示或包括可能会有所帮助
进行交互式调试,部署和测试时.
Of course, the K_REVISION
environment variable on nodejs10 is probably
the way to go, given that it's mentioned in the official documentation.
The X_GOOGLE_FUNCTION_VERSION
environment variable isn't officially
mentioned, so it's probably a bad idea to rely on it for something important,
but I've found that it might be helpful to display or include opportunistically
when debugging, deploying, and testing interactively.
这篇关于是否存在用于访问给定已部署云功能的版本号的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!