Search code examples
elixirphoenix-framework

Git last commit hash and current version


In my Elixir/Phoenix application I want to show the current commit hash from git and version number from the mix.exs file on an html page. How can I do that? That is, is there access to this kind of information at all?


Solution

  • To extract the current commit hash, I have this solution:

    {hash, _} = System.cmd("git", ["rev-parse", "HEAD"])
    # => {"5f6910ce1f42d5026d0ec5544ce6df9c3f8caf17\n", 0}
    
    String.trim(hash)
    # => "5f6910ce1f42d5026d0ec5544ce6df9c3f8caf17"
    

    To get the app version you can try this:

    {:ok, vsn} = :application.get_key(:my_app, :vsn)
    # => {:ok, '0.1.1'}
    
    vsn
    # => '0.1.1'
    
    List.to_string(vsn)
    # => "0.1.1"
    

    Replace the atom :my_app with your app atom.

    Or you use:

    Mix.Project.config[:version]
    # => "0.1.1"
    

    See Mix.Project.config for more information.