问题描述
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 anofficially documented environment variableK_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 environmentsinclude 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}
`)
};
部署和测试:
$ 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 probablythe way to go, given that it's mentioned in the official documentation.The X_GOOGLE_FUNCTION_VERSION
environment variable isn't officiallymentioned, 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 opportunisticallywhen debugging, deploying, and testing interactively.
这篇关于是否有用于访问给定部署的云功能的版本号的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!