Skip to content

Instantly share code, notes, and snippets.

@holysugar
Last active August 29, 2015 14:18
Show Gist options
  • Save holysugar/0f3b200d1c3d230e4f4a to your computer and use it in GitHub Desktop.
Save holysugar/0f3b200d1c3d230e4f4a to your computer and use it in GitHub Desktop.
手元で手抜き docker build
#!/usr/bin/env ruby
# vim: ft=ruby
#
# Simple Dockerfile build tasks
# https://gist.github.com/holysugar/0a78f58d02e52f1af9f2
#
# set environment variables before execute:
#
# - DOCKER_REGISTRY=192.168.xxx.xxx
# - DOCKER_PREFIX=yourprefix
# - DOCKER_HOST=tcp://192.168.yyy.yyy:4243 (unless boot2docker)
# - DOCKER_IMAGE=imagename (default: directory name, without prefix)
# - GCP_PROJECT_ID (if you push to Google Container Registry)
#
# usage:
# (1) install system commands via thor install
#
# * `thor install dockerbuild.thor`
# * and in a directory with Dockerfile
# * `thor docker:build`
#
# (2) directly execute this script
#
# * in a directory with Dockerfile
# * `path/to/dockerbuild.thor build`
require 'thor'
require 'pathname'
class Docker < Thor
include Thor::Actions
desc "build", "build your Dockerfile"
def build
load_config
say("build #{prefix}/#{name}")
say("version #{read_version}") if read_version
run("docker build -t #{prefix}/#{name} .") or return
run("docker tag -f #{prefix}/#{name} #{prefix}/#{name}:#{read_version}") or return if read_version
true
end
desc "pushtag [registry address]", "tagging for push"
def pushtag(registry = nil)
load_config
registry ||= registry_from_env
run("docker tag -f #{prefix}/#{name} #{registry}/#{prefix}/#{name}") or return
run("docker tag -f #{prefix}/#{name} #{registry}/#{prefix}/#{name}:#{read_version}") or return if read_version
true
end
desc "", "tagging for google container registry"
def gcrtag(gcp_project_id = nil)
load_config
load_gcp_project_id
gcp_project_id ||= ENV['GCP_PROJECT_ID']
run("docker tag -f #{prefix}/#{name} gcr.io/#{gcp_project_id}/#{name}") or return
run("docker tag -f #{prefix}/#{name} gcr.io/#{gcp_project_id}/#{name}:#{read_version}") or return if read_version
true
end
desc "spec", "check spec with serverspec"
def spec
load_config
say("skip spec because no spec directory") and return true unless FileTest.directory?("spec")
run("#{bundle_exec} rake spec")
end
desc "push [registry address]", "push docker image to the (private) registry"
def push(registry = nil)
load_config
registry ||= registry_from_env
run("docker push #{registry}/#{prefix}/#{name}")
end
desc "release [registry address]", "build, spec, pushtag and push image"
def release(registry = nil)
registry ||= registry_from_env
build && spec && pushtag(registry) && push(registry)
end
desc "version", "read dockerfile version"
def version
puts read_version
end
private
# changeme
def prefix
@prefix ||= (ENV['DOCKER_PREFIX'] || `whoami`.chomp)
end
def name
@name ||= (ENV['DOCKER_IMAGE'] || Pathname(Dir.pwd).expand_path.basename.to_s)
end
def read_version
File.read("Dockerfile").scan(/#\s+VERSION\s+(\d+\.\d+\.\d+)\s*$/m).flatten.first
end
def bundle_exec
File.exist?("Gemfile") ? "bundle exec" : ""
end
def load_config
return if ENV['DOCKER_HOST']
if system('which boot2docker >/dev/null 2>/dev/null')
load_config_from_boot2docker
end
end
def load_config_from_boot2docker
conf = `boot2docker shellinit`
puts "---> load config from boot2docker"
puts conf
conf.scan(/export (\w+?)=(.*)$/).each do |key, value|
ENV[key] = value
end
end
def load_gcp_project_id
ENV['GCP_PROJECT_ID'] ||= load_gcp_project_id_from_gcloud
end
def load_gcp_project_id_from_gcloud
info = `gcloud info | grep '^Project:'`.chomp
info.match(/\[(.*)\]/)[1].tr('-','_')
end
def registry_from_env(registry = nil)
registry || ENV['DOCKER_REGISTRY']
end
end
if __FILE__ == $0
Docker.start
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment