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
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Class Method Summary collapse
Instance Method Summary collapse
- #branch ⇒ Object
- #build ⇒ Object
- #docker_repository ⇒ Object
- #dump ⇒ Object
- #git_push(tag_name) ⇒ Object
- #git_tag(tag_name) ⇒ Object
- #image_branch ⇒ Object
- #image_latest ⇒ Object
- #image_name ⇒ Object
- #image_release ⇒ 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_release ⇒ 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_release ⇒ Object
Release.
-
#tag_timestamp ⇒ Object
Timestamp.
-
#tag_version ⇒ Object
Version.
- #timestamp ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Tasks
Returns a new instance of Tasks.
24 25 26 27 28 |
# File 'lib/gz_release/tasks.rb', line 24 def initialize( = {}) @image_name, @version = .values_at(:image_name, :version) raise BuildError, 'Image name not specified.' unless image_name end |
Instance Attribute Details
#version ⇒ Object (readonly)
Returns the value of attribute version.
22 23 24 |
# File 'lib/gz_release/tasks.rb', line 22 def version @version end |
Class Method Details
.install(*args) ⇒ Object
17 18 19 |
# File 'lib/gz_release/tasks.rb', line 17 def install(*args) new(*args).install end |
Instance Method Details
#branch ⇒ Object
181 182 183 |
# File 'lib/gz_release/tasks.rb', line 181 def branch @branch ||= (ENV['CIRCLE_BRANCH'] ? ENV['CIRCLE_BRANCH'] : `git rev-parse --abbrev-ref HEAD`).strip.gsub(/\//,'_') end |
#build ⇒ Object
106 107 108 |
# File 'lib/gz_release/tasks.rb', line 106 def build run("docker build -t #{image_sha} .") end |
#docker_repository ⇒ Object
155 156 157 |
# File 'lib/gz_release/tasks.rb', line 155 def docker_repository ENV['DOCKER_REPOSITORY'] || '' end |
#dump ⇒ Object
99 100 101 102 103 104 |
# File 'lib/gz_release/tasks.rb', line 99 def dump require 'fileutils' FileUtils.mkdir_p(DOCKER_CACHE_PATH) run("docker save #{image_latest} > #{DOCKER_IMAGE_PATH}") end |
#git_push(tag_name) ⇒ Object
124 125 126 |
# File 'lib/gz_release/tasks.rb', line 124 def git_push(tag_name) run("git push origin #{tag_name}") end |
#git_tag(tag_name) ⇒ Object
114 115 116 |
# File 'lib/gz_release/tasks.rb', line 114 def git_tag(tag_name) run("git tag -a #{tag_name} -m 'Tagging #{tag_name} from #{branch}(#{sha})' #{sha}") end |
#image_branch ⇒ Object
177 178 179 |
# File 'lib/gz_release/tasks.rb', line 177 def image_branch "#{image_name}:#{branch}" end |
#image_latest ⇒ Object
211 212 213 |
# File 'lib/gz_release/tasks.rb', line 211 def image_latest "#{image_name}:latest" end |
#image_name ⇒ Object
149 150 151 152 153 |
# File 'lib/gz_release/tasks.rb', line 149 def image_name name = '' name << "#{docker_repository}/" unless docker_repository.empty? name << @image_name end |
#image_release ⇒ Object
224 225 226 |
# File 'lib/gz_release/tasks.rb', line 224 def image_release "#{image_name}:release" end |
#image_sha ⇒ Object
SHA
160 161 162 |
# File 'lib/gz_release/tasks.rb', line 160 def image_sha "#{image_name}:#{sha}" end |
#image_timestamp ⇒ Object
239 240 241 |
# File 'lib/gz_release/tasks.rb', line 239 def "#{image_name}:#{}" end |
#image_version ⇒ Object
198 199 200 |
# File 'lib/gz_release/tasks.rb', line 198 def image_version "#{image_name}:#{version}" end |
#install ⇒ Object
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 75 76 77 78 79 80 81 82 83 |
# File 'lib/gz_release/tasks.rb', line 30 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 release tag for the docker image' task(:release){tag_release} 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 tagged as release' task(release: :'release:tag:release'){push_release} desc 'Pushes docker image and git tag tagged by the timestamp.' task(timestamp: :'release:tag:timestamp'){} end end end |
#load ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/gz_release/tasks.rb', line 91 def load if File.exist?(DOCKER_IMAGE_PATH) run("docker load -i #{DOCKER_IMAGE_PATH}") else pull end end |
#login ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/gz_release/tasks.rb', line 128 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} #{docker_repository}", display_command: false) end |
#pull ⇒ Object
85 86 87 88 89 |
# File 'lib/gz_release/tasks.rb', line 85 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
118 119 120 121 122 |
# File 'lib/gz_release/tasks.rb', line 118 def push(tag_name) login run("docker push #{tag_name}") end |
#push_branch ⇒ Object
173 174 175 |
# File 'lib/gz_release/tasks.rb', line 173 def push_branch push(image_branch) end |
#push_latest ⇒ Object
207 208 209 |
# File 'lib/gz_release/tasks.rb', line 207 def push_latest push(image_latest) end |
#push_release ⇒ Object
220 221 222 |
# File 'lib/gz_release/tasks.rb', line 220 def push_release push(image_release) end |
#push_timestamp ⇒ Object
234 235 236 237 |
# File 'lib/gz_release/tasks.rb', line 234 def git_push() push() end |
#push_version ⇒ Object
192 193 194 195 196 |
# File 'lib/gz_release/tasks.rb', line 192 def push_version return unless version push(image_version) end |
#run(command, options = {display_command: true}) ⇒ Object
142 143 144 145 146 147 |
# File 'lib/gz_release/tasks.rb', line 142 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
164 165 166 |
# File 'lib/gz_release/tasks.rb', line 164 def sha @sha ||= `git rev-parse HEAD`.strip end |
#tag(tag_name) ⇒ Object
110 111 112 |
# File 'lib/gz_release/tasks.rb', line 110 def tag(tag_name) run("docker tag -f #{image_sha} #{tag_name}") end |
#tag_branch ⇒ Object
Branch
169 170 171 |
# File 'lib/gz_release/tasks.rb', line 169 def tag_branch tag(image_branch) end |
#tag_latest ⇒ Object
Latest
203 204 205 |
# File 'lib/gz_release/tasks.rb', line 203 def tag_latest tag(image_latest) end |
#tag_release ⇒ Object
Release
216 217 218 |
# File 'lib/gz_release/tasks.rb', line 216 def tag_release tag(image_release) end |
#tag_timestamp ⇒ Object
Timestamp
229 230 231 232 |
# File 'lib/gz_release/tasks.rb', line 229 def git_tag() tag() end |
#tag_version ⇒ Object
Version
186 187 188 189 190 |
# File 'lib/gz_release/tasks.rb', line 186 def tag_version return unless version tag(image_version) end |
#timestamp ⇒ Object
243 244 245 |
# File 'lib/gz_release/tasks.rb', line 243 def @timestamp ||= Time.now.utc.strftime(TIMESTAMP_FORMAT) end |