sezaru

sezaru

How to find the release name during compile time?

My system has two different releases, I want to change compile time configurations and enable/disable code at compile time depending on which release I’m building.

I know I can use the env variable RELEASE_NAME during runtime to find which release was generated, but I can’t figure out how to find that during compilation time.

Is there some way to achieve that?

Marked As Solved

christhekeele

christhekeele

I’ve used MIX_TARGET to do stuff like this before. If you have:

# mix.exs

def project do
  [
    releases: [
      demo: [
        include_executables_for: [:unix],
        applications: [runtime_tools: :permanent]
      ],
      staging: [
        # ...
      ]
    ]
  ]
end

Instead of just doing

$ MIX_ENV=prod mix release demo

You could do

$ MIX_ENV=prod MIX_TARGET=demo mix release demo

Just like MIX_ENV, Config is target-aware, so you can set some compile-time config there based on config_target, and then code can reference those values with Application.compile_env to enable/disable what you want.


Example:

# config/config.exs

# Something specific configured for the demo release
case config_target() do
  :demo -> config Ecto.Repo, :special, :case
  :staging -> config Ecto.Repo, :other, :case
end

# Dedicated files containing configuration for each release
import_config "#{config_target()}.exs"

# Something generally available for reference when compiling your code
config :my_app, :release_mode, config_target()
# lib/my_app.ex

case Application.compile_env!(:my_app, :release_mode) do
  :demo -> def release_variant, do: :whatever
  :staging -> def release_variant, do: :whatever_else
end

MIX_TARGET defaults to :host, so you can cover that case to assume local development or raise an error in config/config.exs if you want to force folk to be explicit.

Where Next?

Popular in Questions Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement