Skip to content

Commit

Permalink
Change Branch on pre-deployment.
Browse files Browse the repository at this point in the history
  • Loading branch information
andres-montanez committed Dec 26, 2012
1 parent c2a9bf0 commit ca12e74
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 10 deletions.
31 changes: 24 additions & 7 deletions Mage/Command/BuiltIn/Deploy.php
Expand Up @@ -49,7 +49,7 @@ public function run()


if (count($tasksToRun) == 0) { 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('<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 { } else {
foreach ($tasksToRun as $taskData) { foreach ($tasksToRun as $taskData) {
Expand Down Expand Up @@ -150,12 +150,29 @@ private function _runNonDeploymentTasks($stage, Mage_Config $config, $title)
{ {
$tasksToRun = $config->getTasks($stage); $tasksToRun = $config->getTasks($stage);


// Look for Remote Source // PreDeployment Hook
if (is_array($this->_config->deployment('source', null))) { if ($stage == 'pre-deploy') {
if ($stage == 'pre-deploy') { // Look for Remote Source
array_unshift($tasksToRun, 'scm/clone'); if (is_array($this->_config->deployment('source', null))) {
} elseif ($stage == 'post-deploy') { array_unshift($tasksToRun, 'scm/clone');
array_unshift($tasksToRun, 'scm/remove-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');
} }
} }


Expand Down
58 changes: 58 additions & 0 deletions Mage/Task/BuiltIn/Scm/ChangeBranch.php
@@ -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;
}
}
47 changes: 47 additions & 0 deletions Mage/Task/BuiltIn/Scm/ChangeBranchBack.php
@@ -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;
}
}
8 changes: 5 additions & 3 deletions docs/example-config/.mage/config/environment/staging.yml
Expand Up @@ -3,18 +3,20 @@ deployment:
user: root user: root
from: ./ from: ./
to: /var/www/ to: /var/www/
scm:
branch: master
releases: releases:
enabled: true enabled: true
max: 5 max: 5
symlink: current symlink: current
directory: releases directory: releases
hosts: hosts:
- localhost # - localhost
- dbserver # - dbserver
tasks: tasks:
pre-deploy: pre-deploy:
- sampleTask - sampleTask
# - scm/update - scm/update
on-deploy: on-deploy:
- privileges - privileges
- sampleTask - sampleTask
Expand Down

0 comments on commit ca12e74

Please sign in to comment.