Skip to content

Commit

Permalink
Add docker.rb to manage builds
Browse files Browse the repository at this point in the history
  • Loading branch information
JaciBrunning committed Mar 14, 2020
1 parent 7b6c0ad commit 2e75816
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ FROM jaci/rails-base:5.2.3-alpine

RUN apk add --no-cache sqlite-dev

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

RUN bundle install

Expand Down
46 changes: 46 additions & 0 deletions docker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'fileutils'

DOCKER_TAG=ENV.fetch('DOCKER_TAG') {"#{`git rev-parse --short HEAD`.strip}#{`git diff HEAD --quiet || echo -dirty`.strip}"}
DOCKER_REPO=ENV.fetch('DOCKER_REPO') {"gcr.io/imjacinta/jaci/curtincourses"}

DOCKER_IMG="#{DOCKER_REPO}:#{DOCKER_TAG}"

def copy_deps
FileUtils.mkdir_p 'build/depslayer'
Dir.glob(['**/Gemfile', '**/Gemfile.lock', '**/*.gemspec']).each do |file|
next if file.include?('build/depslayer')

dest = "build/depslayer/#{file}"
FileUtils.mkdir_p(File.dirname(dest))

if !File.exist?(dest) || File.read(file) != File.read(dest)
puts "Update: #{file} -> #{dest}"
FileUtils.cp(file, dest)
end
end
end

def docker_build
copy_deps
system "docker build -t #{DOCKER_IMG} ."
end

def docker_push
docker_build
system "docker push #{DOCKER_IMG}"
end

if __FILE__ == $0
act = ARGV[0]
if act == 'build'
docker_build
elsif act == 'push'
docker_push
elsif act == 'get_tag'
puts DOCKER_TAG
elsif act == 'get_img'
puts DOCKER_IMG
elsif act == 'get_repo'
puts DOCKER_REPO
end
end

0 comments on commit 2e75816

Please sign in to comment.