From 338190150be91461a3d3a46480dc2d2a6d918147 Mon Sep 17 00:00:00 2001 From: Drew Brown Date: Wed, 19 Feb 2020 16:53:55 -0500 Subject: [PATCH 1/4] * Added tag-or-branch argument in DeployCommand * Implemented new argument in DefaultDeployer to override any configured branch set in the config. Will still default to master if nothing is passed or set. --- src/Command/DeployCommand.php | 1 + src/Configuration/DefaultConfiguration.php | 11 +++++++++-- src/Deployer/AbstractDeployer.php | 10 ++++++++++ src/Deployer/DefaultDeployer.php | 7 ++++++- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Command/DeployCommand.php b/src/Command/DeployCommand.php index a3c6a5c..ce2fe0a 100644 --- a/src/Command/DeployCommand.php +++ b/src/Command/DeployCommand.php @@ -45,6 +45,7 @@ protected function configure() ->setDescription('Deploys a Symfony application to one or more remote servers.') ->setHelp('...') ->addArgument('stage', InputArgument::OPTIONAL, 'The stage to deploy to ("production", "staging", etc.)', 'prod') + ->addArgument('branch-or-tag', InputArgument::OPTIONAL, 'Branch or tag you would like checked out') ->addOption('configuration', 'c', InputOption::VALUE_REQUIRED, 'Load configuration from the given file path') ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Shows the commands to perform the deployment without actually executing them') ; diff --git a/src/Configuration/DefaultConfiguration.php b/src/Configuration/DefaultConfiguration.php index 1abe5ac..ca31e20 100644 --- a/src/Configuration/DefaultConfiguration.php +++ b/src/Configuration/DefaultConfiguration.php @@ -33,6 +33,7 @@ final class DefaultConfiguration extends AbstractConfiguration private $keepReleases = 5; private $repositoryUrl; private $repositoryBranch = 'master'; + private $passedBranchOrTag = false; private $remotePhpBinaryPath = 'php'; private $updateRemoteComposerBinary = false; private $remoteComposerBinaryPath = '/usr/local/bin/composer'; @@ -61,11 +62,15 @@ final class DefaultConfiguration extends AbstractConfiguration private $sharedDirs = []; private $resetOpCacheFor; - public function __construct(string $localProjectDir) + public function __construct(string $localProjectDir, ?string $branchOrTag) { parent::__construct(); $this->localProjectDir = $localProjectDir; $this->setDefaultConfiguration(Kernel::MAJOR_VERSION, Kernel::MINOR_VERSION); + if (!empty($branchOrTag)) { + $this->repositoryBranch = $branchOrTag; + $this->passedBranchOrTag = true; + } } // this proxy method is needed because the autocompletion breaks @@ -122,7 +127,9 @@ public function repositoryUrl(string $url): self public function repositoryBranch(string $branchName): self { - $this->repositoryBranch = $branchName; + if ($this->passedBranchOrTag === false) { + $this->repositoryBranch = $branchName; + } return $this; } diff --git a/src/Deployer/AbstractDeployer.php b/src/Deployer/AbstractDeployer.php index fbc7c64..aafbec8 100644 --- a/src/Deployer/AbstractDeployer.php +++ b/src/Deployer/AbstractDeployer.php @@ -35,6 +35,8 @@ abstract class AbstractDeployer /** @var ConfigurationAdapter */ private $config; + protected $branchOrTag; + abstract public function getRequirements(): array; abstract public function deploy(); @@ -132,6 +134,7 @@ public function beforeFinishingRollback() public function initialize(Context $context): void { $this->context = $context; + $this->branchOrTag = $context->getInput()->getArgument('branch-or-tag'); $this->logger = new Logger($context); $this->taskRunner = new TaskRunner($this->context->isDryRun(), $this->logger); $this->log('

Initializing configuration'); @@ -141,6 +144,7 @@ public function initialize(Context $context): void $this->log($this->config); $this->log('

Checking technical requirements'); $this->checkRequirements(); + } abstract protected function getConfigBuilder(); @@ -169,6 +173,11 @@ final protected function runLocal(string $command): TaskCompleted return $this->taskRunner->run($task)[0]; } + final protected function getBranchOrTag(): string + { + return $this->branchOrTag; + } + /** * @return TaskCompleted[] */ @@ -231,4 +240,5 @@ private function checkRequirements(): void $this->log($requirement->getMessage()); } } + } diff --git a/src/Deployer/DefaultDeployer.php b/src/Deployer/DefaultDeployer.php index fa28adc..0e02e4b 100644 --- a/src/Deployer/DefaultDeployer.php +++ b/src/Deployer/DefaultDeployer.php @@ -27,7 +27,7 @@ abstract class DefaultDeployer extends AbstractDeployer public function getConfigBuilder(): DefaultConfiguration { - return new DefaultConfiguration($this->getContext()->getLocalProjectRootDir()); + return new DefaultConfiguration($this->getContext()->getLocalProjectRootDir(), $this->getBranchOrTag()); } public function getRequirements(): array @@ -245,6 +245,11 @@ private function doGetcodeRevision(): string $this->log('

