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

Commit b688ef4

Browse files
committed
Allow docker image version to be customized
Closes #4
1 parent d3f4a8e commit b688ef4

2 files changed

Lines changed: 37 additions & 8 deletions

File tree

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ config :hello, Hello.Mailer,
7070
api_key: "${MAILGUN_API_KEY}"
7171
```
7272

73+
#### How to configure the image version?
74+
75+
By default, the image version uses the following format: `#{mix_version}.#{git_count}-#{git_sha}`
76+
You can provide your own version in `config/prod.exs` like this:
77+
78+
```elixir
79+
# config/config.exs
80+
config :mix_docker,
81+
version: "\#{mix_version}_\#{git_sha}"
82+
```
83+
84+
Additionally, you can pass the version as an argument to `mix docker.publish` and `mix docker.shipit`:
85+
86+
```bash
87+
mix docker.publish --version "#{mix_version}_#{git_sha}"
88+
```
89+
7390

7491
#### How to attach to running app using remote_console?
7592

lib/mix_docker.ex

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ defmodule MixDocker do
4242
Mix.shell.info " docker run -it --rm #{image(:release)} foreground"
4343
end
4444

45-
def publish(_args) do
46-
name = image(:version)
45+
def publish(args) do
46+
name = image(version: image_version(args))
4747

4848
docker :tag, image(:release), name
4949
docker :push, name
@@ -63,13 +63,21 @@ defmodule MixDocker do
6363
end
6464

6565
defp git_head_sha do
66-
{sha, 0} = System.cmd "git", ["rev-parse", "HEAD"]
67-
String.slice(sha, 0, 10)
66+
with true <- File.regular?(".git"),
67+
{sha, 0} <- System.cmd("git", ["rev-parse", "HEAD"]) do
68+
String.slice(sha, 0, 10)
69+
else
70+
_ -> nil
71+
end
6872
end
6973

7074
defp git_commit_count do
71-
{count, 0} = System.cmd "git", ["rev-list", "--count", "HEAD"]
72-
String.trim(count)
75+
with true <- File.regular?(".git"),
76+
{count, 0} <- System.cmd("git", ["rev-list", "--count", "HEAD"]) do
77+
String.trim(count)
78+
else
79+
_ -> nil
80+
end
7381
end
7482

7583
defp image(tag) do
@@ -80,15 +88,19 @@ defmodule MixDocker do
8088
Application.get_env(:mix_docker, :image) || to_string(Mix.Project.get.project[:app])
8189
end
8290

83-
defp image_tag(:version) do
91+
defp image_tag(version: version_template) do
8492
version = Mix.Project.get.project[:version]
8593
count = git_commit_count()
8694
sha = git_head_sha()
8795

88-
"#{version}.#{count}-#{sha}"
96+
Code.eval_string(version_template, mix_version: version, git_count: count, git_sha: sha) |> elem(0)
8997
end
9098
defp image_tag(tag), do: tag
9199

100+
defp image_version(args) do
101+
version = (OptionParser.parse(args) |> elem(0) |> Keyword.get(:version, Application.get_env(:mix_docker, :version, "\#{mix_version}.\#{git_count}-\#{git_sha}")))
102+
"\"" <> version <> "\""
103+
end
92104

93105
defp docker(:cp, cid, source, dest) do
94106
system! "docker", ["cp", "#{cid}:#{source}", dest]

0 commit comments

Comments
 (0)