Set & load environment variables in a phoenix app
I have found another example
1.- Create a file .env
in you main folder
2.- Add the env variables in the .env
file
# Example:
# MyApp/.env file
export GITHUB_CLIENT_ID="testID"
export GITHUB_SECRET_CLIENT_ID="testSecretID"
3.- Run source .env
and every time it is modified, execute the command again, maybe in reboot too
4.- What is really important - don't forget to add your secret files to MyApp/.gitignore
# add this at the end
/.env
5.- Run phoenix server mix phx.server
or mix phoenix.server
You can test it with
iex -S mix phoenix.server
iex> System.get_env("GITHUB_CLIENT_ID")
"testID"
Links of help:
Environment variable error
Documentation of config
There are two main glitches with what you are trying to do:
First of all, you are trying to instruct mix
to configure oauth
OTP application, while what you need is to configure your own one:
config :my_app, :oauth,
github_client_id: "(CLIENT ID)",
github_client_secret: "(SECRET)"
Now in your main config you might do:
config :ueberauth, Ueberauth.Strategy.Github.OAuth,
client_id: Application.get_env(:my_app, :oauth)[:github_client_id],
client_secret: Application.get_env(:my_app, :oauth)[:github_client_secret]
The second glitch is that GITHUB_CLIENT_ID
is an atom, and you are trying to access it lately as a string. In general, one should not use atom names starting with an uppercase since they kinda reserved for modules names.
On the other hand, you might still use System.get_env/2
(with your config.ex
,) assuming the values were previously put there in environment:
prod.secret.exs
(as it is still plain Elixir)
System.put_env("GITHUB_CLIENT_ID", "(CLIENT ID)")
System.put_env("GITHUB_CLIENT_SECRET", "(CLIENT SECRET)")