Skip to content

Instantly share code, notes, and snippets.

@kechol
Last active September 23, 2019 20:45
Show Gist options
  • Select an option

  • Save kechol/59c05d095364fa7a9aa4 to your computer and use it in GitHub Desktop.

Select an option

Save kechol/59c05d095364fa7a9aa4 to your computer and use it in GitHub Desktop.
hubot deploy script
# Description:
# Capistrano deployment from hubot
#
# Dependencies:
# Prepare capistrano config files in your repo.
#
# Configuration:
# None
#
# Commands:
# hubot deploy reponame [env=staging] [branch=master]
fs = require 'fs'
path = require 'path'
shell = require 'shelljs'
module.exports = (robot) ->
robot.respond /deploy( help)?$/i, (msg) ->
msg.send 'usage: hubot deploy reponame [env=staging] [branch=master]'
robot.respond /deploy([\s]+[\w-\/]+?)([\s]+[\w-\/]+?)?([\s]+[\w-\/]+?)?$/i, (msg) ->
repo = (msg.match[1] || '').trim()
stage = (msg.match[2] || '').trim()
branch = (msg.match[3] || '').trim()
# set default value if value is not specified
stage = 'staging' if stage is ''
branch = 'master' if branch is ''
# update sub projects
shell.exec 'git submodule init && git submodule update'
shell.exec 'git submodule foreach git pull origin master'
repo_path = path.resolve(__dirname, "..", "repos/#{repo}")
if fs.existsSync("#{repo_path}/capistrano/Capfile")
shell.cd "#{repo_path}/capistrano"
shell.exec "CAP_DEPLOY_BRANCH=#{branch} cap #{stage} deploy"
else
msg.send "Prease set up capistrano configurations in #{repo_path}/capistrano"
require 'uri'
require 'net/https'
require 'json'
# use command_map for custom commands
#SSHKit.config.command_map[:composer] = '/usr/bin/env php composer.phar'
# config valid only for current version of Capistrano
lock '3.3.5'
set :application, 'Your Application'
set :repo_url, '[email protected]:you/your-repo.git'
# Default branch is :master
# ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }.call
set :branch, ENV.fetch('CAP_DEPLOY_BRANCH', 'master')
# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, '/deploy/path/here'
# Default value for :scm is :git
set :scm, :git
# Default value for :format is :pretty
set :format, :pretty
# Default value for :log_level is :debug
# set :log_level, :debug
# Default value for :pty is false
# set :pty, true
# Default value for :linked_files is []
# set :linked_files, fetch(:linked_files, []).push()
# Default value for linked_dirs is []
# set :linked_dirs, fetch(:linked_dirs, []).push()
# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
# Default value for keep_releases is 5
set :keep_releases, 5
before 'deploy:starting', 'notify:update:start'
after 'deploy:finishing', 'notify:update:finish'
before 'deploy:reverting', 'notify:rollback:start'
after 'deploy:finishing_rollback', 'notify:rollback:finish'
namespace :notify do
namespace :update do
task :start do
send_message "#{fetch(:application)}:#{fetch(:stage)} (#{fetch(:branch)}) $ deploy started :ship:"
end
task :finish do
send_message "#{fetch(:application)}:#{fetch(:stage)} (#{fetch(:branch)}) $ deploy finished. release: #{fetch(:current_revision)}"
end
end
namespace :rollback do
task :start do
send_message "#{fetch(:application)}:#{fetch(:stage)} (#{fetch(:branch)}) $ rollback from #{fetch(:current_revision)} started."
end
task :finish do
send_message "#{fetch(:application)}:#{fetch(:stage)} (#{fetch(:branch)}) $ rollback finished. revision: #{fetch(:current_revision)}"
end
end
end
def send_message(message)
uri = URI.parse(ENV.fetch("SLACK_WEBHOOK_URL"))
payload = {
text: message,
channel: '#release',
username: 'cap',
icon_emoji: ':checkered_flag'
}
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data(payload: payload.to_json)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.request(request)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment