Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/CraftStaticDusk.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use todaydesign\craftstaticdusk\services\CraftStaticDuskService as CraftStaticDuskServiceService;
use todaydesign\craftstaticdusk\variables\CraftStaticDuskVariable;
use todaydesign\craftstaticdusk\jobs\StaticBuildIncremental as StaticBuildIncrementalJob;
use todaydesign\craftstaticdusk\models\Settings;

use Craft;
Expand All @@ -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;

Expand Down Expand Up @@ -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) {

$element = $event->element;

$isNotDraft = ElementHelper::isDraftOrRevision($element);
$isEntry = $element instanceof craft\elements\Entry;

if ($isEntry && $isNotDraft) {

$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,
]));

}
}
);

// Register services as components
$this->setComponents([
'buildstatic' => CraftStaticDuskService::class,
Expand Down
96 changes: 96 additions & 0 deletions src/jobs/StaticBuildIncremental.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/**
* Craft Static Dusk plugin for Craft CMS 3.x
*
* Static site builder for Craft and AWS Code Pipelines
*
* @link https://today.design
* @copyright Copyright (c) 2020 Jason D'Souza
*/

namespace todaydesign\craftstaticdusk\jobs;

use todaydesign\craftstaticdusk\CraftStaticDusk;
use todaydesign\craftstaticdusk\managers\StaticBuildManager;

use Craft;
use craft\queue\BaseJob;

/**
* StaticBuild job
*
* Jobs are run in separate process via a Queue of pending jobs. This allows
* you to spin lengthy processing off into a separate PHP process that does not
* block the main process.
*
* You can use it like this:
*
* use todaydesign\craftstaticdusk\jobs\StaticBuildIncremental as StaticBuildIncrementalJob;
*
* $queue = Craft::$app->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');
}
}
77 changes: 76 additions & 1 deletion src/managers/StaticBuildManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function getScheduledBuilds($site)


/**
* Get scheduled static builds for this environment
* Get build history for this environment
*
* @return mixed
*/
Expand Down Expand Up @@ -107,4 +107,79 @@ public function getBuildHistory($site)
}


public function launchIncrementalBuild($siteHandle, $pageUri)
{

$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' => $siteHandle,
'incrementalBuildUri' => $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'
);
}

}
18 changes: 3 additions & 15 deletions src/variables/CraftStaticDuskVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}