Change Branch on pre-deployment.

This commit is contained in:
Andrés Montañez 2012-12-26 13:15:37 -02:00
parent c2a9bf0d62
commit ca12e74a6d
4 changed files with 134 additions and 10 deletions

View file

@ -49,7 +49,7 @@ class Mage_Command_BuiltIn_Deploy
if (count($tasksToRun) == 0) {
Mage_Console::output('<light_purple>Warning!</light_purple> <dark_gray>No </dark_gray><light_cyan>Deployment</light_cyan> <dark_gray>tasks defined.</dark_gray>', 2);
Mage_Console::output('Deployment to <dark_gray>' . $config->getHost() . '</dark_gray> skipped!', 1, 3);
Mage_Console::output('Deployment to <dark_gray>' . $host . '</dark_gray> skipped!', 1, 3);
} else {
foreach ($tasksToRun as $taskData) {
@ -150,12 +150,29 @@ class Mage_Command_BuiltIn_Deploy
{
$tasksToRun = $config->getTasks($stage);
// Look for Remote Source
if (is_array($this->_config->deployment('source', null))) {
if ($stage == 'pre-deploy') {
array_unshift($tasksToRun, 'scm/clone');
} elseif ($stage == 'post-deploy') {
array_unshift($tasksToRun, 'scm/remove-clone');
// PreDeployment Hook
if ($stage == 'pre-deploy') {
// Look for Remote Source
if (is_array($this->_config->deployment('source', null))) {
array_unshift($tasksToRun, 'scm/clone');
}
// Change Branch
if ($this->getConfig()->deployment('scm', false)) {
array_unshift($tasksToRun, 'scm/change-branch');
}
}
// PostDeployment Hook
if ($stage == 'post-deploy') {
// Change Branch Back
if ($this->getConfig()->deployment('scm', false)) {
array_unshift($tasksToRun, 'scm/change-branch-back');
}
// Remove Remote Source
if (is_array($this->_config->deployment('source', null))) {
array_push($tasksToRun, 'scm/remove-clone');
}
}

View file

@ -0,0 +1,58 @@
<?php
class Mage_Task_BuiltIn_Scm_ChangeBranch
extends Mage_Task_TaskAbstract
{
private $_name = 'SCM Changing branch [built-in]';
public function getName()
{
return $this->_name;
}
public function init()
{
switch ($this->getConfig()->scm('type')) {
case 'git':
$this->_name = 'SCM Changing branch (GIT) [built-in]';
break;
case 'svn':
$this->_name = 'SCM Changing branch (Subversion) [built-in]';
break;
}
}
public function run()
{
switch ($this->getConfig()->scm('type')) {
case 'git':
$command = 'git branch | grep \'*\' | cut -d\' \' -f 2';
$currentBranch = 'master';
$result = $this->_runLocalCommand($command, $currentBranch);
$scmData = $this->getConfig()->deployment('scm', false);
if ($result && is_array($scmData) && isset($scmData['branch'])) {
$branch = $this->getParameter('branch', $scmData['branch']);
$command = 'git checkout ' . $branch;
$result = $this->_runLocalCommand($command);
$oldBranchFile = '.mage/' . $this->getConfig()->getEnvironment() . '.oldBranch';
file_put_contents($oldBranchFile, $currentBranch);
} else {
throw new Mage_Task_SkipException;
}
break;
default:
return false;
break;
}
$this->getConfig()->reload();
return $result;
}
}

View file

@ -0,0 +1,47 @@
<?php
class Mage_Task_BuiltIn_Scm_ChangeBranchBack
extends Mage_Task_TaskAbstract
{
private $_name = 'SCM Changing branch Back [built-in]';
public function getName()
{
return $this->_name;
}
public function init()
{
switch ($this->getConfig()->scm('type')) {
case 'git':
$this->_name = 'SCM Changing branch Back (GIT) [built-in]';
break;
case 'svn':
$this->_name = 'SCM Changing branch Back (Subversion) [built-in]';
break;
}
}
public function run()
{
switch ($this->getConfig()->scm('type')) {
case 'git':
$oldBranchFile = '.mage/' . $this->getConfig()->getEnvironment() . '.oldBranch';
$currentBranch = trim(file_get_contents($oldBranchFile));
$command = 'git checkout ' . $currentBranch;
$result = $this->_runLocalCommand($command);
@unlink($oldBranchFile);
break;
default:
return false;
break;
}
$this->getConfig()->reload();
return $result;
}
}

View file

@ -3,18 +3,20 @@ deployment:
user: root
from: ./
to: /var/www/
scm:
branch: master
releases:
enabled: true
max: 5
symlink: current
directory: releases
hosts:
- localhost
- dbserver
# - localhost
# - dbserver
tasks:
pre-deploy:
- sampleTask
# - scm/update
- scm/update
on-deploy:
- privileges
- sampleTask