hubot-deployment/deployment.coffee

100 lines
3 KiB
CoffeeScript
Raw Normal View History

2016-01-21 13:52:05 +01:00
# Description:
# Deployment script
#
# Commands:
2016-01-21 16:07:27 +01:00
# hubot deploy <project> [with <program>] [from branch <branch>] to <target> [after <command>[, <command>, ...]]
2016-01-21 13:52:05 +01:00
#
# URLS:
#
# Notes:
#
module.exports = (robot) ->
2016-01-21 15:15:45 +01:00
fs = require 'fs'
2016-01-21 16:07:27 +01:00
robot.hear /deploy +([^\s]+)( +with +([^\s]+))?( +from +branch +([^\s]+))? +to +([^\s]+)( +after (.*))?$/i, (res) ->
project = res.match[1]
target = res.match[6]
program = if res.match[3] then res.match[3] else null
branch = if res.match[5] then res.match[5] else null
commands = if res.match[8] then cleanCommands res.match[8].split "," else []
2016-01-21 15:15:45 +01:00
directory = "/home/simon/www/repo/" + project
try
fs.stat directory, (err, stats) ->
if err != null or !stats.isDirectory
return res.reply "Project not found [" + directory + "]"
2016-01-21 16:38:31 +01:00
try
fs.statSync directory + '/.svn'
pv = new SvnProjectVersionning branch
catch e
2016-01-21 16:07:27 +01:00
2016-01-21 16:38:31 +01:00
try
fs.statSync directory + '/.git'
pv = new GitProjectVersionning branch
catch e
if pv
deploy directory, pv, commands
else
res.reply "Not a SVN or GIT repository"
2016-01-21 15:15:45 +01:00
catch e
res.reply "Exception: " + e
2016-01-21 13:52:05 +01:00
2016-01-21 16:07:27 +01:00
class ProjectVersionning
constructor: (@branch) ->
getUpdateCommands: () ->
class SvnProjectVersionning extends ProjectVersionning
getUpdateCommands: () ->
['svn up']
class GitProjectVersionning extends ProjectVersionning
getUpdateCommands: () ->
commands = []
if @branch == null
commands.push 'git pull origin $(git rev-parse --abbrev-ref HEAD)'
else
br = @branch.split('/')
if br.length == 1
commands.push 'git checkout ' + br[0]
commands.push 'git pull origin ' + br[0]
else if br.length == 2
commands.push 'git checkout ' + br[1]
commands.push 'git pull ' + br.join(' ')
commands
2016-01-21 16:38:31 +01:00
deploy = (directory, projectVersionning, commands) ->
exec = require('sync-exec');
commands = projectVersionning.getUpdateCommands().concat(commands)
for command in commands
exec command, {cwd: directory}
2016-01-21 16:07:27 +01:00
2016-01-21 13:52:05 +01:00
cleanCommands = (commands) ->
results = []
2016-01-21 15:15:45 +01:00
commands = commands.map Function.prototype.call, String.prototype.trim
interpreters = {
"sf" : "php symfony",
"sf2" : "php app/console",
"sf3" : "php bin/console",
"artisan": "php artisan",
}
2016-01-21 13:52:05 +01:00
for command in commands
2016-01-21 15:15:45 +01:00
if command != ""
if command.match /(sf[2-3]?|artisan):.+/
elements = command.split(":")
interpreter = elements[0]
elements = elements.splice 1
args = elements.join(":")
results.push interpreters[interpreter] + " " + args
2016-01-21 13:52:05 +01:00
results