Last active
December 18, 2018 23:13
-
-
Save jeffweiss/9df547a4e472e3cf5bd3 to your computer and use it in GitHub Desktop.
Retrieving application version number in mix.exs from git describe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.Mixfile do | |
use Mix.Project | |
def project do | |
[app: :my_app, | |
version: get_version, | |
elixir: "~> 1.0", | |
elixirc_paths: elixirc_paths(Mix.env), | |
compilers: Mix.compilers, | |
build_embedded: Mix.env == :prod, | |
start_permanent: Mix.env == :prod, | |
deps: deps] | |
end | |
# Configuration for the OTP application | |
# | |
# Type `mix help compile.app` for more information | |
def application do | |
[mod: {MyApp, []}, | |
applications: [:logger]] | |
end | |
defp get_version do | |
version_from_file | |
|> handle_file_version | |
|> String.split("-") | |
|> case do | |
[tag] -> tag | |
[tag, _num_commits, commit] -> "#{tag}-#{commit}" | |
end | |
end | |
defp version_from_file(file \\ "VERSION") do | |
File.read(file) | |
end | |
defp handle_file_version({:ok, content}) do | |
content | |
end | |
defp handle_file_version({:error, _}) do | |
retrieve_version_from_git | |
end | |
defp retrieve_version_from_git do | |
require Logger | |
Logger.warn "Calling out to `git describe` for the version number. This is slow! You should think about a hook to set the VERSION file" | |
System.cmd("git", ["describe", "--always", "--tags"]) | |
|> Tuple.to_list | |
|> List.first | |
|> String.strip | |
end | |
# Specifies which paths to compile per environment | |
defp elixirc_paths(:test), do: ["lib", "web", "test/support"] | |
defp elixirc_paths(_), do: ["lib", "web"] | |
# Specifies your project dependencies | |
# | |
# Type `mix help deps` for examples and options | |
defp deps do | |
[ | |
] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# installed to .git/hooks/post-checkout | |
`git describe --always --tags > VERSION` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# installed to .git/hooks/post-commit | |
`git describe --always --tags > VERSION` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I merged the recommendations above, fixed the warnings about missing ()'s, removed 'v' and only changed one '-' to '.', so tags in the form vN.N work well with mix's SemVer check.
Given a tag like v2.3 then it will produce version "2.3", then after 5 extra commits produce "2.3.5-gitsha", optionally adding -dirty if there where uncommitted files (Officially this makes it a pre-release of 2.3.5, but since each commit increments the patch version, it isn't an issue).