Skip to content

Commit

Permalink
Merge pull request #9 from fd6130/patch
Browse files Browse the repository at this point in the history
Add CLI argument for tag or branch
  • Loading branch information
fd6130 authored Nov 12, 2021
2 parents 6fa3b79 + dee6371 commit 3e321b9
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 5 deletions.
3 changes: 3 additions & 0 deletions doc/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ but you can pass any stage name as the argument of the deploy/rollback commands:
# deploy the current application to the "prod" server(s)
$ ./bin/console deploy

# deploy the given branch or tag application to the "prod" server(s)
$ ./bin/console deploy --branch-or-tag=main

# deploy the current application to the "staging" server(s)
$ ./bin/console deploy staging

Expand Down
1 change: 1 addition & 0 deletions src/Command/DeployCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
;
Expand Down
12 changes: 10 additions & 2 deletions src/Configuration/DefaultConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,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';
Expand Down Expand Up @@ -66,11 +67,16 @@ 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);

if (!empty($branchOrTag)) {
$this->repositoryBranch = $branchOrTag;
$this->passedBranchOrTag = true;
}
}

// this proxy method is needed because the autocompletion breaks
Expand Down Expand Up @@ -127,7 +133,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;
}
Expand Down
8 changes: 8 additions & 0 deletions src/Deployer/AbstractDeployer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ abstract class AbstractDeployer
/** @var ConfigurationAdapter */
private $config;

protected $branchOrTag;

abstract public function getRequirements(): array;

abstract public function deploy();
Expand Down Expand Up @@ -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('<h1>Initializing configuration</>');
Expand Down Expand Up @@ -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[]
*/
Expand Down
7 changes: 6 additions & 1 deletion src/Deployer/DefaultDeployer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -252,6 +252,11 @@ private function doGetcodeRevision(): string
$this->log('<h2>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)';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Server/ServerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
class ServerRepository
{
/** @var Server[] $servers */
/** @var Server[] */
private $servers = [];

public function __toString(): string
Expand Down
2 changes: 1 addition & 1 deletion src/Task/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class Task
{
/** @var Server[] $servers */
/** @var Server[] */
private $servers;
private $shellCommand;
private $envVars;
Expand Down

0 comments on commit 3e321b9

Please sign in to comment.