mirror of
https://github.com/andres-montanez/Magallanes
synced 2024-06-23 15:50:06 +02:00
Merge pull request #87 from WouterSioen/git-rebase-with-releases
Make the git rebase strategy work with releases
This commit is contained in:
commit
003e36dc64
|
@ -214,4 +214,22 @@ abstract class AbstractTask
|
||||||
return $this->runCommandLocal($command, $output);
|
return $this->runCommandLocal($command, $output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* adds a cd to the needed release if we work with releases.
|
||||||
|
*
|
||||||
|
* @param string $command
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getReleasesAwareCommand($command)
|
||||||
|
{
|
||||||
|
if ($this->getConfig()->release('enabled', false) == true) {
|
||||||
|
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
|
||||||
|
|
||||||
|
$deployToDirectory = $releasesDirectory . '/' . $this->getConfig()->getReleaseId();
|
||||||
|
return 'cd ' . $deployToDirectory . ' && ' . $command;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $command;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,4 +61,45 @@ abstract class BaseStrategyTaskAbstract extends AbstractTask implements IsReleas
|
||||||
|
|
||||||
return array_merge($excludes, $userExcludes);
|
return array_merge($excludes, $userExcludes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes old releases
|
||||||
|
*/
|
||||||
|
protected function cleanUpReleases()
|
||||||
|
{
|
||||||
|
// Count Releases
|
||||||
|
if ($this->getConfig()->release('enabled', false) == true) {
|
||||||
|
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
|
||||||
|
$symlink = $this->getConfig()->release('symlink', 'current');
|
||||||
|
|
||||||
|
if (substr($symlink, 0, 1) == '/') {
|
||||||
|
$releasesDirectory = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $releasesDirectory;
|
||||||
|
}
|
||||||
|
|
||||||
|
$maxReleases = $this->getConfig()->release('max', false);
|
||||||
|
if (($maxReleases !== false) && ($maxReleases > 0)) {
|
||||||
|
$releasesList = '';
|
||||||
|
$countReleasesFetch = $this->runCommandRemote('ls -1 ' . $releasesDirectory, $releasesList);
|
||||||
|
$releasesList = trim($releasesList);
|
||||||
|
|
||||||
|
if ($countReleasesFetch && $releasesList != '') {
|
||||||
|
$releasesList = explode(PHP_EOL, $releasesList);
|
||||||
|
if (count($releasesList) > $maxReleases) {
|
||||||
|
$releasesToDelete = array_diff($releasesList, array($this->getConfig()->getReleaseId()));
|
||||||
|
sort($releasesToDelete);
|
||||||
|
$releasesToDeleteCount = count($releasesToDelete) - $maxReleases;
|
||||||
|
$releasesToDelete = array_slice($releasesToDelete, 0, $releasesToDeleteCount + 1);
|
||||||
|
|
||||||
|
foreach ($releasesToDelete as $releaseIdToDelete) {
|
||||||
|
$directoryToDelete = $releasesDirectory . '/' . $releaseIdToDelete;
|
||||||
|
if ($directoryToDelete != '/') {
|
||||||
|
$command = 'rm -rf ' . $directoryToDelete;
|
||||||
|
$result = $result && $this->runCommandRemote($command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,12 +18,12 @@ use Mage\Task\Releases\IsReleaseAware;
|
||||||
*
|
*
|
||||||
* @author Oscar Reales <oreales@gmail.com>
|
* @author Oscar Reales <oreales@gmail.com>
|
||||||
*/
|
*/
|
||||||
class GitRebaseTask extends AbstractTask implements IsReleaseAware
|
class GitRebaseTask extends BaseStrategyTaskAbstract implements IsReleaseAware
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* (non-PHPdoc)
|
* (non-PHPdoc)
|
||||||
* @see \Mage\Task\AbstractTask::getName()
|
* @see \Mage\Task\AbstractTask::getName()
|
||||||
*/
|
*/
|
||||||
public function getName()
|
public function getName()
|
||||||
{
|
{
|
||||||
return 'Deploy via Git Rebase [built-in]';
|
return 'Deploy via Git Rebase [built-in]';
|
||||||
|
@ -34,44 +34,71 @@ class GitRebaseTask extends AbstractTask implements IsReleaseAware
|
||||||
* @see \Mage\Task\AbstractTask::run()
|
* @see \Mage\Task\AbstractTask::run()
|
||||||
*/
|
*/
|
||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
$branch = $this->getParameter('branch', 'master');
|
$this->checkOverrideRelease();
|
||||||
$remote = $this->getParameter('remote', 'origin');
|
$excludes = $this->getExcludes();
|
||||||
|
|
||||||
// Fetch Remote
|
// If we are working with releases
|
||||||
$command = 'git fetch ' . $remote;
|
$deployToDirectory = $this->getConfig()->deployment('to');
|
||||||
|
if ($this->getConfig()->release('enabled', false) == true) {
|
||||||
|
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
|
||||||
|
|
||||||
|
$deployToDirectory = rtrim($this->getConfig()->deployment('to'), '/')
|
||||||
|
. '/' . $releasesDirectory
|
||||||
|
. '/' . $this->getConfig()->getReleaseId();
|
||||||
|
$this->runCommandRemote('mkdir -p ' . $releasesDirectory . '/' . $this->getConfig()->getReleaseId());
|
||||||
|
}
|
||||||
|
|
||||||
|
$branch = $this->getParameter('branch', 'master');
|
||||||
|
$remote = $this->getParameter('remote', 'origin');
|
||||||
|
|
||||||
|
// Fetch Remote
|
||||||
|
$command = $this->getReleasesAwareCommand('git fetch ' . $remote);
|
||||||
$result = $this->runCommandRemote($command);
|
$result = $this->runCommandRemote($command);
|
||||||
|
|
||||||
|
if ($result === false) {
|
||||||
|
$repository = $this->getConfig()->deployment('repository');
|
||||||
|
if ($repository) {
|
||||||
|
$command = $this->getReleasesAwareCommand('git clone ' . $repository . ' .');
|
||||||
|
$result = $this->runCommandRemote($command);
|
||||||
|
|
||||||
|
$command = $this->getReleasesAwareCommand('git fetch ' . $remote);
|
||||||
|
$result = $this->runCommandRemote($command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Checkout
|
// Checkout
|
||||||
$command = 'git checkout ' . $branch;
|
$command = $this->getReleasesAwareCommand('git checkout ' . $branch);
|
||||||
$result = $this->runCommandRemote($command) && $result;
|
$result = $this->runCommandRemote($command) && $result;
|
||||||
|
|
||||||
// Check Working Copy status
|
// Check Working Copy status
|
||||||
$stashed = false;
|
$stashed = false;
|
||||||
$status = '';
|
$status = '';
|
||||||
$command = 'git checkout ' . $branch;
|
$command = $this->getReleasesAwareCommand('git checkout ' . $branch);
|
||||||
$result = $this->runCommandRemote($command) && $result;
|
$result = $this->runCommandRemote($command) && $result;
|
||||||
|
|
||||||
// Stash if Working Copy is not clean
|
// Stash if Working Copy is not clean
|
||||||
if(!$status) {
|
if(!$status) {
|
||||||
$stashResult = '';
|
$stashResult = '';
|
||||||
$command = 'git stash';
|
$command = $this->getReleasesAwareCommand('git stash');
|
||||||
$result = $this->runCommandRemote($command, $stashResult) && $result;
|
$result = $this->runCommandRemote($command, $stashResult) && $result;
|
||||||
if($stashResult != "No local changes to save") {
|
if($stashResult != "No local changes to save") {
|
||||||
$stashed = true;
|
$stashed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rebase
|
// Rebase
|
||||||
$command = 'git rebase ' . $remote . '/' . $branch;
|
$command = $this->getReleasesAwareCommand('git rebase ' . $remote . '/' . $branch);
|
||||||
$result = $this->runCommandRemote($command) && $result;
|
$result = $this->runCommandRemote($command) && $result;
|
||||||
|
|
||||||
// If Stashed, restore.
|
// If Stashed, restore.
|
||||||
if ($stashed) {
|
if ($stashed) {
|
||||||
$command = 'git stash pop';
|
$command = $this->getReleasesAwareCommand('git stash pop');
|
||||||
$result = $this->runCommandRemote($command) && $result;
|
$result = $this->runCommandRemote($command) && $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->cleanUpReleases();
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,40 +88,7 @@ class RsyncTask extends BaseStrategyTaskAbstract implements IsReleaseAware
|
||||||
|
|
||||||
$result = $this->runCommandLocal($command);
|
$result = $this->runCommandLocal($command);
|
||||||
|
|
||||||
// Count Releases
|
$this->cleanUpReleases();
|
||||||
if ($this->getConfig()->release('enabled', false) == true) {
|
|
||||||
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
|
|
||||||
$symlink = $this->getConfig()->release('symlink', 'current');
|
|
||||||
|
|
||||||
if (substr($symlink, 0, 1) == '/') {
|
|
||||||
$releasesDirectory = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $releasesDirectory;
|
|
||||||
}
|
|
||||||
|
|
||||||
$maxReleases = $this->getConfig()->release('max', false);
|
|
||||||
if (($maxReleases !== false) && ($maxReleases > 0)) {
|
|
||||||
$releasesList = '';
|
|
||||||
$countReleasesFetch = $this->runCommandRemote('ls -1 ' . $releasesDirectory, $releasesList);
|
|
||||||
$releasesList = trim($releasesList);
|
|
||||||
|
|
||||||
if ($countReleasesFetch && $releasesList != '') {
|
|
||||||
$releasesList = explode(PHP_EOL, $releasesList);
|
|
||||||
if (count($releasesList) > $maxReleases) {
|
|
||||||
$releasesToDelete = array_diff($releasesList, array($this->getConfig()->getReleaseId()));
|
|
||||||
sort($releasesToDelete);
|
|
||||||
$releasesToDeleteCount = count($releasesToDelete) - $maxReleases;
|
|
||||||
$releasesToDelete = array_slice($releasesToDelete, 0, $releasesToDeleteCount + 1);
|
|
||||||
|
|
||||||
foreach ($releasesToDelete as $releaseIdToDelete) {
|
|
||||||
$directoryToDelete = $releasesDirectory . '/' . $releaseIdToDelete;
|
|
||||||
if ($directoryToDelete != '/') {
|
|
||||||
$command = 'rm -rf ' . $directoryToDelete;
|
|
||||||
$result = $result && $this->runCommandRemote($command);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,66 +75,19 @@ class TarGzTask extends BaseStrategyTaskAbstract implements IsReleaseAware
|
||||||
$result = $this->runCommandLocal($command) && $result;
|
$result = $this->runCommandLocal($command) && $result;
|
||||||
|
|
||||||
// Extract Tar Gz
|
// Extract Tar Gz
|
||||||
if ($this->getConfig()->release('enabled', false) == true) {
|
$this->getReleasesAwareCommand('tar xfz ' . $remoteTarGz . '.tar.gz');
|
||||||
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
|
|
||||||
|
|
||||||
$deployToDirectory = $releasesDirectory . '/' . $this->getConfig()->getReleaseId();
|
|
||||||
$command = 'cd ' . $deployToDirectory . ' && tar xfz ' . $remoteTarGz . '.tar.gz';
|
|
||||||
} else {
|
|
||||||
$command = 'tar xfz ' . $remoteTarGz . '.tar.gz';
|
|
||||||
}
|
|
||||||
$result = $this->runCommandRemote($command) && $result;
|
$result = $this->runCommandRemote($command) && $result;
|
||||||
|
|
||||||
// Delete Tar Gz from Remote Host
|
// Delete Tar Gz from Remote Host
|
||||||
if ($this->getConfig()->release('enabled', false) == true) {
|
$this->getReleasesAwareCommand('rm ' . $remoteTarGz . '.tar.gz');
|
||||||
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
|
|
||||||
|
|
||||||
$deployToDirectory = $releasesDirectory . '/' . $this->getConfig()->getReleaseId();
|
|
||||||
$command = 'rm ' . $deployToDirectory . '/' . $remoteTarGz . '.tar.gz';
|
|
||||||
} else {
|
|
||||||
$command = 'rm ' . $remoteTarGz . '.tar.gz';
|
|
||||||
}
|
|
||||||
$result = $this->runCommandRemote($command) && $result;
|
$result = $this->runCommandRemote($command) && $result;
|
||||||
|
|
||||||
// Delete Tar Gz from Local
|
// Delete Tar Gz from Local
|
||||||
$command = 'rm ' . $localTarGz . ' ' . $localTarGz . '.tar.gz';
|
$command = 'rm ' . $localTarGz . ' ' . $localTarGz . '.tar.gz';
|
||||||
$result = $this->runCommandLocal($command) && $result;
|
$result = $this->runCommandLocal($command) && $result;
|
||||||
|
|
||||||
// Count Releases
|
$this->cleanUpReleases();
|
||||||
if ($this->getConfig()->release('enabled', false) == true) {
|
|
||||||
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
|
|
||||||
$symlink = $this->getConfig()->release('symlink', 'current');
|
|
||||||
|
|
||||||
if (substr($symlink, 0, 1) == '/') {
|
|
||||||
$releasesDirectory = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $releasesDirectory;
|
|
||||||
}
|
|
||||||
|
|
||||||
$maxReleases = $this->getConfig()->release('max', false);
|
|
||||||
if (($maxReleases !== false) && ($maxReleases > 0)) {
|
|
||||||
$releasesList = '';
|
|
||||||
$countReleasesFetch = $this->runCommandRemote('ls -1 ' . $releasesDirectory, $releasesList);
|
|
||||||
$releasesList = trim($releasesList);
|
|
||||||
|
|
||||||
if ($countReleasesFetch && $releasesList != '') {
|
|
||||||
$releasesList = explode(PHP_EOL, $releasesList);
|
|
||||||
if (count($releasesList) > $maxReleases) {
|
|
||||||
$releasesToDelete = array_diff($releasesList, array($this->getConfig()->getReleaseId()));
|
|
||||||
sort($releasesToDelete);
|
|
||||||
$releasesToDeleteCount = count($releasesToDelete) - $maxReleases;
|
|
||||||
$releasesToDelete = array_slice($releasesToDelete, 0, $releasesToDeleteCount + 1);
|
|
||||||
|
|
||||||
foreach ($releasesToDelete as $releaseIdToDelete) {
|
|
||||||
$directoryToDelete = $releasesDirectory . '/' . $releaseIdToDelete;
|
|
||||||
if ($directoryToDelete != '/') {
|
|
||||||
$command = 'rm -rf ' . $directoryToDelete;
|
|
||||||
$result = $result && $this->runCommandRemote($command);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue