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

Commit 821b83e

Browse files
committed
Add mix docker.customize
1 parent 0aa96a4 commit 821b83e

5 files changed

Lines changed: 47 additions & 10 deletions

File tree

lib/mix/tasks/docker.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,12 @@ defmodule Mix.Tasks.Docker.Shipit do
4141

4242
defdelegate run(args), to: MixDocker, as: :shipit
4343
end
44+
45+
defmodule Mix.Tasks.Docker.Customize do
46+
use Mix.Task
47+
48+
@shortdoc "Copy & customize Dockerfiles"
49+
@preferred_cli_env :prod
50+
51+
defdelegate run(args), to: MixDocker, as: :customize
52+
end

lib/mix_docker.ex

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ defmodule MixDocker do
5757
publish(args)
5858
end
5959

60+
def customize([]) do
61+
try_copy_dockerfile @dockerfile_build
62+
try_copy_dockerfile @dockerfile_release
63+
end
64+
6065
defp git_head_sha do
6166
{sha, 0} = System.cmd "git", ["rev-parse", "HEAD"]
6267
String.slice(sha, 0, 10)
@@ -109,26 +114,36 @@ defmodule MixDocker do
109114
system! "docker", ["push", image]
110115
end
111116

112-
113117
defp with_dockerfile(name, fun) do
114118
if File.exists?(name) do
115119
fun.()
116120
else
117-
app = Mix.Project.get.project[:app]
118-
119121
try do
120-
content = [@dockerfile_path, name]
121-
|> Path.join
122-
|> File.read!
123-
|> String.replace("${APP}", to_string(app))
124-
File.write!(name, content)
122+
copy_dockerfile(name)
125123
fun.()
126124
after
127125
File.rm(name)
128126
end
129127
end
130128
end
131129

130+
defp copy_dockerfile(name) do
131+
app = Mix.Project.get.project[:app]
132+
content = [@dockerfile_path, name]
133+
|> Path.join
134+
|> File.read!
135+
|> String.replace("${APP}", to_string(app))
136+
File.write!(name, content)
137+
end
138+
139+
defp try_copy_dockerfile(name) do
140+
if File.exists?(name) do
141+
Logger.warn("#{name} already exists")
142+
else
143+
copy_dockerfile(name)
144+
end
145+
end
146+
132147
defp system(cmd, args) do
133148
Logger.debug "$ #{cmd} #{args |> Enum.join(" ")}"
134149
System.cmd(cmd, args, into: IO.stream(:stdio, :line))

test-app/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ app.tar.gz
2121
/rel
2222
.dockerignore
2323
mix.lock
24+
Dockerfile.build
25+
Dockerfile.release

test-app/mix.exs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ defmodule App.Mixfile do
1616

1717
defp deps do
1818
[
19-
# can't use path since it will be unavailable inside docker container
20-
{:mix_docker, github: "recruitee/mix_docker", branch: "master"}
19+
case Mix.env do
20+
:dev -> {:mix_docker, path: ".."}
21+
:prod -> {:mix_docker, github: "recruitee/mix_docker", branch: "master"}
22+
end
2123
]
2224
end
2325
end

test/mix_docker_test.exs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,13 @@ defmodule MixDockerTest do
3939
mix "docker.release"
4040
end
4141
end
42+
43+
test "customize" do
44+
inapp do
45+
mix "docker.customize"
46+
47+
assert File.exists?("Dockerfile.build")
48+
assert File.exists?("Dockerfile.release")
49+
end
50+
end
4251
end

0 commit comments

Comments
 (0)