Class: GzRelease::Tasks

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/gz_release/tasks.rb

Overview

Rake tasks for building a docker image

Constant Summary collapse

DOCKERFILE_PATH =
File.expand_path('./Dockerfile')
DOCKER_CACHE_PATH =
File.expand_path('~/.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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Tasks

Returns a new instance of Tasks.

Raises:



21
22
23
24
25
# File 'lib/gz_release/tasks.rb', line 21

def initialize(options = {})
  @image_name, @version = options.values_at(:image_name, :version)

  raise BuildError, 'Image name not specified.' unless image_name
end

Instance Attribute Details

#image_nameObject (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

#versionObject (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

#branchObject



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

#buildObject



97
98
99
# File 'lib/gz_release/tasks.rb', line 97

def build
  run("docker build -t #{image_sha} .")
end

#dumpObject



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_branchObject



158
159
160
# File 'lib/gz_release/tasks.rb', line 158

def image_branch
  "#{image_name}:#{branch}"
end

#image_latestObject



192
193
194
# File 'lib/gz_release/tasks.rb', line 192

def image_latest
  "#{image_name}:latest"
end

#image_shaObject

SHA



141
142
143
# File 'lib/gz_release/tasks.rb', line 141

def image_sha
  "#{image_name}:#{sha}"
end

#image_timestampObject



207
208
209
# File 'lib/gz_release/tasks.rb', line 207

def image_timestamp
  "#{image_name}:#{timestamp}"
end

#image_versionObject



179
180
181
# File 'lib/gz_release/tasks.rb', line 179

def image_version
  "#{image_name}:#{version}"
end

#installObject



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){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){tag_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'){push_timestamp}
    end
  end
end

#loadObject



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

#loginObject

Raises:



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/gz_release/tasks.rb', line 119

def 
  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

#pullObject



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)
  

  run("docker push #{tag_name}")
end

#push_branchObject



154
155
156
# File 'lib/gz_release/tasks.rb', line 154

def push_branch
  push(image_branch)
end

#push_latestObject



188
189
190
# File 'lib/gz_release/tasks.rb', line 188

def push_latest
  push(image_latest)
end

#push_timestampObject



202
203
204
205
# File 'lib/gz_release/tasks.rb', line 202

def push_timestamp
  git_push(timestamp)
  push(image_timestamp)
end

#push_versionObject



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

Raises:



133
134
135
136
137
138
# File 'lib/gz_release/tasks.rb', line 133

def run(command, options = {display_command: true})
  puts command if options[:display_command]
  system(command)

  raise BuildError, 'There was a problem executing this command.' unless $CHILD_STATUS == 0
end

#shaObject



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_branchObject

Branch



150
151
152
# File 'lib/gz_release/tasks.rb', line 150

def tag_branch
  tag(image_branch)
end

#tag_latestObject

Latest



184
185
186
# File 'lib/gz_release/tasks.rb', line 184

def tag_latest
  tag(image_latest)
end

#tag_timestampObject

Timestamp



197
198
199
200
# File 'lib/gz_release/tasks.rb', line 197

def tag_timestamp
  git_tag(timestamp)
  tag(image_timestamp)
end

#tag_versionObject

Version



167
168
169
170
171
# File 'lib/gz_release/tasks.rb', line 167

def tag_version
  return unless version

  tag(image_version)
end

#timestampObject



211
212
213
# File 'lib/gz_release/tasks.rb', line 211

def timestamp
  @timestamp ||= Time.now.utc.strftime(TIMESTAMP_FORMAT)
end