elixirelixir-mix

Get elixir mix release version in application


I have the following mix.exs

defmodule App.MixProject do
  use Mix.Project

  @version "0.1.0"
  def git_ref(), do: System.get_git_hash()

  def project do
    [
      app: :app,
      version: @version,
      ...
      releases: [
        app: fn ->
          [version: @version <> "+" <> git_ref()]
        end
      ]
    ]
  end
end

I would like to output the release version in my app. I have seen this question, Access project version within elixir application but all the answers return "0.1.0", i.e the "project.version"

If I run my release with _build/../app version, it returns the correct value, so it is able to get the git hash and set the key value, but that value is written out to a file for the version command.

I just don't know how to get it "at runtime". Is this possible? I thought the version may overwrite the previous key value, but I guess they are distinct?


Solution

  • As far as I know, the release version is not included into the beam files, it's purely metadata that's handled by the release scripts. I don't believe there is a built-in way to lookup the release version. However, the script exports the version to RELEASE_VSN, so you can use that.

    System.get_env("RELEASE_VSN")
    

    This is documented in the Environment variables section of the mix release docs:

    • RELEASE_VSN - the version of the release, otherwise the latest version is used. It can be set to a custom value when invoking the release. The custom value must be an existing release version in the releases/ directory