From 3ece351977d9f453e7b5c33c2146d7d4da262cea Mon Sep 17 00:00:00 2001 From: Matt Di Florio Date: Fri, 5 Aug 2022 16:56:49 +1000 Subject: [PATCH 1/2] [FEAT] DEVOPS-360: Launch incremental static build on save entry --- src/CraftStaticDusk.php | 31 +++++++++ src/managers/StaticBuildManager.php | 77 ++++++++++++++++++++++- src/variables/CraftStaticDuskVariable.php | 18 +----- 3 files changed, 110 insertions(+), 16 deletions(-) diff --git a/src/CraftStaticDusk.php b/src/CraftStaticDusk.php index 07dcbd2..6e5c769 100644 --- a/src/CraftStaticDusk.php +++ b/src/CraftStaticDusk.php @@ -11,6 +11,7 @@ namespace todaydesign\craftstaticdusk; use todaydesign\craftstaticdusk\services\CraftStaticDuskService as CraftStaticDuskServiceService; +use todaydesign\craftstaticdusk\managers\StaticBuildManager; use todaydesign\craftstaticdusk\variables\CraftStaticDuskVariable; use todaydesign\craftstaticdusk\models\Settings; @@ -21,6 +22,11 @@ use craft\web\UrlManager; use craft\web\twig\variables\CraftVariable; use craft\events\RegisterUrlRulesEvent; +use craft\helpers\ElementHelper; +use craft\services\Elements; + +use craft\helpers\FileHelper; + use yii\base\Event; @@ -133,6 +139,31 @@ function (PluginEvent $event) { } ); + + // Run incremental static build on save + Event::on( + Elements::class, + Elements::EVENT_AFTER_SAVE_ELEMENT, + function (Event $event) { + $staticBuildManager = new StaticBuildManager(); + + $element = $event->element; + + $isNotDraft = ElementHelper::isDraftOrRevision($element); + $isEntry = $element instanceof craft\elements\Entry; + + if ($isEntry && $isNotDraft) { + + $payload = [ + "site" => $element->siteId, + "pageUri" => $element->uri + ]; + + $staticBuildManager->launchIncrementalBuild($payload); + } + } + ); + // Register services as components $this->setComponents([ 'buildstatic' => CraftStaticDuskService::class, diff --git a/src/managers/StaticBuildManager.php b/src/managers/StaticBuildManager.php index 89929d2..6e314af 100644 --- a/src/managers/StaticBuildManager.php +++ b/src/managers/StaticBuildManager.php @@ -62,7 +62,7 @@ public function getScheduledBuilds($site) /** - * Get scheduled static builds for this environment + * Get build history for this environment * * @return mixed */ @@ -107,4 +107,79 @@ public function getBuildHistory($site) } + public function launchIncrementalBuild($payload) + { + + $isMissingEnvVariables = $this->isMissingEnvVariables(); + + if ($isMissingEnvVariables) { + return; + } + + $settings = CraftStaticDusk::$plugin->getSettings(); + + $curl = curl_init(); + + $payload = [ + 'secret' => Craft::parseEnv($settings->webHookSecret) + ]; + + if (Craft::parseEnv($settings->webHookType) === 'GH') { + $payload = array_merge($payload, [ + 'repo' => Craft::parseEnv($settings->gitRepo), + 'ref' => Craft::parseEnv($settings->gitRef), + 'envName' => Craft::parseEnv($settings->environmentName), + 'site' => $payload["site"], + 'incrementalBuildUri' => $payload["pageUri"], + ]); + } + + curl_setopt_array($curl, array( + CURLOPT_URL => Craft::parseEnv($settings->webHookUrl), +// CURLOPT_URL => "http://host.docker.internal:3000/static-build", + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => "", + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => "POST", + CURLOPT_VERBOSE => true, + CURLOPT_POSTFIELDS => json_encode((object)$payload), + CURLOPT_HTTPHEADER => array( + "Content-Type: application/json" + ), + )); + + $response = curl_exec($curl); + + curl_close($curl); + + } + + + /** + * Check if Environment variables are missing + * + * @return boolean + */ + public function isMissingEnvVariables() + { + $settings = CraftStaticDusk::$plugin->getSettings(); + + $webHookSecret = Craft::parseEnv($settings->webHookSecret); + $gitRepo = Craft::parseEnv($settings->gitRepo); + $gitRef = Craft::parseEnv($settings->gitRef); + $environmentName = Craft::parseEnv($settings->environmentName); + $webHookUrl = Craft::parseEnv($settings->webHookUrl); + + return ( + empty($webHookSecret) || $webHookSecret === '$STATIC_BUILD_WEBHOOK_SECRET' || + empty($gitRepo) || $gitRepo === '$STATIC_BUILD_GIT_REPO' || + empty($gitRef) || $gitRef === '$STATIC_BUILD_GIT_REF' || + empty($environmentName) || $environmentName === '$STATIC_BUILD_WEBHOOK_URL' || + empty($webHookUrl) || $webHookUrl === '$STATIC_BUILD_WEBHOOK_URL' + ); + } + } \ No newline at end of file diff --git a/src/variables/CraftStaticDuskVariable.php b/src/variables/CraftStaticDuskVariable.php index 230f3f2..8475abc 100644 --- a/src/variables/CraftStaticDuskVariable.php +++ b/src/variables/CraftStaticDuskVariable.php @@ -64,21 +64,9 @@ public function getBuildHistory($site) */ public function isMissingEnvVariables() { - $settings = CraftStaticDusk::$plugin->getSettings(); - - $webHookSecret = Craft::parseEnv($settings->webHookSecret); - $gitRepo = Craft::parseEnv($settings->gitRepo); - $gitRef = Craft::parseEnv($settings->gitRef); - $environmentName = Craft::parseEnv($settings->environmentName); - $webHookUrl = Craft::parseEnv($settings->webHookUrl); - - return ( - empty($webHookSecret) || $webHookSecret === '$STATIC_BUILD_WEBHOOK_SECRET' || - empty($gitRepo) || $gitRepo === '$STATIC_BUILD_GIT_REPO' || - empty($gitRef) || $gitRef === '$STATIC_BUILD_GIT_REF' || - empty($environmentName) || $environmentName === '$STATIC_BUILD_WEBHOOK_URL' || - empty($webHookUrl) || $webHookUrl === '$STATIC_BUILD_WEBHOOK_URL' - ); + $manager = new StaticBuildManager(); + $result = $manager->isMissingEnvVariables(); + return $result; } } From 3842bf4835bc474440dfe19b5414756d3ba95259 Mon Sep 17 00:00:00 2001 From: Matt Di Florio Date: Mon, 8 Aug 2022 14:28:20 +1000 Subject: [PATCH 2/2] [FEAT] DEVOPS-360: Move incremental build to craft queue so that save doesn't break --- src/CraftStaticDusk.php | 14 ++--- src/jobs/StaticBuildIncremental.php | 96 +++++++++++++++++++++++++++++ src/managers/StaticBuildManager.php | 6 +- 3 files changed, 106 insertions(+), 10 deletions(-) create mode 100644 src/jobs/StaticBuildIncremental.php diff --git a/src/CraftStaticDusk.php b/src/CraftStaticDusk.php index 6e5c769..e14ccb8 100644 --- a/src/CraftStaticDusk.php +++ b/src/CraftStaticDusk.php @@ -11,8 +11,8 @@ namespace todaydesign\craftstaticdusk; use todaydesign\craftstaticdusk\services\CraftStaticDuskService as CraftStaticDuskServiceService; -use todaydesign\craftstaticdusk\managers\StaticBuildManager; use todaydesign\craftstaticdusk\variables\CraftStaticDuskVariable; +use todaydesign\craftstaticdusk\jobs\StaticBuildIncremental as StaticBuildIncrementalJob; use todaydesign\craftstaticdusk\models\Settings; use Craft; @@ -145,7 +145,6 @@ function (PluginEvent $event) { Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, function (Event $event) { - $staticBuildManager = new StaticBuildManager(); $element = $event->element; @@ -154,12 +153,13 @@ function (Event $event) { if ($isEntry && $isNotDraft) { - $payload = [ - "site" => $element->siteId, - "pageUri" => $element->uri - ]; + $queue = Craft::$app->getQueue(); + $jobId = $queue->push(new StaticBuildIncrementalJob([ + 'description' => Craft::t('craft-static-dusk', 'Running incremental static build on page "' . $element->uri . '"'), + 'siteHandle' => Craft::$app->getSites()->currentSite->handle, + 'pageUri' => $element->uri, + ])); - $staticBuildManager->launchIncrementalBuild($payload); } } ); diff --git a/src/jobs/StaticBuildIncremental.php b/src/jobs/StaticBuildIncremental.php new file mode 100644 index 0000000..47b34ac --- /dev/null +++ b/src/jobs/StaticBuildIncremental.php @@ -0,0 +1,96 @@ +getQueue(); + * $jobId = $queue->push(new StaticBuildIncrementalJob([ + * 'description' => Craft::t('craft-static-dusk', 'This overrides the default description'), + * 'someAttribute' => 'someValue', + * ])); + * + * The key/value pairs that you pass in to the job will set the public properties + * for that object. Thus whatever you set 'someAttribute' to will cause the + * public property $someAttribute to be set in the job. + * + * Passing in 'description' is optional, and only if you want to override the default + * description. + * + * More info: https://github.com/yiisoft/yii2-queue + * + * @author Jason D'Souza + * @package CraftStaticDusk + * @since 1.0.1 + */ +class StaticBuildIncremental extends BaseJob +{ + // Public Properties + // ========================================================================= + + /** + * Site handle to build + * + * @var string + */ + public $siteHandle = ''; + + /** + * Page URI to build + * + * @var string + */ + public $pageUri = ''; + + // Public Methods + // ========================================================================= + + /** + * When the Queue is ready to run your job, it will call this method. + * You don't need any steps or any other special logic handling, just do the + * jobs that needs to be done here. + * + * More info: https://github.com/yiisoft/yii2-queue + */ + public function execute($queue) + { + $manager = new StaticBuildManager(); + $manager->launchIncrementalBuild($this->siteHandle, $this->pageUri); + } + + // Protected Methods + // ========================================================================= + + /** + * Returns a default description for [[getDescription()]], if [[description]] isn’t set. + * + * @return string The default task description + */ + protected function defaultDescription(): string + { + return Craft::t('craft-static-dusk', 'Incremental Static build'); + } +} diff --git a/src/managers/StaticBuildManager.php b/src/managers/StaticBuildManager.php index 6e314af..b6d9d36 100644 --- a/src/managers/StaticBuildManager.php +++ b/src/managers/StaticBuildManager.php @@ -107,7 +107,7 @@ public function getBuildHistory($site) } - public function launchIncrementalBuild($payload) + public function launchIncrementalBuild($siteHandle, $pageUri) { $isMissingEnvVariables = $this->isMissingEnvVariables(); @@ -129,8 +129,8 @@ public function launchIncrementalBuild($payload) 'repo' => Craft::parseEnv($settings->gitRepo), 'ref' => Craft::parseEnv($settings->gitRef), 'envName' => Craft::parseEnv($settings->environmentName), - 'site' => $payload["site"], - 'incrementalBuildUri' => $payload["pageUri"], + 'site' => $siteHandle, + 'incrementalBuildUri' => $pageUri, ]); }