You are here

public function Trigger::triggerBuildHookForEnvironment in Build Hooks 3.x

Same name and namespace in other branches
  1. 8.2 src/Trigger.php \Drupal\build_hooks\Trigger::triggerBuildHookForEnvironment()

Trigger a deployment for a frontend environment.

Parameters

\Drupal\build_hooks\Entity\FrontendEnvironmentInterface $frontendEnvironment: The frontend environment entity to trigger the deployment for.

Overrides TriggerInterface::triggerBuildHookForEnvironment

1 call to Trigger::triggerBuildHookForEnvironment()
Trigger::deployEnvironmentsByDeploymentStrategy in src/Trigger.php
Triggers all environments found by a specific deployment strategy.

File

src/Trigger.php, line 193

Class

Trigger
Defines a service for triggering deployments.

Namespace

Drupal\build_hooks

Code

public function triggerBuildHookForEnvironment(FrontendEnvironmentInterface $frontendEnvironment) {

  /** @var \Drupal\build_hooks\Plugin\FrontendEnvironmentInterface $plugin */
  $plugin = $frontendEnvironment
    ->getPlugin();
  $buildHookDetails = $plugin
    ->getBuildHookDetails();
  try {
    $result = $this
      ->triggerBuildHook($buildHookDetails, $frontendEnvironment);
    if (!$result
      ->shouldBuild()) {
      $reason = $result
        ->getReason();
      $this->messenger
        ->addWarning($reason);
      $this->logger
        ->get('build_hooks')
        ->notice(sprintf('Build was cancelled: %s', (string) $reason));
      return;
    }
    if ($plugin
      ->deploymentWasTriggered($result
      ->getResponse())) {

      // If the call was successful, set the latest deployment time
      // for this environment.
      $this->deployLogger
        ->setLastDeployTimeForEnvironment($frontendEnvironment);
      $this
        ->messenger()
        ->addMessage($this
        ->t('Deployment triggered for environment @env .', [
        '@env' => $frontendEnvironment
          ->label(),
      ]));
      $this
        ->invalidateToolbarCacheTag();
    }
    else {
      $this
        ->messenger()
        ->addWarning($result
        ->getResponse()
        ->getReasonPhrase());
    }
  } catch (GuzzleException $e) {
    $error = [
      'Failed to execute build hook for environment @env . Error message: <pre> @message </pre>',
      [
        '@message' => $e
          ->getMessage(),
        '@env' => $frontendEnvironment
          ->label(),
      ],
    ];
    $this
      ->messenger()
      ->addError($this
      ->t('Failed to execute build hook for environment @env . Error message: <pre> @message </pre>', $error[1]));
    $this->logger
      ->get('build_hooks')
      ->error($error[0], $error[1]);
  }
}