Skip to content

Commit fc25874

Browse files
committed
feat: add rollback command
1 parent bd8aa3c commit fc25874

13 files changed

+374
-35
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"ext-pcntl": "*",
1717
"ext-zip": "*",
1818
"guzzlehttp/guzzle": "^7.0",
19+
"nesbot/carbon": "^2.40",
1920
"symfony/config": "^4.4",
2021
"symfony/console": "^4.4",
2122
"symfony/dependency-injection": "^4.4",

composer.lock

Lines changed: 217 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/services.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,10 @@ services:
7373
$deploymentSteps:
7474
- '@Ymir\Cli\Deployment\StartAndMonitorDeploymentStep'
7575

76+
Ymir\Cli\Command\Project\RollbackProjectCommand:
77+
arguments:
78+
$deploymentSteps:
79+
- '@Ymir\Cli\Deployment\StartAndMonitorDeploymentStep'
80+
7681
Symfony\Component\Filesystem\:
7782
resource: '../vendor/symfony/filesystem'

src/ApiClient.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,16 @@ public function createRedeployment(int $projectId, string $environment): Collect
181181
return $this->request('post', "/projects/{$projectId}/environments/{$environment}/redeployments");
182182
}
183183

184+
/**
185+
* Create a new deployment to rollback to the given deployment.
186+
*/
187+
public function createRollback(int $projectId, string $environment, int $deploymentId): Collection
188+
{
189+
return $this->request('post', "/projects/{$projectId}/environments/{$environment}/rollbacks", [
190+
'deployment' => $deploymentId,
191+
]);
192+
}
193+
184194
/**
185195
* Create a new team with the given name.
186196
*/
@@ -341,6 +351,14 @@ public function getDeployment(int $deploymentId): Collection
341351
return $this->request('get', "/deployments/{$deploymentId}");
342352
}
343353

354+
/**
355+
* Get all the deployments for the given project on the given environment.
356+
*/
357+
public function getDeployments(int $projectId, string $environment): Collection
358+
{
359+
return $this->request('get', "/projects/{$projectId}/environments/{$environment}/deployments");
360+
}
361+
344362
/**
345363
* Get the DNS records belonging to the given DNS zone.
346364
*/

src/Command/Project/AbstractProjectDeploymentCommand.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Ymir\Cli\Command\Project;
1515

1616
use Symfony\Component\Console\Input\InputInterface;
17+
use Tightenco\Collect\Support\Collection;
1718
use Ymir\Cli\ApiClient;
1819
use Ymir\Cli\CliConfiguration;
1920
use Ymir\Cli\Command\AbstractProjectCommand;
@@ -48,7 +49,7 @@ public function __construct(ApiClient $apiClient, CliConfiguration $cliConfigura
4849
/**
4950
* Create the deployment and return its ID.
5051
*/
51-
abstract protected function createDeployment(InputInterface $input, OutputStyle $output): int;
52+
abstract protected function createDeployment(InputInterface $input, OutputStyle $output): Collection;
5253

5354
/**
5455
* Get the message to display when a deployment was successful.
@@ -60,17 +61,17 @@ abstract protected function getSuccessMessage(string $environment): string;
6061
*/
6162
protected function perform(InputInterface $input, OutputStyle $output)
6263
{
63-
$deploymentId = $this->createDeployment($input, $output);
64+
$deployment = $this->createDeployment($input, $output);
6465
$environment = $this->getStringArgument($input, 'environment');
6566
$projectId = $this->projectConfiguration->getProjectId();
6667

6768
foreach ($this->deploymentSteps as $deploymentStep) {
68-
$deploymentStep->perform($deploymentId, $output);
69+
$deploymentStep->perform($deployment, $output);
6970
}
7071

7172
$output->info($this->getSuccessMessage($environment));
7273

73-
$unmanagedDomains = (array) $this->apiClient->getDeployment($deploymentId)->get('unmanaged_domains');
74+
$unmanagedDomains = (array) $this->apiClient->getDeployment($deployment->get('id'))->get('unmanaged_domains');
7475
$vanityDomainName = $this->apiClient->getEnvironmentVanityDomainName($projectId, $environment);
7576

7677
$this->displayEnvironmentUrlAndCopyToClipboard($output, $vanityDomainName);

src/Command/Project/DeployProjectCommand.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Console\Exception\RuntimeException;
1717
use Symfony\Component\Console\Input\InputArgument;
1818
use Symfony\Component\Console\Input\InputInterface;
19+
use Tightenco\Collect\Support\Collection;
1920
use Ymir\Cli\Console\OutputStyle;
2021

2122
class DeployProjectCommand extends AbstractProjectDeploymentCommand
@@ -42,21 +43,21 @@ protected function configure()
4243
/**
4344
* {@inheritdoc}
4445
*/
45-
protected function createDeployment(InputInterface $input, OutputStyle $output): int
46+
protected function createDeployment(InputInterface $input, OutputStyle $output): Collection
4647
{
4748
$environment = $this->getStringArgument($input, 'environment');
4849
$projectId = $this->projectConfiguration->getProjectId();
4950

5051
$this->invoke($output, ValidateProjectCommand::NAME, ['environments' => $environment]);
5152
$this->invoke($output, BuildProjectCommand::NAME, ['environment' => $environment]);
5253

53-
$deploymentId = (int) $this->apiClient->createDeployment($projectId, $environment, $this->projectConfiguration)->get('id');
54+
$deployment = $this->apiClient->createDeployment($projectId, $environment, $this->projectConfiguration);
5455

55-
if (empty($deploymentId)) {
56+
if (!$deployment->has('id')) {
5657
throw new RuntimeException('There was an error creating the deployment');
5758
}
5859

59-
return $deploymentId;
60+
return $deployment;
6061
}
6162

6263
/**

src/Command/Project/RedeployProjectCommand.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Console\Exception\RuntimeException;
1717
use Symfony\Component\Console\Input\InputArgument;
1818
use Symfony\Component\Console\Input\InputInterface;
19+
use Tightenco\Collect\Support\Collection;
1920
use Ymir\Cli\Console\OutputStyle;
2021

2122
class RedeployProjectCommand extends AbstractProjectDeploymentCommand
@@ -42,15 +43,15 @@ protected function configure()
4243
/**
4344
* {@inheritdoc}
4445
*/
45-
protected function createDeployment(InputInterface $input, OutputStyle $output): int
46+
protected function createDeployment(InputInterface $input, OutputStyle $output): Collection
4647
{
47-
$deploymentId = (int) $this->apiClient->createRedeployment($this->projectConfiguration->getProjectId(), $this->getStringArgument($input, 'environment'))->get('id');
48+
$redeployment = $this->apiClient->createRedeployment($this->projectConfiguration->getProjectId(), $this->getStringArgument($input, 'environment'));
4849

49-
if (empty($deploymentId)) {
50+
if (!$redeployment->has('id')) {
5051
throw new RuntimeException('There was an error creating the redeployment');
5152
}
5253

53-
return $deploymentId;
54+
return $redeployment;
5455
}
5556

5657
/**

0 commit comments

Comments
 (0)