Skip to content
This repository has been archived by the owner on Jun 13, 2019. It is now read-only.

Commit

Permalink
Add mix docker.customize
Browse files Browse the repository at this point in the history
  • Loading branch information
teamon committed Jan 17, 2017
1 parent 0aa96a4 commit 821b83e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
9 changes: 9 additions & 0 deletions lib/mix/tasks/docker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,12 @@ defmodule Mix.Tasks.Docker.Shipit do

defdelegate run(args), to: MixDocker, as: :shipit
end

defmodule Mix.Tasks.Docker.Customize do
use Mix.Task

@shortdoc "Copy & customize Dockerfiles"
@preferred_cli_env :prod

defdelegate run(args), to: MixDocker, as: :customize
end
31 changes: 23 additions & 8 deletions lib/mix_docker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ defmodule MixDocker do
publish(args)
end

def customize([]) do
try_copy_dockerfile @dockerfile_build
try_copy_dockerfile @dockerfile_release
end

defp git_head_sha do
{sha, 0} = System.cmd "git", ["rev-parse", "HEAD"]
String.slice(sha, 0, 10)
Expand Down Expand Up @@ -109,26 +114,36 @@ defmodule MixDocker do
system! "docker", ["push", image]
end


defp with_dockerfile(name, fun) do
if File.exists?(name) do
fun.()
else
app = Mix.Project.get.project[:app]

try do
content = [@dockerfile_path, name]
|> Path.join
|> File.read!
|> String.replace("${APP}", to_string(app))
File.write!(name, content)
copy_dockerfile(name)
fun.()
after
File.rm(name)
end
end
end

defp copy_dockerfile(name) do
app = Mix.Project.get.project[:app]
content = [@dockerfile_path, name]
|> Path.join
|> File.read!
|> String.replace("${APP}", to_string(app))
File.write!(name, content)
end

defp try_copy_dockerfile(name) do
if File.exists?(name) do
Logger.warn("#{name} already exists")
else
copy_dockerfile(name)
end
end

defp system(cmd, args) do
Logger.debug "$ #{cmd} #{args |> Enum.join(" ")}"
System.cmd(cmd, args, into: IO.stream(:stdio, :line))
Expand Down
2 changes: 2 additions & 0 deletions test-app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ app.tar.gz
/rel
.dockerignore
mix.lock
Dockerfile.build
Dockerfile.release
6 changes: 4 additions & 2 deletions test-app/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ defmodule App.Mixfile do

defp deps do
[
# can't use path since it will be unavailable inside docker container
{:mix_docker, github: "recruitee/mix_docker", branch: "master"}
case Mix.env do
:dev -> {:mix_docker, path: ".."}
:prod -> {:mix_docker, github: "recruitee/mix_docker", branch: "master"}
end
]
end
end
9 changes: 9 additions & 0 deletions test/mix_docker_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,13 @@ defmodule MixDockerTest do
mix "docker.release"
end
end

test "customize" do
inapp do
mix "docker.customize"

assert File.exists?("Dockerfile.build")
assert File.exists?("Dockerfile.release")
end
end
end

0 comments on commit 821b83e

Please sign in to comment.