Koen Bok
Koen Bok

Reputation: 3284

AppEngine: Get current serving application version

Is there a simple way to get the current serving application version in AppEngine?

Upvotes: 38

Views: 12693

Answers (8)

typeracer
typeracer

Reputation: 798

Based on my experiments today, there are two os.environ variables that you can use to get the current app version:

  1. os.environ['GAE_VERSION']: the version name only
  2. os.environ['CURRENT_VERSION_ID']: a unique version identifier composed of {version name}.{deployment id}, which is equivalent to os.environ['GAE_VERSION'] + '.' + os.environ['GAE_DEPLOYMENT_ID']

It appears that the so-called "deployment id" can be right-shifted 28 bits to get a timestamp in epoch seconds (as other answers already described).

For example: I deployed version "101" of my app at 2021-03-04T00:17:12Z and I'm seeing the following values:

  • os.environ['GAE_VERSION']: '101'
  • os.environ['CURRENT_VERSION_ID']: '101.433474146608888597'
  • os.environ['GAE_DEPLOYMENT_ID']: '433474146608888597'

You can use the following code to get the version name and timestamp from os.environ['CURRENT_VERSION_ID']:

>>> import os
>>> import datetime
>>> version_id = os.environ['CURRENT_VERSION_ID']  # example: '101.433474146608888597'
>>> name, ts = version_id.split('.')
>>> dt = datetime.datetime.utcfromtimestamp(int(ts) >> 28))
>>> dt.isoformat()
'2021-03-04T00:17:12'

Disclaimer: Most of this functionality is undocumented and the deployment ID format may be subject to change.

Upvotes: 1

user1559679
user1559679

Reputation: 306

For those who want an update, environment variables set for a GAE instance as of September 2020:

GAE_VERSION is the one that seems to answer the original question.

Google doc:

https://cloud.google.com/appengine/docs/standard/python3/runtime#environment_variables

The following environment variables are set by the runtime:

Environment variable Description GAE_APPLICATION The ID of your App Engine application. This ID is prefixed with 'region code~' such as 'e~' for applications deployed in Europe.

GAE_DEPLOYMENT_ID The ID of the current deployment.

GAE_ENV The App Engine environment. Set to standard.

GAE_INSTANCE The ID of the instance on which your service is currently running.

GAE_MEMORY_MB The amount of memory available to the application process, in MB.

GAE_RUNTIME The runtime specified in your app.yaml file.

GAE_SERVICE The service name specified in your app.yaml file. If no service name is specified, it is set to default.

GAE_VERSION The current version label of your service.

GOOGLE_CLOUD_PROJECT The Cloud project ID associated with your application.

PORT The port that receives HTTP requests.

Upvotes: 1

Kanit Mekritthikrai
Kanit Mekritthikrai

Reputation: 31

For nodejs, I am not sure if this is documented.

process.env.GAE_VERSION

Upvotes: 2

Andres Biarge
Andres Biarge

Reputation: 389

You can also access the process' environment variables:

GAE_VERSION

which is available when you deploy (gcloud app deploy) using the flag --version

Upvotes: 1

Eyal Levin
Eyal Levin

Reputation: 18474

from google.appengine.api import modules
modules.get_current_version_name()

Source: https://cloud.google.com/appengine/docs/python/modules/functions

Upvotes: 10

husayt
husayt

Reputation: 15179

String version = SystemProperty.version.get();
String applicationVersion = SystemProperty.applicationVersion.get();

This is the syntax:

public static final SystemProperty applicationVersion

The major version number for the currently running version of the application plus a timestamp at which it was deployed. Has the key, "com.google.appengine.application.version".

See here

PS. One puzzle still remains. What does timestamp next to version means and how to read it??

EDIT: Here is the key to the mystery.

 Date UploadDate = new Date(Long.parseLong(
   applicationVersion.substring(applicationVersion.lastIndexOf(‌​".")+1))
   / (2 << 27) * 1000);

Upvotes: 18

Marco Rossi
Marco Rossi

Reputation: 736

For Python (GAE SDK release: "1.4.2")

version_id = self.request.environ["CURRENT_VERSION_ID"].split('.')[1]

timestamp = long(version_id) / pow(2,28) 

version = datetime.datetime.fromtimestamp(timestamp).strftime("%d/%m/%y %X")

See http://groups.google.com/group/google-appengine-python/browse_thread/thread/f86010e7cf3c71b4

Upvotes: 10

Drew Sears
Drew Sears

Reputation: 12838

os.environ['CURRENT_VERSION_ID']

Upvotes: 44

Related Questions