Aplicativo Sinatra para implantar branch master com base na carga útil SemaphoreApp – Parte 2

Por favor, consulte a parte 1 deste post para a implementação original que inclui a configuração de vhosts Gemfile, config.ru e Apache …

app.rb

require 'open3'

WORKSPACE
= '/var/www/deploy/whatevs_repo_clone'

post
'/deploy' do
begin
write_log
"Received post: branch #{data["branch_name"]} #{data["result"]}"

if master_branch_passed?
message_campfire deploy_notification
, room
deploy

else
write_log
"SKIPPING DEPLOYMENT FOR #{data['branch_name']} with a result of #{data['result']}"
end
rescue => e
write_log e
.inspect
end
end

get '/' do
"Whatchu talkin bout, Willis?"
end

def write_log(log_message, filename = :master)
File.open("log/deploy_#{filename.to_s}.log", "a") do |file|
file
.write "[#{current_time}] #{log_message.to_s}n"
end
end

def current_time
Time.now.strftime("%Y-%m-%d %h:%m:%s")
end

def message_campfire(message, room)
write_log
"Messaged Campfire: #{message}"
room
.speak message
end

def campfire
@campfire ||= Tinder::Campfire.new account, :token => token, :ssl_verify => false
end

def account
ENV
['CAMPFIRE_ACCOUNT']
end

def token
ENV
['CAMPFIRE_TOKEN']
end

def room
@room ||= campfire.find_room_by_name("Whatevs")
end

def json
request
.body.read
end

def data
@data ||= Yajl::Parser.parse(json)
end

def deploy_notification
"Deploying #{data['branch_name']} to staging"
end

def master_branch_passed?
data
['branch_name'] == 'master' && data['result'] == 'passed'
end

def command
@command ||= [
"cd #{WORKSPACE}",
"git reset --hard origin",
"git pull origin master",
"bundle install",
"bundle exec cap staging deploy:migrations",
].join(" && ")
end

def deploy
Bundler.with_sparkling_clean_env do
write_log
"Executing: #{command}"
executed
= Open3.capture3(command)
write_log
"stdout: #{executed[0]}"
write_log
"stderr: #{executed[1]}"
write_log
"success?: #{executed[2].success?}"
end
room
.speak "Deployment finished: #{command}"
write_log
"DONE"
end

module Bundler
class << self
def with_sparkling_clean_env
oenv
= ENV.to_hash

%w{BUNDLE_GEMFILE RUBYOPT GEM_HOME GIT_DIR GIT_WORK_TREE}.each { |key| ENV.delete(key) }

yield

ENV
.replace(oenv)
end
end
end

Para obter mais dicas sobre UNIX, consulte
Learning the UNIX Command Line .

Aproveite,
Chip