diff --git a/Mage/Command/BuiltIn/Deploy.php b/Mage/Command/BuiltIn/Deploy.php index 49bdff8..b64a9d8 100644 --- a/Mage/Command/BuiltIn/Deploy.php +++ b/Mage/Command/BuiltIn/Deploy.php @@ -49,7 +49,7 @@ class Mage_Command_BuiltIn_Deploy if (count($tasksToRun) == 0) { Mage_Console::output('Warning! No Deployment tasks defined.', 2); - Mage_Console::output('Deployment to ' . $config->getHost() . ' skipped!', 1, 3); + Mage_Console::output('Deployment to ' . $host . ' 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'); } } diff --git a/Mage/Task/BuiltIn/Scm/ChangeBranch.php b/Mage/Task/BuiltIn/Scm/ChangeBranch.php new file mode 100644 index 0000000..259bd78 --- /dev/null +++ b/Mage/Task/BuiltIn/Scm/ChangeBranch.php @@ -0,0 +1,58 @@ +_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; + } +} \ No newline at end of file diff --git a/Mage/Task/BuiltIn/Scm/ChangeBranchBack.php b/Mage/Task/BuiltIn/Scm/ChangeBranchBack.php new file mode 100644 index 0000000..d44838e --- /dev/null +++ b/Mage/Task/BuiltIn/Scm/ChangeBranchBack.php @@ -0,0 +1,47 @@ +_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; + } +} \ No newline at end of file diff --git a/docs/example-config/.mage/config/environment/staging.yml b/docs/example-config/.mage/config/environment/staging.yml index 1039bd0..6b33c08 100644 --- a/docs/example-config/.mage/config/environment/staging.yml +++ b/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