Andrew Watson
Andrew Watson

Reputation: 144

Passing values to a Go application on App Engine with Environment variables

According to the docs, you just have to include them in the app.yaml file https://cloud.google.com/appengine/docs/go/config/appconfig#Go_app_yaml_Defining_environment_variables

So I did that like this:

env_variables:
    DEVDOT_OAUTH_CLIENT_ID: 'xxxxx'
    DEVDOT_OAUTH_CLIENT_SECRET: 'xxxxxx'

But when my application runs, it doesn't get these values. I'm running on a MVM runtime so I removed my init() function and tried calling os.Getenv() from both main() and from a handler I had assigned to the /_ah/start path.

Any ideas?

Upvotes: 1

Views: 864

Answers (2)

Diego SG
Diego SG

Reputation: 11

I think you have to get the env variables from the app.yaml file?. This works for me:

  1. Do this go install github.com/joho/godotenv/cmd/godotenv@latest.
  2. Once you install that you will have an .exe file in bin in the GOPATH directory.
  3. Now you just to do that in your directory where you have your project: 3.1. godotenv -f ./app.yaml(or where you have your app.yaml file) go run .\cmd.(or path where you have your main file)

Upvotes: 1

Alex Martelli
Alex Martelli

Reputation: 881555

As how to set environment variables on google appengine? explains, "environment variables are not ready until all the init functions are run and I would say same might be applying to the global variable declaration too. It works from a function because by that time environment variables are set".

You appear to be saying otherwise -- can you try the example code at the other Q I pointed to, which was working when that Q was answered, and tell us what you observe instead?

Upvotes: 1

Related Questions