Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 3e321b9

Browse files
authored
Merge pull request #9 from fd6130/patch
Add CLI argument for tag or branch
2 parents 6fa3b79 + dee6371 commit 3e321b9

7 files changed

Lines changed: 30 additions & 5 deletions

File tree

doc/getting-started.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ but you can pass any stage name as the argument of the deploy/rollback commands:
2121
# deploy the current application to the "prod" server(s)
2222
$ ./bin/console deploy
2323

24+
# deploy the given branch or tag application to the "prod" server(s)
25+
$ ./bin/console deploy --branch-or-tag=main
26+
2427
# deploy the current application to the "staging" server(s)
2528
$ ./bin/console deploy staging
2629

src/Command/DeployCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ protected function configure()
4545
->setDescription('Deploys a Symfony application to one or more remote servers.')
4646
->setHelp('...')
4747
->addArgument('stage', InputArgument::OPTIONAL, 'The stage to deploy to ("production", "staging", etc.)', 'prod')
48+
->addArgument('branch-or-tag', InputArgument::OPTIONAL, 'Branch or tag you would like checked out')
4849
->addOption('configuration', 'c', InputOption::VALUE_REQUIRED, 'Load configuration from the given file path')
4950
->addOption('dry-run', null, InputOption::VALUE_NONE, 'Shows the commands to perform the deployment without actually executing them')
5051
;

src/Configuration/DefaultConfiguration.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ final class DefaultConfiguration extends AbstractConfiguration
3838
private $keepReleases = 5;
3939
private $repositoryUrl;
4040
private $repositoryBranch = 'master';
41+
private $passedBranchOrTag = false;
4142
private $remotePhpBinaryPath = 'php';
4243
private $updateRemoteComposerBinary = false;
4344
private $remoteComposerBinaryPath = '/usr/local/bin/composer';
@@ -66,11 +67,16 @@ final class DefaultConfiguration extends AbstractConfiguration
6667
private $sharedDirs = [];
6768
private $resetOpCacheFor;
6869

69-
public function __construct(string $localProjectDir)
70+
public function __construct(string $localProjectDir, ?string $branchOrTag = null)
7071
{
7172
parent::__construct();
7273
$this->localProjectDir = $localProjectDir;
7374
$this->setDefaultConfiguration(Kernel::MAJOR_VERSION);
75+
76+
if (!empty($branchOrTag)) {
77+
$this->repositoryBranch = $branchOrTag;
78+
$this->passedBranchOrTag = true;
79+
}
7480
}
7581

7682
// this proxy method is needed because the autocompletion breaks
@@ -127,7 +133,9 @@ public function repositoryUrl(string $url): self
127133

128134
public function repositoryBranch(string $branchName): self
129135
{
130-
$this->repositoryBranch = $branchName;
136+
if (false === $this->passedBranchOrTag) {
137+
$this->repositoryBranch = $branchName;
138+
}
131139

132140
return $this;
133141
}

src/Deployer/AbstractDeployer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ abstract class AbstractDeployer
3535
/** @var ConfigurationAdapter */
3636
private $config;
3737

38+
protected $branchOrTag;
39+
3840
abstract public function getRequirements(): array;
3941

4042
abstract public function deploy();
@@ -132,6 +134,7 @@ public function beforeFinishingRollback()
132134
public function initialize(Context $context): void
133135
{
134136
$this->context = $context;
137+
$this->branchOrTag = $context->getInput()->getArgument('branch-or-tag');
135138
$this->logger = new Logger($context);
136139
$this->taskRunner = new TaskRunner($this->context->isDryRun(), $this->logger);
137140
$this->log('<h1>Initializing configuration</>');
@@ -169,6 +172,11 @@ final protected function runLocal(string $command): TaskCompleted
169172
return $this->taskRunner->run($task)[0];
170173
}
171174

175+
final protected function getBranchOrTag(): ?string
176+
{
177+
return $this->branchOrTag;
178+
}
179+
172180
/**
173181
* @return TaskCompleted[]
174182
*/

src/Deployer/DefaultDeployer.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ abstract class DefaultDeployer extends AbstractDeployer
2727

2828
public function getConfigBuilder(): DefaultConfiguration
2929
{
30-
return new DefaultConfiguration($this->getContext()->getLocalProjectRootDir());
30+
return new DefaultConfiguration($this->getContext()->getLocalProjectRootDir(), $this->getBranchOrTag());
3131
}
3232

3333
public function getRequirements(): array
@@ -252,6 +252,11 @@ private function doGetcodeRevision(): string
252252
$this->log('<h2>Getting the revision ID of the code repository</>');
253253
$result = $this->runLocal(sprintf('git ls-remote %s %s', $this->getConfig(Option::repositoryUrl), $this->getConfig(Option::repositoryBranch)));
254254
$revision = explode("\t", $result->getTrimmedOutput())[0];
255+
256+
if (empty($revision)) {
257+
throw new InvalidConfigurationException(sprintf('No revisions found for %s', $this->getConfig(Option::repositoryBranch)));
258+
}
259+
255260
if ($this->getContext()->isDryRun()) {
256261
$revision = '(the code revision)';
257262
}

src/Server/ServerRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
class ServerRepository
1919
{
20-
/** @var Server[] $servers */
20+
/** @var Server[] */
2121
private $servers = [];
2222

2323
public function __toString(): string

src/Task/Task.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
class Task
1717
{
18-
/** @var Server[] $servers */
18+
/** @var Server[] */
1919
private $servers;
2020
private $shellCommand;
2121
private $envVars;

0 commit comments

Comments
 (0)