From 80c8db4b93efd7f194168d7e9490bb6272dbe042 Mon Sep 17 00:00:00 2001 From: Jay Rogers Date: Tue, 24 Nov 2026 20:48:10 +0000 Subject: [PATCH] Sync Laravel Scout index settings in the Laravel automations --- docs/content/data/env-vars.yml | 7 +++ .../1.laravel/1.automations.md | 6 +++ .../image/fixtures/laravel-scout/artisan | 3 ++ .../fixtures/laravel-scout/bootstrap/app.php | 48 +++++++++++++++++++ .../laravel-scout/vendor/autoload.php | 1 + .../vendor/laravel/scout/composer.json | 1 + scripts/tests/image/laravel-automations.sh | 21 ++++++++ .../entrypoint.d/50-laravel-automations.sh | 24 ++++++++++ .../entrypoint.d/lib/laravel/bootstrap.php | 39 +++++++++++++++ .../lib/laravel/scout-has-index-settings.php | 47 ++++++++++++++++++ .../lib/laravel/test-db-connection.php | 34 +------------ 11 files changed, 199 insertions(+), 32 deletions(-) create mode 100644 scripts/tests/image/fixtures/laravel-scout/artisan create mode 100644 scripts/tests/image/fixtures/laravel-scout/bootstrap/app.php create mode 100644 scripts/tests/image/fixtures/laravel-scout/vendor/autoload.php create mode 100644 scripts/tests/image/fixtures/laravel-scout/vendor/laravel/scout/composer.json create mode 100644 src/common/etc/entrypoint.d/lib/laravel/bootstrap.php create mode 100644 src/common/etc/entrypoint.d/lib/laravel/scout-has-index-settings.php diff --git a/docs/content/data/env-vars.yml b/docs/content/data/env-vars.yml index 92e8f6b6..b77c39da 100644 --- a/docs/content/data/env-vars.yml +++ b/docs/content/data/env-vars.yml @@ -200,6 +200,13 @@ variables: reference: label: Laravel automations url: /docs/framework-guides/laravel/automations + - name: AUTORUN_LARAVEL_SCOUT_SYNC_INDEX_SETTINGS + default: "true" + requires: AUTORUN_ENABLED + description: Run `php artisan scout:sync-index-settings` after the other automations, so search engines such as Meilisearch get the index settings in `config/scout.php`. Skipped when Laravel Scout isn't installed or its engine has no index settings to sync. + reference: + label: Laravel automations + url: /docs/framework-guides/laravel/automations - name: AUTORUN_LARAVEL_SKIP_IF_NOT_FOUND default: "false" requires: AUTORUN_ENABLED diff --git a/docs/content/docs/3.framework-guides/1.laravel/1.automations.md b/docs/content/docs/3.framework-guides/1.laravel/1.automations.md index a116bf8d..ae0f3f6b 100644 --- a/docs/content/docs/3.framework-guides/1.laravel/1.automations.md +++ b/docs/content/docs/3.framework-guides/1.laravel/1.automations.md @@ -118,6 +118,12 @@ Turn one cache off, and the script runs `php artisan optimize --except=...` with Set `AUTORUN_LARAVEL_OPTIMIZE=false` to skip `optimize` and its package steps. The script still runs a `cache` command for each cache variable set to `true`. Set those to `false` too, and nothing gets cached. +## Scout index settings sync on start + +If your app uses [Laravel Scout](https://laravel.com/docs/13.x/scout){target="_blank"}, the script runs `php artisan scout:sync-index-settings` after caching. Meilisearch only filters and sorts on the attributes you list in `config/scout.php`, so the settings ship with the code that uses them. + +The script skips this step when Scout isn't installed, or when your engine has no `index-settings` to sync, such as the `database` driver. A search engine that's down doesn't stop the container: the error shows in the logs and startup continues. Set `AUTORUN_LARAVEL_SCOUT_SYNC_INDEX_SETTINGS=false` to turn it off. + ## A broken release never serves traffic If a step fails, the script exits with an error. The container stops before the web server starts. A failing migration shows up as a container that won't start, with the error in its logs. diff --git a/scripts/tests/image/fixtures/laravel-scout/artisan b/scripts/tests/image/fixtures/laravel-scout/artisan new file mode 100644 index 00000000..9149cc1c --- /dev/null +++ b/scripts/tests/image/fixtures/laravel-scout/artisan @@ -0,0 +1,3 @@ + $driver, + "scout.{$driver}.index-settings" => ['posts' => ['filterableAttributes' => ['organization_id']]], + ]; + + return $config[$key] ?? null; + } + + function app($class) + { + return new $class(); + } + + return new class { + public function make($class) + { + return $this; + } + + public function bootstrap() {} + }; +} diff --git a/scripts/tests/image/fixtures/laravel-scout/vendor/autoload.php b/scripts/tests/image/fixtures/laravel-scout/vendor/autoload.php new file mode 100644 index 00000000..b3d9bbc7 --- /dev/null +++ b/scripts/tests/image/fixtures/laravel-scout/vendor/autoload.php @@ -0,0 +1 @@ +&1); then + debug_log "Skipping Scout index settings sync: $scout_check_output" + return 0 + fi + + echo "🔎 Syncing Scout index settings: \"php artisan scout:sync-index-settings\"..." + if ! php "$APP_BASE_DIR/artisan" scout:sync-index-settings; then + echo "⚠️ $script_name: Scout index settings sync failed, continuing startup..." + fi +} + convert_comma_delimited_to_space_separated() { echo $1 | tr ',' ' ' } @@ -469,6 +489,10 @@ if laravel_is_installed; then [ "$AUTORUN_LARAVEL_EVENT_CACHE" = "true" ]; then artisan_optimize fi + + if [ "$AUTORUN_LARAVEL_SCOUT_SYNC_INDEX_SETTINGS" = "true" ]; then + artisan_scout_sync_index_settings + fi else if [ "$AUTORUN_LARAVEL_SKIP_IF_NOT_FOUND" = "true" ]; then debug_log "Laravel not detected in $APP_BASE_DIR. Skipping automations (AUTORUN_LARAVEL_SKIP_IF_NOT_FOUND=true)." diff --git a/src/common/etc/entrypoint.d/lib/laravel/bootstrap.php b/src/common/etc/entrypoint.d/lib/laravel/bootstrap.php new file mode 100644 index 00000000..a3227de4 --- /dev/null +++ b/src/common/etc/entrypoint.d/lib/laravel/bootstrap.php @@ -0,0 +1,39 @@ +make(Illuminate\Contracts\Console\Kernel::class); + $kernel->bootstrap(); + } catch (Throwable $e) { + fwrite(STDERR, "Error bootstrapping Laravel: {$e->getMessage()}\n"); + exit(1); + } +} diff --git a/src/common/etc/entrypoint.d/lib/laravel/scout-has-index-settings.php b/src/common/etc/entrypoint.d/lib/laravel/scout-has-index-settings.php new file mode 100644 index 00000000..774462ff --- /dev/null +++ b/src/common/etc/entrypoint.d/lib/laravel/scout-has-index-settings.php @@ -0,0 +1,47 @@ +engine($driver); +} catch (Throwable $e) { + fwrite(STDERR, "Error loading the Scout engine: {$e->getMessage()}\n"); + exit(1); +} + +if (!method_exists($engine, 'updateIndexSettings')) { + fwrite(STDOUT, "The \"{$driver}\" engine does not support index settings\n"); + exit(1); +} + +if (empty(config("scout.{$driver}.index-settings"))) { + fwrite(STDOUT, "No index settings configured for the \"{$driver}\" engine\n"); + exit(1); +} + +fwrite(STDOUT, "Index settings configured for the \"{$driver}\" engine\n"); +exit(0); diff --git a/src/common/etc/entrypoint.d/lib/laravel/test-db-connection.php b/src/common/etc/entrypoint.d/lib/laravel/test-db-connection.php index 59ca68f0..7584a7e5 100644 --- a/src/common/etc/entrypoint.d/lib/laravel/test-db-connection.php +++ b/src/common/etc/entrypoint.d/lib/laravel/test-db-connection.php @@ -38,38 +38,8 @@ exit(1); } -// Validate that the app base directory exists -if (!is_dir($appBaseDir)) { - fwrite(STDERR, "Error: App base directory does not exist: {$appBaseDir}\n"); - exit(1); -} - -// Validate that required Laravel files exist -$vendorAutoload = "{$appBaseDir}/vendor/autoload.php"; -$bootstrapApp = "{$appBaseDir}/bootstrap/app.php"; - -if (!file_exists($vendorAutoload)) { - fwrite(STDERR, "Error: Composer autoload file not found: {$vendorAutoload}\n"); - exit(1); -} - -if (!file_exists($bootstrapApp)) { - fwrite(STDERR, "Error: Laravel bootstrap file not found: {$bootstrapApp}\n"); - exit(1); -} - -// Bootstrap Laravel -try { - require $vendorAutoload; - - $app = require_once $bootstrapApp; - $kernel = $app->make(Illuminate\Contracts\Console\Kernel::class); - $kernel->bootstrap(); - -} catch (Exception $e) { - fwrite(STDERR, "Error bootstrapping Laravel: {$e->getMessage()}\n"); - exit(1); -} +require __DIR__ . '/bootstrap.php'; +bootstrap_laravel($appBaseDir); // Test database connection try {