Skip to content
Browse files

Change Branch on pre-deployment.

  • Loading branch information...
1 parent c2a9bf0 commit ca12e74a6dfb5ea2259d74cb5393dc5d925c3c75 @andres-montanez committed Dec 26, 2012
View
31 Mage/Command/BuiltIn/Deploy.php
@@ -49,7 +49,7 @@ public function run()
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 @@ private function _runNonDeploymentTasks($stage, Mage_Config $config, $title)
{
$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
58 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;
+ }
+}
View
47 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;
+ }
+}
View
8 docs/example-config/.mage/config/environment/staging.yml
@@ -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

0 comments on commit ca12e74

Please sign in to comment.
Something went wrong with that request. Please try again.