[Galactica] Allow to deploy a specific tag.

This commit is contained in:
Andrés Montañez 2022-04-10 21:34:29 -03:00
parent b80efc1e09
commit 5d2ab6fa64
No known key found for this signature in database
GPG Key ID: 97E9F675F4C03DE2
8 changed files with 139 additions and 12 deletions

View File

@ -42,12 +42,19 @@ class DeployCommand extends AbstractCommand
$this
->setName('deploy')
->setDescription('Deploy code to hosts')
->addArgument('environment', InputArgument::REQUIRED, 'Name of the environment to deploy to')
->addArgument('environment', InputArgument::REQUIRED, 'Name of the environment to deploy to.')
->addOption(
'branch',
null,
InputOption::VALUE_REQUIRED,
'Force to switch to a branch other than the one defined',
'Force to switch to a branch other than the one defined.',
false
)
->addOption(
'tag',
null,
InputOption::VALUE_REQUIRED,
'Deploys a specific tag.',
false
);
}
@ -83,14 +90,25 @@ class DeployCommand extends AbstractCommand
$output->writeln(sprintf(' Strategy: <fg=green>%s</>', $strategy->getName()));
if (($input->getOption('branch') !== false) && ($input->getOption('tag') !== false)) {
throw new RuntimeException('Branch and Tag options are mutually exclusive.');
}
if ($input->getOption('branch') !== false) {
$this->runtime->setEnvOption('branch', $input->getOption('branch'));
}
if ($input->getOption('tag') !== false) {
$this->runtime->setEnvOption('branch', false);
$this->runtime->setEnvOption('tag', $input->getOption('tag'));
$output->writeln(sprintf(' Tag: <fg=green>%s</>', $this->runtime->getEnvOption('tag')));
}
if ($this->runtime->getEnvOption('branch', false)) {
$output->writeln(sprintf(' Branch: <fg=green>%s</>', $this->runtime->getEnvOption('branch')));
}
$output->writeln('');
$this->runDeployment($output, $strategy);
} catch (RuntimeException $exception) {

View File

@ -38,7 +38,11 @@ class ReleasesStrategy implements StrategyInterface
$this->checkStage(Runtime::PRE_DEPLOY);
$tasks = $this->runtime->getTasks();
if ($this->runtime->getBranch() && !$this->runtime->inRollback() && !in_array('git/change-branch', $tasks)) {
if (
($this->runtime->getBranch() || $this->runtime->getTag()) &&
!$this->runtime->inRollback() &&
!in_array('git/change-branch', $tasks)
) {
array_unshift($tasks, 'git/change-branch');
}
@ -94,12 +98,16 @@ class ReleasesStrategy implements StrategyInterface
$this->checkStage(Runtime::POST_DEPLOY);
$tasks = $this->runtime->getTasks();
if (!$this->runtime->inRollback() && !in_array('deploy/tar/cleanup', $tasks)) {
array_unshift($tasks, 'deploy/tar/cleanup');
if (
($this->runtime->getBranch() || $this->runtime->getTag()) &&
!$this->runtime->inRollback() &&
!in_array('git/change-branch', $tasks)
) {
array_unshift($tasks, 'git/change-branch');
}
if ($this->runtime->getBranch() && !$this->runtime->inRollback() && !in_array('git/change-branch', $tasks)) {
array_push($tasks, 'git/change-branch');
if (!$this->runtime->inRollback() && !in_array('deploy/tar/cleanup', $tasks)) {
array_unshift($tasks, 'deploy/tar/cleanup');
}
return $tasks;

View File

@ -38,7 +38,11 @@ class RsyncStrategy implements StrategyInterface
$this->checkStage(Runtime::PRE_DEPLOY);
$tasks = $this->runtime->getTasks();
if ($this->runtime->getBranch() && !$this->runtime->inRollback() && !in_array('git/change-branch', $tasks)) {
if (
($this->runtime->getBranch() || $this->runtime->getTag()) &&
!$this->runtime->inRollback() &&
!in_array('git/change-branch', $tasks)
) {
array_unshift($tasks, 'git/change-branch');
}
@ -72,7 +76,12 @@ class RsyncStrategy implements StrategyInterface
$this->checkStage(Runtime::POST_DEPLOY);
$tasks = $this->runtime->getTasks();
if ($this->runtime->getBranch() && !$this->runtime->inRollback() && !in_array('git/change-branch', $tasks)) {
if (
($this->runtime->getBranch() ||
$this->runtime->getTag()) &&
!$this->runtime->inRollback() &&
!in_array('git/change-branch', $tasks)
) {
array_push($tasks, 'git/change-branch');
}

View File

@ -495,6 +495,16 @@ class Runtime
return $this->getEnvOption('branch', false);
}
/**
* Shortcut for getting Tag information
*
* @return bool|string
*/
public function getTag(): mixed
{
return $this->getEnvOption('tag', false);
}
/**
* Guesses the Deploy Strategy to use
*/

View File

@ -30,12 +30,17 @@ class ChangeBranchTask extends AbstractTask
public function getDescription(): string
{
$options = $this->getOptions();
$tag = $options['tag'];
$branch = $options['branch'];
if ($this->runtime->getVar('git_revert_branch', null)) {
$branch = $this->runtime->getVar('git_revert_branch');
}
if ($tag) {
return sprintf('[Git] Checkout Tag (%s)', $tag);
}
return sprintf('[Git] Change Branch (%s)', $branch);
}
@ -59,7 +64,7 @@ class ChangeBranchTask extends AbstractTask
throw new SkipException();
}
$branch = $options['branch'];
$branch = $options['tag'] ? $options['tag'] : $options['branch'];
$this->runtime->setVar('git_revert_branch', $currentBranch);
}
@ -75,9 +80,10 @@ class ChangeBranchTask extends AbstractTask
*/
protected function getOptions(): array
{
$tag = $this->runtime->getEnvOption('tag', false);
$branch = $this->runtime->getEnvOption('branch', 'master');
$options = array_merge(
['path' => 'git', 'branch' => $branch],
['path' => 'git', 'branch' => $branch, 'tag' => $tag],
$this->options
);

View File

@ -13,6 +13,7 @@ namespace Mage\Task\BuiltIn\Git;
use Symfony\Component\Process\Process;
use Mage\Task\AbstractTask;
use Mage\Task\Exception\SkipException;
/**
* Git Task - Pull
@ -34,6 +35,10 @@ class UpdateTask extends AbstractTask
public function execute(): bool
{
$options = $this->getOptions();
if ($options['tag']) {
throw new SkipException();
}
$command = $options['path'] . ' pull';
$process = $this->runtime->runLocalCommand($command);
@ -47,8 +52,10 @@ class UpdateTask extends AbstractTask
protected function getOptions(): array
{
$branch = $this->runtime->getEnvOption('branch', 'master');
$tag = $this->runtime->getEnvOption('tag', false);
$options = array_merge(
['path' => 'git', 'branch' => $branch],
['path' => 'git', 'branch' => $branch, 'tag' => $tag],
$this->options
);

View File

@ -37,6 +37,26 @@ class DeployCommandMiscTest extends TestCase
$this->assertEquals(0, $tester->getStatusCode());
}
public function testTagAndBranch()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/no-hosts.yml');
/** @var AbstractCommand $command */
$command = $application->find('deploy');
$this->assertTrue($command instanceof DeployCommand);
$tester = new CommandTester($command);
$tester->execute([
'command' => $command->getName(),
'environment' => 'test',
'--branch' => 'branch',
'--tag' => 'tag'
]);
$this->assertTrue(strpos($tester->getDisplay(), 'Branch and Tag options are mutually exclusive.') !== false);
$this->assertGreaterThan(0, $tester->getStatusCode());
}
public function testInvalidLog()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/invalid-log.yml');

View File

@ -69,6 +69,55 @@ class DeployCommandWithReleasesTest extends TestCase
$this->assertEquals(0, $tester->getStatusCode());
}
public function testDeploymentWithReleasesCommandsWithTag()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost.yml');
$application->getRuntime()->setReleaseId('20170101015120');
/** @var AbstractCommand $command */
$command = $application->find('deploy');
$this->assertTrue($command instanceof DeployCommand);
$tester = new CommandTester($command);
$tester->execute(['command' => $command->getName(), 'environment' => 'test', '--tag' => 'v1.0.0']);
$ranCommands = $application->getRuntime()->getRanCommands();
$testCase = array(
0 => 'git branch | grep "*"',
1 => 'git checkout v1.0.0',
2 => 'composer install --optimize-autoloader',
3 => 'composer dump-autoload --optimize',
4 => 'tar cfzp /tmp/mageXYZ --exclude=".git" --exclude="./var/cache/*" --exclude="./var/log/*" --exclude="./web/app_dev.php" ./',
5 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "mkdir -p /var/www/test/releases/1234567890"',
6 => 'scp -P 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no /tmp/mageXYZ tester@testhost:/var/www/test/releases/1234567890/mageXYZ',
7 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test/releases/1234567890 && tar xfzop mageXYZ"',
8 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm /var/www/test/releases/1234567890/mageXYZ"',
9 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test/releases/1234567890 && bin/console cache:warmup --env=dev"',
10 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test/releases/1234567890 && bin/console assets:install web --env=dev --symlink --relative"',
11 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test/releases/1234567890 && bin/console cache:pool:prune --env=dev"',
12 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "cd /var/www/test && ln -snf releases/1234567890 current"',
13 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "ls -1 /var/www/test/releases"',
14 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015110"',
15 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015111"',
16 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015112"',
17 => 'ssh -p 22 -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no tester@testhost "rm -rf /var/www/test/releases/20170101015113"',
18 => 'rm /tmp/mageXYZ',
19 => 'git checkout master',
);
// Check total of Executed Commands
$this->assertEquals(count($testCase), count($ranCommands));
// Check Generated Commands
foreach ($testCase as $index => $command) {
$this->assertEquals($command, $ranCommands[$index]);
}
$this->assertEquals(0, $tester->getStatusCode());
}
public function testDeploymentWithReleasesCommandsCustomSymlink()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-custom-symlink.yml');