How to find the release name during compile time?

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.

6 Likes