Skip to content

Commit 2e75816

Browse files
committed
Add docker.rb to manage builds
1 parent 7b6c0ad commit 2e75816

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

Dockerfile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ FROM jaci/rails-base:5.2.3-alpine
22

33
RUN apk add --no-cache sqlite-dev
44

5-
# Bundle install Gemfile first to take advantage of caching
6-
COPY ./Gemfile /app/Gemfile
7-
COPY ./Gemfile.lock /app/Gemfile.lock
5+
# Copy all dependency files over before the main stuff
6+
# The reason for this is that it reduces the number of layers that
7+
# require rebuilding. If we copied all the files here, the bundle install
8+
# layer would require rebuilding every time, which is both time and space
9+
# expensive.
10+
COPY ./build/depslayer /app
811

912
RUN bundle install
1013

docker.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require 'fileutils'
2+
3+
DOCKER_TAG=ENV.fetch('DOCKER_TAG') {"#{`git rev-parse --short HEAD`.strip}#{`git diff HEAD --quiet || echo -dirty`.strip}"}
4+
DOCKER_REPO=ENV.fetch('DOCKER_REPO') {"gcr.io/imjacinta/jaci/curtincourses"}
5+
6+
DOCKER_IMG="#{DOCKER_REPO}:#{DOCKER_TAG}"
7+
8+
def copy_deps
9+
FileUtils.mkdir_p 'build/depslayer'
10+
Dir.glob(['**/Gemfile', '**/Gemfile.lock', '**/*.gemspec']).each do |file|
11+
next if file.include?('build/depslayer')
12+
13+
dest = "build/depslayer/#{file}"
14+
FileUtils.mkdir_p(File.dirname(dest))
15+
16+
if !File.exist?(dest) || File.read(file) != File.read(dest)
17+
puts "Update: #{file} -> #{dest}"
18+
FileUtils.cp(file, dest)
19+
end
20+
end
21+
end
22+
23+
def docker_build
24+
copy_deps
25+
system "docker build -t #{DOCKER_IMG} ."
26+
end
27+
28+
def docker_push
29+
docker_build
30+
system "docker push #{DOCKER_IMG}"
31+
end
32+
33+
if __FILE__ == $0
34+
act = ARGV[0]
35+
if act == 'build'
36+
docker_build
37+
elsif act == 'push'
38+
docker_push
39+
elsif act == 'get_tag'
40+
puts DOCKER_TAG
41+
elsif act == 'get_img'
42+
puts DOCKER_IMG
43+
elsif act == 'get_repo'
44+
puts DOCKER_REPO
45+
end
46+
end

0 commit comments

Comments
 (0)