ojinari
When deploying a release via “mix compile / release”, Mix may not exist on a server, right? Therefore, if Mix.env() == 'abc' won’t work.
What is it then a way to detect the current environment? A one which would work everywhere, whether it be in dev or production or any other custom environment that I may create. And a one which won’t require copy-paste, and which will update itself automatically. Namely, merely using an environmental variable won’t work because what if I launched a project in prod. enviroment but locally? And then in dev. env again locally too. Or in dev. environment on a server? An env. variable would hold the wrong value half of the time - unreliable solution.
Also note that on a server I may use systemd or rc.d service, therefore simply using .env file although would work locally in dev env, wouldn’t on a server because it’d require copy-pasting the env. data into systemd or rc.d services and I want to avoid copy-paste.
Trending in Discussions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
jwarlander
I’m not 100% sure what you’re after, but neither Elixir / Phoenix nor any other programming platform can automagically know if you intend to run the program in “production” or not when you start it.
You have to figure out how to make it work for you.
In our case, we deploy to AWS, with different accounts for test & prod. The app picks up configuration from Secrets Manager, in the form of secrets for eg. database access (hostname + user + password). The secret is always called the same name, but has different contents in the test vs prod accounts.
If we run locally in Mix dev mode, it will just pick up environment variables. If running locally in Mix prod, it’ll pick up the relevant secrets depending on which account the AWS_PROFILE env var points to.
josevalim
The thing is that hardcoding Mix.env() checks in your application is confusing and error prone. Instead, try using the application configuration and setting the proper values in your dev/test/prod. This way an operator of the system can see all behaviour that is different between dev/test/prod without having to grep for Mix.env() checks in the codebase.
ojinari
Do you suggest that I hard-code “current_env: :dev” in
config/dev.exsand “current_env: :prod” inconfig/prod.exs? This doesn’t make sense because how does the appropriate enviroment config file get loaded in the first place? Where does Phoenix itself get information about the current environment from and therefore which env. config file to load?And it’s not about setting values in dev/prod/test. It’s about checking the current environment itself. It’s because in some places in my project I need to check whether or not, for instance, the current environment is dev, in others – if it’s prod, in others – if it’s staging, in others – if it’s my_custom_env.
soup
Application.get_env?Can you provide a stricter example of need? It sounds like a code smell.
I am assuming that say, in Prod you want to hit a real remote service, but in dev you want to hit something else or not hit anything at all.
IMO you should keep your code the same no matter the env and swap in/out a stub service to act as the API where required.
E.g:
And your config/dev|prod|staging sets an appropriate value for the application to load the right service, so your code is just
TicketCreator.create_ticket(...)no matter the environment.ojinari
soup
Yes, but why.
josevalim
No, instead have a config for each specific behavior. For example, imagine there is an external service which provides both dev and prod URLs, make that a configuration instead of checking for the environment inside the code.
ojinari
And it’s not about setting values in dev/prod/test. It’s about checking the current environment itself. It’s because in some places in my project I need to check whether or not, for instance, the current environment is dev, in others – if it’s prod, in others – if it’s staging, in others – if it’s my_custom_env.
The behaviour of some parts of my project would differ depending on the current env. That is, I may do something in case the env. is “dev”, and don’t do it if it’s “my_custom_env”, and do something different if it’s “prod”.
soup
Why do you have to have the
ifin code and not just inject the correct delegate when the service boots though?Anyway, I believe Jose is saying set whatever you want to check in the appropriate config file, you can then get that with
Application.get_envor you can useSystem.get_envif you want to get an regular system environment variables. You could probably doconfig :my_app, :execution_env, Mix.env()inconfig/config.exsI guess thenApplication.get_env(:my_app, :execution_env).I think Jose is also implying that instead of checking against the env en todo, it’s better design to check against specific configurations, which is more readable and maintainable:
vs
See
h Application.get_envandh System.get_enviniex.jwarlander
I guess you could define a module attribute?
..and then use it wherever you need. If I’m not mistaken, it should be evaluated at compile time, so if compiled for Mix env = dev, will always be “dev”, etc.