3

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.

Given that version updates take considerable time to update (30 seconds +) and propagate, such version information would be useful to wield.

Todd
  • 14,946
  • 6
  • 42
  • 56

3 Answers3

2

The recently-released nodejs10 runtime environment now includes an officially documented environment variable K_REVISION that contains the deployment version of a cloud function.

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.

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`)
};

Deploying and testing:

$ 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

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.

wheatazogg
  • 36
  • 3
0

There is no straightforward way to get the version number of a function at runtime. You always have the option of assigning some value yourself and put it in an environment variable using the --set-env-vars flag in gcloud.

If your goal is to figure out if a function invocation is actually running the code you just deployed while iterating during development, it's effective to simply console.log('deployed') at the global scope of your function. The message will appear in the console logs after the deployment is complete, and you can wait for that message to appear in the log in order to know when your new code is active. Everything logged after that log should be from your latest code.

Doug Stevenson
  • 236,239
  • 27
  • 275
  • 302
  • Nice, that's probably enough, but it would be amazing if that console log included the version number too! – Todd Dec 31 '18 at 22:32
  • I totally agree. – Doug Stevenson Dec 31 '18 at 22:35
  • But still, the ability to update a batch function to respond with `res.status(200).send(environment.version);` would eliminate the need to open the log viewer. – Todd Dec 31 '18 at 22:40
  • Again, you can specify your own environment var to do with what you want. Could even be a random UUID, if all you need is a visual match. – Doug Stevenson Dec 31 '18 at 22:45
  • true, it's not perfect (it necessitates command-line interaction), but it's a workaround. – Todd Jan 01 '19 at 01:15
-1

Check gcloud version, which prints out version info.

Laurens Deprost
  • 1,453
  • 3
  • 15
  • Indeed this returns some version numbers, but they are for the SDK components (`gcloud` and its dependencies), rather than the version of a deployed Function application, which is what OP was asking. – Andrei Mustață Jun 19 '20 at 14:13