Skip to content

Commit

Permalink
Merge pull request #48 from ByteInternet/deploy_runner_return_exit_code
Browse files Browse the repository at this point in the history
DeployRunner: return deployer exit code for runStage
  • Loading branch information
tdgroot authored Sep 19, 2022
2 parents 22e9514 + f9461a8 commit dc6f374
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 34 deletions.
3 changes: 1 addition & 2 deletions src/Command/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->deployRunner->run($output, 'build', 'build');
return 0;
return $this->deployRunner->run($output, 'build', 'build');
}
}
3 changes: 1 addition & 2 deletions src/Command/ComposerAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->deployRunner->run($output, 'build', 'deploy:vendors:auth');
return 0;
return $this->deployRunner->run($output, 'build', 'deploy:vendors:auth');
}
}
3 changes: 1 addition & 2 deletions src/Command/Deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->deployRunner->run($output, $input->getArgument('stage'), 'deploy');
return 0;
return $this->deployRunner->run($output, $input->getArgument('stage'), 'deploy');
}
}
3 changes: 1 addition & 2 deletions src/Command/RunTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->deployRunner->run($output, $input->getArgument('stage'), $input->getArgument('task'));
return 0;
return $this->deployRunner->run($output, $input->getArgument('stage'), $input->getArgument('task'));
}
}
52 changes: 26 additions & 26 deletions src/DeployRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Deployer\Deployer;
use Deployer\Exception\Exception;
use Deployer\Exception\GracefulShutdownException;
use Deployer\Host\Host;
use Hypernode\Deploy\Console\Output\OutputWatcher;
use Hypernode\Deploy\Deployer\RecipeLoader;
use Hypernode\Deploy\Exception\InvalidConfigurationException;
Expand Down Expand Up @@ -67,10 +68,8 @@ public function __construct(
* @throws GracefulShutdownException
* @throws Throwable
* @throws Exception
*
* @return void
*/
public function run(OutputInterface $output, string $stage, string $task = 'deploy')
public function run(OutputInterface $output, string $stage, string $task = 'deploy'): int
{
$console = new Application();
$deployer = new Deployer($console);
Expand All @@ -87,9 +86,10 @@ public function run(OutputInterface $output, string $stage, string $task = 'depl
$this->initializeDeployer($deployer);
} catch (InvalidConfigurationException $e) {
$output->write($e->getMessage());
return;
return 1;
}
$this->runStage($deployer, $stage, $task);

return $this->runStage($deployer, $stage, $task);
}

/**
Expand Down Expand Up @@ -271,7 +271,7 @@ private function initializeBuildStage(Configuration $config): void
* @throws Throwable
* @throws Exception
*/
private function runStage(Deployer $deployer, string $stage, string $task = 'deploy'): void
private function runStage(Deployer $deployer, string $stage, string $task = 'deploy'): int
{
$hosts = $deployer->selector->select("stage=$stage");
if (empty($hosts)) {
Expand All @@ -281,30 +281,30 @@ private function runStage(Deployer $deployer, string $stage, string $task = 'dep
$tasks = $deployer->scriptManager->getTasks($task);
$executor = $deployer->master;

try {
/**
* Set the env variable to tell deployer to deploy the hosts sequentially instead of parallel.
* @see \Deployer\Executor\Master::runTask()
*/
putenv('DEPLOYER_LOCAL_WORKER=true');
$executor->run($tasks, $hosts);
} catch (Throwable $exception) {
$deployer->output->writeln('[' . \get_class($exception) . '] ' . $exception->getMessage());
$deployer->output->writeln($exception->getTraceAsString());
/**
* Set the env variable to tell deployer to deploy the hosts sequentially instead of parallel.
* @see \Deployer\Executor\Master::runTask()
*/
putenv('DEPLOYER_LOCAL_WORKER=true');
$exitCode = $executor->run($tasks, $hosts);

if ($exception instanceof GracefulShutdownException) {
throw $exception;
}
if ($exitCode === 0) {
return 0;
}

// Check if we have tasks to execute on failure
if ($deployer['fail']->has($task)) {
$taskName = $deployer['fail']->get($task);
$tasks = $deployer->scriptManager->getTasks($taskName);
if ($exitCode === GracefulShutdownException::EXIT_CODE) {
return 1;
}

$executor->run($tasks, $hosts);
}
throw $exception;
// Check if we have tasks to execute on failure
if ($deployer['fail']->has($task)) {
$taskName = $deployer['fail']->get($task);
$tasks = $deployer->scriptManager->getTasks($taskName);

$executor->run($tasks, $hosts);
}

return $exitCode;
}

private function tryGetConfiguration(): Configuration
Expand Down

0 comments on commit dc6f374

Please sign in to comment.