Trouble with environment variable when running a mix release

I’ve posted this question on Stackoverflow as well.

When running the following, everything works nicely:

GOOGLE_APPLICATION_CREDENTIALS=/absolute/path/to.json \
MIX_ENV=prod \
DATABASE_URL=... \
SECRET_KEY_BASE=... \
HOST=localhost \
PORT=80 \
iex -S mix phx.server

However, when generating an Elixir release with mix release:

GOOGLE_APPLICATION_CREDENTIALS=/absolute/path/to.json \
MIX_ENV=prod \
DATABASE_URL=... \
SECRET_KEY_BASE=... \
HOST=localhost \
PORT=80 \
mix release

(iex -S mix phx.server is replaced with mix release and the rest is the same)

And running it via: _build/prod/rel/app/bin/app start

I’m getting the following error:

0:24:29.781 [info] Application goth exited: Goth.start(:normal, []) returned an error: shutdown: failed to start child: Goth.Config
    ** (EXIT) an exception was raised:
        ** (RuntimeError)  Failed to retrieve project data from GCE internal metadata service.
                   Either you haven't configured your GCP credentials, you aren't running on GCE, or both.
                   Please see README.md for instructions on configuring your credentials.
            (goth 1.2.0) lib/goth/config.ex:182: Goth.Config.determine_project_id/2
            (goth 1.2.0) lib/goth/config.ex:73: anonymous fn/2 in Goth.Config.load_and_init/1
            (elixir 1.11.2) lib/enum.ex:1403: anonymous fn/3 in Enum.map/2
            (stdlib 3.13.2) maps.erl:233: :maps.fold_1/3
            (elixir 1.11.2) lib/enum.ex:2197: Enum.map/2
            (goth 1.2.0) lib/goth/config.ex:71: Goth.Config.load_and_init/1
            (stdlib 3.13.2) gen_server.erl:417: :gen_server.init_it/2
            (stdlib 3.13.2) gen_server.erl:385: :gen_server.init_it/6

This error means that the GOOGLE_APPLICATION_CREDENTIALS=/absolute/path/to.json part did not work as expected. The JSON file cannot be found, for some reason.

I can guarantee that the JSON file exists and it seems that it’s just not accessible from the release.

Does anyone know why the JSON file cannot be found when running the app from the release?

mix release only builds the release and will leverage those environment variables if they are needed at build time. If those environment variables are accessed at run-time and they are not set then you’ll run into problems. To set application config at run time, use either releases.exs or runtime.exs (https://hexdocs.pm/mix/Mix.Tasks.Release.html#module-runtime-configuration). As a side note, as of Elixir 1.11 runtime.exs is preferred over releases.exs.

4 Likes