Class: GzRelease::Tasks
- Inherits:
-
Object
- Object
- GzRelease::Tasks
- Includes:
- Rake::DSL
- Defined in:
- lib/gz_release/tasks.rb
Overview
Rake tasks for building a docker image
Constant Summary collapse
- DOCKERFILE_PATH =
File.('./Dockerfile')
- DOCKER_CACHE_PATH =
File.('~/.docker')
- DOCKER_IMAGE_PATH =
File.join(DOCKER_CACHE_PATH, 'image.tar')
- TIMESTAMP_FORMAT =
'%Y_%m_%d_%H_%M_%S'- BuildError =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#image_name ⇒ Object
readonly
Returns the value of attribute image_name.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
Instance Method Summary collapse
- #branch ⇒ Object
- #build ⇒ Object
- #dump ⇒ Object
- #git_push(tag_name) ⇒ Object
- #git_tag(tag_name) ⇒ Object
- #image_branch ⇒ Object
- #image_latest ⇒ Object
-
#image_sha ⇒ Object
SHA.
- #image_timestamp ⇒ Object
- #image_version ⇒ Object
-
#initialize(options = {}) ⇒ Tasks
constructor
A new instance of Tasks.
- #install ⇒ Object
- #load ⇒ Object
- #login ⇒ Object
- #pull ⇒ Object
- #push(tag_name) ⇒ Object
- #push_branch ⇒ Object
- #push_latest ⇒ Object
- #push_timestamp ⇒ Object
- #push_version ⇒ Object
- #run(command, options = {display_command: true}) ⇒ Object
- #sha ⇒ Object
- #tag(tag_name) ⇒ Object
-
#tag_branch ⇒ Object
Branch.
-
#tag_latest ⇒ Object
Latest.
-
#tag_timestamp ⇒ Object
Timestamp.
-
#tag_version ⇒ Object
Version.
- #timestamp ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Tasks
Returns a new instance of Tasks.
21 22 23 24 25 |
# File 'lib/gz_release/tasks.rb', line 21 def initialize( = {}) @image_name, @version = .values_at(:image_name, :version) raise BuildError, 'Image name not specified.' unless image_name end |
Instance Attribute Details
#image_name ⇒ Object (readonly)
Returns the value of attribute image_name.
19 20 21 |
# File 'lib/gz_release/tasks.rb', line 19 def image_name @image_name end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
19 20 21 |
# File 'lib/gz_release/tasks.rb', line 19 def version @version end |
Class Method Details
.install(*args) ⇒ Object
14 15 16 |
# File 'lib/gz_release/tasks.rb', line 14 def install(*args) new(*args).install end |
Instance Method Details
#branch ⇒ Object
162 163 164 |
# File 'lib/gz_release/tasks.rb', line 162 def branch @branch ||= ENV['CIRCLE_BRANCH'] ? ENV['CIRCLE_BRANCH'] : `git rev-parse --abbrev-ref HEAD`.strip end |
#build ⇒ Object
97 98 99 |
# File 'lib/gz_release/tasks.rb', line 97 def build run("docker build -t #{image_sha} .") end |
#dump ⇒ Object
90 91 92 93 94 95 |
# File 'lib/gz_release/tasks.rb', line 90 def dump require 'fileutils' FileUtils.mkdir_p(DOCKER_CACHE_PATH) run("docker save #{image_latest} > #{DOCKER_IMAGE_PATH}") end |
#git_push(tag_name) ⇒ Object
115 116 117 |
# File 'lib/gz_release/tasks.rb', line 115 def git_push(tag_name) run("git push origin #{tag_name}") end |
#git_tag(tag_name) ⇒ Object
105 106 107 |
# File 'lib/gz_release/tasks.rb', line 105 def git_tag(tag_name) run("git tag -a #{tag_name} -m 'Tagging #{tag_name} from #{branch}(#{sha})' #{sha}") end |
#image_branch ⇒ Object
158 159 160 |
# File 'lib/gz_release/tasks.rb', line 158 def image_branch "#{image_name}:#{branch}" end |
#image_latest ⇒ Object
192 193 194 |
# File 'lib/gz_release/tasks.rb', line 192 def image_latest "#{image_name}:latest" end |
#image_sha ⇒ Object
SHA
141 142 143 |
# File 'lib/gz_release/tasks.rb', line 141 def image_sha "#{image_name}:#{sha}" end |
#image_timestamp ⇒ Object
207 208 209 |
# File 'lib/gz_release/tasks.rb', line 207 def "#{image_name}:#{timestamp}" end |
#image_version ⇒ Object
179 180 181 |
# File 'lib/gz_release/tasks.rb', line 179 def image_version "#{image_name}:#{version}" end |
#install ⇒ Object
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 66 67 68 69 70 71 72 73 74 |
# File 'lib/gz_release/tasks.rb', line 27 def install namespace :release do desc 'Creates a latest tag and version tag if available. Version is set in Rakefile' desc 'Creates a git tag and image tag of the current timestamp.' task(:tag_timestamp){} desc 'Pulls latest version of the image from dockerhub' task(:pull){pull} desc 'Loads ~/.docker/image.tar into docker cache. If file does not exist, pulls from dockerhub.' task(:load){load} desc 'Dumps docker cache into ~/.docker/image.tar' task(:dump){dump} desc 'Builds the docker image from generated Dockerfile' task(:build){build} namespace :tag do desc 'Creates a tag for the branch for the docker image' task(:branch){tag_branch} desc 'Creates a tag for version specified in Rakefile for docker image' task(:version){tag_version} desc 'Creates latest tag for the docker image' task(:latest){tag_latest} desc 'Creates timestamp tag for the docker image and git repo' task(:timestamp){} end namespace :push do desc 'Pushes docker image tagged as current git branch.' task(branch: :'release:tag:branch'){push_branch} desc 'Pushes docker image tagged as version specified in Rakefile' task(version: :'release:tag:version'){push_version} desc 'Pushes docker image tagged as latest' task(latest: :'release:tag:latest'){push_latest} desc 'Pushes docker image and git tag tagged by the timestamp.' task(timestamp: :'release:tag:timestamp'){} end end end |
#load ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/gz_release/tasks.rb', line 82 def load if File.exist?(DOCKER_IMAGE_PATH) run("docker load -i #{DOCKER_IMAGE_PATH}") else pull end end |
#login ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/gz_release/tasks.rb', line 119 def login email = ENV['DOCKER_EMAIL'] user = ENV['DOCKER_USER'] password = ENV['DOCKER_PASSWORD'] errors = [] errors << 'Missing DOCKER_EMAIL environment variable.' unless email errors << 'Missing DOCKER_USER environment variable.' unless user errors << 'Missing DOCKER_PASSWORD environment variable.' unless password raise BuildError, "Encountered the following errors:\n #{errors.join("\n")}" if errors.any? run("docker login -e #{email} -u #{user} -p #{password}", display_command: false) end |
#pull ⇒ Object
76 77 78 79 80 |
# File 'lib/gz_release/tasks.rb', line 76 def pull run("docker pull #{image_version}") rescue BuildError puts 'Skipping pull. Note, this should generally only happen on first build.' end |
#push(tag_name) ⇒ Object
109 110 111 112 113 |
# File 'lib/gz_release/tasks.rb', line 109 def push(tag_name) login run("docker push #{tag_name}") end |
#push_branch ⇒ Object
154 155 156 |
# File 'lib/gz_release/tasks.rb', line 154 def push_branch push(image_branch) end |
#push_latest ⇒ Object
188 189 190 |
# File 'lib/gz_release/tasks.rb', line 188 def push_latest push(image_latest) end |
#push_timestamp ⇒ Object
202 203 204 205 |
# File 'lib/gz_release/tasks.rb', line 202 def git_push() push() end |
#push_version ⇒ Object
173 174 175 176 177 |
# File 'lib/gz_release/tasks.rb', line 173 def push_version return unless version push(image_version) end |
#run(command, options = {display_command: true}) ⇒ Object
133 134 135 136 137 138 |
# File 'lib/gz_release/tasks.rb', line 133 def run(command, = {display_command: true}) puts command if [:display_command] system(command) raise BuildError, 'There was a problem executing this command.' unless $CHILD_STATUS == 0 end |
#sha ⇒ Object
145 146 147 |
# File 'lib/gz_release/tasks.rb', line 145 def sha @sha ||= `git rev-parse HEAD`.strip end |
#tag(tag_name) ⇒ Object
101 102 103 |
# File 'lib/gz_release/tasks.rb', line 101 def tag(tag_name) run("docker tag -f #{image_sha} #{tag_name}") end |
#tag_branch ⇒ Object
Branch
150 151 152 |
# File 'lib/gz_release/tasks.rb', line 150 def tag_branch tag(image_branch) end |
#tag_latest ⇒ Object
Latest
184 185 186 |
# File 'lib/gz_release/tasks.rb', line 184 def tag_latest tag(image_latest) end |
#tag_timestamp ⇒ Object
Timestamp
197 198 199 200 |
# File 'lib/gz_release/tasks.rb', line 197 def git_tag() tag() end |
#tag_version ⇒ Object
Version
167 168 169 170 171 |
# File 'lib/gz_release/tasks.rb', line 167 def tag_version return unless version tag(image_version) end |
#timestamp ⇒ Object
211 212 213 |
# File 'lib/gz_release/tasks.rb', line 211 def ||= Time.now.utc.strftime(TIMESTAMP_FORMAT) end |