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..95a8e24 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 = null) { 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 (false === $this->passedBranchOrTag) { + $this->repositoryBranch = $branchName; + } return $this; } diff --git a/src/Deployer/AbstractDeployer.php b/src/Deployer/AbstractDeployer.php index fbc7c64..5c3dd21 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'); @@ -169,6 +172,11 @@ final protected function runLocal(string $command): TaskCompleted return $this->taskRunner->run($task)[0]; } + final protected function getBranchOrTag(): ?string + { + return $this->branchOrTag; + } + /** * @return TaskCompleted[] */ diff --git a/src/Deployer/DefaultDeployer.php b/src/Deployer/DefaultDeployer.php index fa28adc..dd667b9 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)'; } 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;