Current section
Files
Jump to
Current section
Files
lib/mix/tasks/publish.ex
defmodule Mix.Tasks.Publish do
@shortdoc "Build a docker image, tag it with app name and version, and publish it"
use Mix.Task
def run(_args) do
cfg = Mix.Project.config
version = calculate_version
app = cfg[:app]
docker_repo = cfg[:docker_repo]
docker_repo != nil or raise ":docker_repo must be set in mix.exs. This is to protect against accidentally publishing in public repos"
tag = "#{docker_repo}/#{app}:#{version}"
cwd = System.cwd
elixir_version = System.version
IO.puts("Cfg = #{inspect cfg}")
IO.puts("Cwd = #{cwd}")
# Build the deployment tarball with distillery
build_args = ["run", "-v", "#{cwd}:#{cwd}", "-e", "MIX_ENV=prod", "msaraiva/elixir-dev:#{elixir_version}",
"sh", "-c", "cd #{cwd}; mix do deps.get --only prod, release --env=prod"]
{_, 0} = System.cmd("docker", build_args, into: IO.stream(:stdio, :line))
# Find tarball we just built. Distillery release numbering allows us to sort
# to find the latest release (YYYY.MMDD.something)
release_path = "rel/#{app}/releases/"
{:ok, entries} = File.ls(release_path)
release_version = entries
|> Enum.find(fn(e) -> e == cfg[:version] end)
dist_tar = release_path <> release_version <> "/#{app}.tar.gz"
docker_file = EEx.eval_file(System.cwd <> "/Dockerfile.deploy.eex", [dist_tar: dist_tar])
File.write!("Dockerfile", docker_file)
# Build/tag and push the deployment docker file
{_, 0} = System.cmd("docker", ["build", "-t", tag, "."], into: IO.stream(:stdio, :line))
{_, 0} = System.cmd("docker", ["push", tag], into: IO.stream(:stdio, :line))
end
@spec calculate_version :: String.t
def calculate_version do
{rev, 0} = System.cmd("git", ["rev-parse", "--short", "HEAD"])
String.replace_trailing(rev, "\n", "")
end
end