Getting the revision ID of the code repository'); $result = $this->runLocal(sprintf('git ls-remote %s %s', $this->getConfig(Option::repositoryUrl), $this->getConfig(Option::repositoryBranch))); $revision = explode("\t", $result->getTrimmedOutput())[0]; + + if (empty($revision)) { + throw new InvalidConfigurationException(sprintf("No revisions found for %s", $this->getConfig(Option::repositoryBranch))); + } + if ($this->getContext()->isDryRun()) { $revision = '(the code revision)'; } From 4f205e891bc98f8b278e347f29ae0426f1210a09 Mon Sep 17 00:00:00 2001 From: Drew Brown Date: Wed, 19 Feb 2020 17:09:19 -0500 Subject: [PATCH 2/4] * Added null to possible return type --- src/Deployer/AbstractDeployer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Deployer/AbstractDeployer.php b/src/Deployer/AbstractDeployer.php index aafbec8..0383d4d 100644 --- a/src/Deployer/AbstractDeployer.php +++ b/src/Deployer/AbstractDeployer.php @@ -173,7 +173,7 @@ final protected function runLocal(string $command): TaskCompleted return $this->taskRunner->run($task)[0]; } - final protected function getBranchOrTag(): string + final protected function getBranchOrTag(): ?string { return $this->branchOrTag; } From ddfb5e7bf4c7ce2d5382ee90cb0c544717bb3514 Mon Sep 17 00:00:00 2001 From: Drew Brown Date: Wed, 22 Jul 2020 12:05:59 -0400 Subject: [PATCH 3/4] * Added default value for branchOrTag argument --- src/Configuration/DefaultConfiguration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Configuration/DefaultConfiguration.php b/src/Configuration/DefaultConfiguration.php index ca31e20..114c6b0 100644 --- a/src/Configuration/DefaultConfiguration.php +++ b/src/Configuration/DefaultConfiguration.php @@ -62,7 +62,7 @@ final class DefaultConfiguration extends AbstractConfiguration private $sharedDirs = []; private $resetOpCacheFor; - public function __construct(string $localProjectDir, ?string $branchOrTag) + public function __construct(string $localProjectDir, ?string $branchOrTag = null) { parent::__construct(); $this->localProjectDir = $localProjectDir; From b3e4b605d9bdaa84b6fcbe17b4b2f5bf43b7f3fb Mon Sep 17 00:00:00 2001 From: Drew Brown Date: Wed, 22 Jul 2020 12:30:51 -0400 Subject: [PATCH 4/4] * Added code style fixes --- src/Configuration/DefaultConfiguration.php | 2 +- src/Deployer/AbstractDeployer.php | 2 -- src/Deployer/DefaultDeployer.php | 2 +- src/Server/ServerRepository.php | 2 +- src/Task/Task.php | 2 +- 5 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Configuration/DefaultConfiguration.php b/src/Configuration/DefaultConfiguration.php index 114c6b0..95a8e24 100644 --- a/src/Configuration/DefaultConfiguration.php +++ b/src/Configuration/DefaultConfiguration.php @@ -127,7 +127,7 @@ public function repositoryUrl(string $url): self public function repositoryBranch(string $branchName): self { - if ($this->passedBranchOrTag === false) { + if (false === $this->passedBranchOrTag) { $this->repositoryBranch = $branchName; } diff --git a/src/Deployer/AbstractDeployer.php b/src/Deployer/AbstractDeployer.php index 0383d4d..5c3dd21 100644 --- a/src/Deployer/AbstractDeployer.php +++ b/src/Deployer/AbstractDeployer.php @@ -144,7 +144,6 @@ public function initialize(Context $context): void $this->log($this->config); $this->log('

Checking technical requirements'); $this->checkRequirements(); - } abstract protected function getConfigBuilder(); @@ -240,5 +239,4 @@ private function checkRequirements(): void $this->log($requirement->getMessage()); } } - } diff --git a/src/Deployer/DefaultDeployer.php b/src/Deployer/DefaultDeployer.php index 0e02e4b..dd667b9 100644 --- a/src/Deployer/DefaultDeployer.php +++ b/src/Deployer/DefaultDeployer.php @@ -247,7 +247,7 @@ private function doGetcodeRevision(): string $revision = explode("\t", $result->getTrimmedOutput())[0]; if (empty($revision)) { - throw new InvalidConfigurationException(sprintf("No revisions found for %s", $this->getConfig(Option::repositoryBranch))); + throw new InvalidConfigurationException(sprintf('No revisions found for %s', $this->getConfig(Option::repositoryBranch))); } if ($this->getContext()->isDryRun()) { diff --git a/src/Server/ServerRepository.php b/src/Server/ServerRepository.php index d0c9b21..0cb5f94 100644 --- a/src/Server/ServerRepository.php +++ b/src/Server/ServerRepository.php @@ -17,7 +17,7 @@ */ class ServerRepository { - /** @var Server[] $servers */ + /** @var Server[] */ private $servers = []; public function __toString(): string diff --git a/src/Task/Task.php b/src/Task/Task.php index 007da0a..18a278f 100644 --- a/src/Task/Task.php +++ b/src/Task/Task.php @@ -15,7 +15,7 @@ class Task { - /** @var Server[] $servers */ + /** @var Server[] */ private $servers; private $shellCommand; private $envVars;