Is there a way to to get my autoscaled application's VersionID in my init()
function without a Context
? The only available option seems to be appengine.VersionID(context.Context)
. Manually scaled instances have /_ah/start
called when they start up (giving access to a Context
), but there is nothing like this for autoscaled instances.
I am not caring about the generated ID that appengine.VersionID
returns with it, just the app.yaml version.
EDIT: A bit of context: I am wanting to deploy versions in the form x-x-x-dev or x-x-x-live and have my database connection depend on the version suffix. This way, when I look in the GCP console, I can be certain which deployed modules/services are using which database. Of course, I setup my DB connection pool in the init()
, which has no access to a Context
.
I searched and searched with no answers online anywhere, so here it is.
Simply parse the app.yaml file in your init()
function. My example here uses a yaml parsing package, but it can be done more lightweight if you need.
import "github.com/ghodss/yaml"
type AppVersion struct {
Version string `json:"version"`
}
func VersionID() (string, error) {
dat, err := ioutil.ReadFile("app.yaml")
if err != nil {
return "", err
}
a := &AppVersion{}
err = yaml.Unmarshal(dat, a)
if err != nil {
return "", err
}
return a.Version, nil
}
Note that this DOES NOT return the generated ID in the form X.Y that appengine.VersionID()
does. Only the X part of the version.
As an aside, in the appengine repo on Github, the actual call to appengine.VersionID
requires a Context
, but internally calls the internal package with nil
. So they basically force you to call it with a Context
, but it isn't actually used. It's incredibly infuriating.
EDIT: It should be noted that the new Go SDK in gcloud no longer supports version
in the app.yaml, as it is now a required parameter at deploy. However, the "legacy" SDK is still supported and maintained, which I am continuing to use as of today (12/24/2018).