LukasKnuth

LukasKnuth

Bake release version at compiletime

I’m using the standard Dockerfile generated by mix phx.gen.release --docker with a small change:

# Compile the release
ARG APP_VERSION
RUN mix compile

The added ARG expression accepts a Docker build-arg that I supply when building the image:

docker build . -t my_app --build-arg APP_VERSION=0.1.1

In my mix.exs I load the data from the Environment variable:

defmodule MyApp.MixProject do
  use Mix.Project

  def project do
    [ #Other config
      version: version()
    ]
  end

  def version do
    System.get_env("APP_VERSION", "0.0.0-local")
  end
end

I’m expecting that MyApp.MixProject.version/0 is invoked at compile time and that the ENV variable is read then and “baked” into the final release.
This seems to be true partially:

  • during image build it logs: assembling my_app-0.1.1 on MIX_ENV=prod
  • running bin/my_app version returns the expected 0.1.1
  • the file releases/start_erl.data contains 15.2.1 0.1.1 (the later is the version)
  • the folder in releases/0.1.1 exists

All this seems to indicate that it worked fine, but at runtime, i get the fallback value:

iex> Application.spec(:my_app, :vsn)
~c"0.0.0-local"

I can’t figure out why it seems to work everywhere except for the Application spec.

I have also tried setting the APP_VERSION env variable to some other string to see if it might be evaluated at runtime - it still returns "0.0.0-local". What am I overlooking?

Marked As Solved

LostKobrakai

LostKobrakai

Not sure if that’s helpful, but all the places where things worked are the result of mix release, while Application.spec uses the .app file of the compiled application as created by mix compile. The .app file would be in lib/my_app-{version}/ebin/my_app.app of the release.

Also Liked

rhcarvalho

rhcarvalho

One of those might be doing it as a side effect of their transitive dependencies. You don’t have to call “mix compile” exclusively to trigger compilation (and in that case your RUN mix compile line was likely a no-op.)

For example, it happens all the time when you run mix test – code is first compiled, then the test suite is run.

Where Next?

Popular in Questions Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
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
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement