-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
65 lines (47 loc) · 1.34 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require "rake"
task :build do
dockerfiles = changed_files
if dockerfiles.none?
puts
puts "No Dockerfiles changed to build."
exit(0)
end
dockerfiles.each do |dockerfile|
image, tag = dockerfile.gsub(/\/Dockerfile/, "").split("/")
context = File.dirname(dockerfile)
tag ||= "latest"
puts
puts "Building #{image}:#{tag}..."
puts
if ENV.fetch("DEBUG", "").empty?
run %{docker build -t emaiax/#{image}:#{tag} #{context}}
unless ENV.fetch("DEPLOY_TO_DOCKER_HUB", "").empty?
run %{docker login -u #{ENV.fetch("DOCKER_USER", "")} -p #{ENV.fetch("DOCKER_PASSWORD", "")}}
run %{docker push emaiax/#{image}:#{tag}}
end
end
end
end
task default: :build
private
def run(cmd)
puts "[Running] #{cmd}"
%x{#{cmd}}
end
def changed_files
head, upstream = git_diffs
if head == upstream
files = run %{git diff HEAD~ --name-only --diff-filter=d -- '*Dockerfile'}
else
files = run %{git diff "#{upstream.chomp}...#{head.chomp}" --name-only --diff-filter=d -- '*Dockerfile'}
end
files.split("\n")
end
def git_diffs
repo = "https://github.com/emaiax/dockerfiles.git"
branch = "master"
head = run %{git rev-parse --verify HEAD}
run %{git fetch -q #{repo} refs/heads/#{branch}}
upstream = run %{git rev-parse --verify FETCH_HEAD}
[head, upstream]
end