Is there a variable for accessing the version number for a given deployed cloud function

后端 未结 3 543
深忆病人
深忆病人 2021-01-20 21:53

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 de

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-20 22:22

    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.

提交回复
热议问题