38

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

Koen Bok
  • 3,234
  • 3
  • 29
  • 42

8 Answers8

44

os.environ['CURRENT_VERSION_ID']

Drew Sears
  • 12,812
  • 1
  • 32
  • 41
  • 6
    This is not deprecated. Getting the current version this way is perfectly fine. The deprecated key is APPLICATION_ID and get_application_id() function should be used instead. – Mateusz Mrozewski Sep 05 '13 at 09:34
  • 2
    The result is something like `my-version.383096322806301043`, so split on the dot if you just want the version name. I guess the second part is a timestamp, judging by Marco's answer on this page. – Pat Mar 23 '15 at 21:41
  • 1
    To complement Pat's response I extract it from this line major_ver, minor_ver = os.environ.get('CURRENT_VERSION_ID').rsplit('.',1); – user1961 Jan 23 '16 at 20:16
  • `CURRENT_VERSION_ID` isn't documented, I think it should be `GAE_VERSION`, according to https://cloud.google.com/appengine/docs/flexible/python/runtime#environment_variables – typeracer Feb 25 '21 at 01:37
  • *Update*: looks like the difference between `GAE_VERSION` and `CURRENT_VERSION_ID` is that the former is just the version name, while the latter is `.` (as described in the other answers). – typeracer Mar 03 '21 at 23:42
18
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);
husayt
  • 14,553
  • 8
  • 53
  • 81
  • 3
    To convert the number at the end of the version into the deploy time as number of seconds since epoch, divide the value by 2^28. See: http://code.google.com/p/googleappengine/issues/detail?id=5788 for a feature request for GAE around this. I just tested on a deployed version from this week (June, 2012), and that resulted in the correct timestamp. – mbafford Jun 19 '12 at 16:15
  • 3
    So basically `Date UploadDate = new Date(Long.parseLong(applicationVersion.substring(applicationVersion.lastIndexOf(".")+1)) / (2 << 27) * 1000);` – husayt Jun 19 '12 at 20:57
10
from google.appengine.api import modules
modules.get_current_version_name()

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

Eyal Levin
  • 16,271
  • 6
  • 66
  • 56
10

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

Marco Rossi
  • 736
  • 7
  • 7
  • 1
    `v << 28` is a little easier if you want to shift by 28 bits, and `2 ** 28` is a little easier if you want to do pow. – lericson Aug 08 '12 at 15:09
  • 1
    That will give you the version time. Taking `[0]` instead of `[1]` in the first line will give you the version itself. – asmeurer Oct 01 '12 at 23:25
2

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

process.env.GAE_VERSION

1

You can also access the process' environment variables:

GAE_VERSION

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

Andres Biarge
  • 370
  • 4
  • 15
1

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.

user1559679
  • 306
  • 3
  • 4
1

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.

typeracer
  • 759
  • 8
  • 11