class ServiceTemplate::Deploy

Attributes

errors[R]
github_login[R]

Public Class Methods

new(environment, revision: nil, force: false, github_repo: nil, github_token: nil) click to toggle source
# File lib/service_template/deploy.rb, line 8
def initialize(environment, revision: nil, force: false, github_repo: nil, github_token: nil)
  ServiceTemplate.load_environment

  @github_repo  = github_repo || ENV['GITHUB_REPO']
  @github_token = github_token || ENV['GITHUB_OAUTH_TOKEN']
  @environment  = environment
  @revision     = revision || local_head_revision
  @force        = force

  @errors       = []
end

Public Instance Methods

any_local_uncommited_changes?() click to toggle source
# File lib/service_template/deploy.rb, line 88
def any_local_uncommited_changes?
  changes = local_repo_status.changed.collect{|change| change[0]}
  @errors << "#{changes.to_sentence} have been changed and not committed." if changes.any?
end
any_local_untracked_files?() click to toggle source
# File lib/service_template/deploy.rb, line 93
def any_local_untracked_files?
  changes = local_repo_status.untracked.collect{|change| change[0]}
  @errors << "#{changes.to_sentence} file(s) have been added and are not tracked." if changes.any?
end
deploy!() click to toggle source
# File lib/service_template/deploy.rb, line 20
def deploy!
  if deployable?
    set_github_tag
    "#{@revision} tagged as #{@environment} by #{@github_login} at #{Time.now.to_s(:long)}"
  else
    "Deploy error(s): #{@errors.join(' --- ')}"
  end
end
deployable?() click to toggle source
# File lib/service_template/deploy.rb, line 72
def deployable?
  revision_exists_on_github?
  any_local_uncommited_changes?
  any_local_untracked_files?

  @errors.empty? || @force
end
github_client() click to toggle source
# File lib/service_template/deploy.rb, line 54
def github_client
  return @github_client if @github_client
  @errors << "ENV['GITHUB_REPO'] is not defined" if @github_repo.nil?
  @errors << "ENV['GITHUB_OAUTH_TOKEN'] is not defined" if @github_token.nil?

  client = Octokit::Client.new(access_token: @github_token)
  begin
    @github_login = client.login
    return @github_client = client
  rescue Octokit::Unauthorized
    @errors << "Access denied for GITHUB_OAUTH_TOKEN"
  end
end
local_head_revision() click to toggle source
# File lib/service_template/deploy.rb, line 68
def local_head_revision
  local_repo.object('HEAD').sha
end
local_repo() click to toggle source
# File lib/service_template/deploy.rb, line 46
def local_repo
  @local_repo ||= Git.open('.')
end
local_repo_status() click to toggle source
# File lib/service_template/deploy.rb, line 50
def local_repo_status
  @local_repo_status ||= local_repo.status
end
revision_exists_on_github?() click to toggle source
# File lib/service_template/deploy.rb, line 80
def revision_exists_on_github?
  begin
    github_client.commit(@github_repo, @revision)
  rescue Octokit::NotFound
    @errors << "Revision #{@revision} does not exist on #{@github_repo}, make sure you've merged your changes."
  end
end
set_github_tag() click to toggle source
# File lib/service_template/deploy.rb, line 29
def set_github_tag
  begin
    github_client.update_ref(
      @github_repo,
      "tags/#{@environment}",
      @revision,
      @force
    )
  rescue Octokit::UnprocessableEntity
    github_client.create_ref(
      @github_repo,
      "tags/#{@environment}",
      @revision
    )
  end
end