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
7 changes: 7 additions & 0 deletions docs/content/data/env-vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions scripts/tests/image/fixtures/laravel-scout/artisan
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
// The fake artisan from fixtures/laravel, so this app records its commands the same way.
require __DIR__ . '/../laravel/artisan';
48 changes: 48 additions & 0 deletions scripts/tests/image/fixtures/laravel-scout/bootstrap/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
// fixtures/laravel with Laravel Scout installed, so lib/laravel/scout-has-index-settings.php
// can boot it. FAKE_SCOUT_DRIVER picks the engine: meilisearch (the default) takes
// index settings, others don't.

namespace Laravel\Scout {
class MeilisearchEngine
{
public function updateIndexSettings() {}
}

class DatabaseEngine {}

class EngineManager
{
public function engine($driver)
{
return $driver === 'meilisearch' ? new MeilisearchEngine() : new DatabaseEngine();
}
}
}

namespace {
function config($key)
{
$driver = getenv('FAKE_SCOUT_DRIVER') ?: 'meilisearch';
$config = [
'scout.driver' => $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() {}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name": "laravel/scout"}
21 changes: 21 additions & 0 deletions scripts/tests/image/laravel-automations.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ expect_container_stopped() {

no_migration_or_storage_link=(--env AUTORUN_LARAVEL_STORAGE_LINK=false --env AUTORUN_LARAVEL_MIGRATION=false)
no_optimizations=(--env AUTORUN_LARAVEL_OPTIMIZE=false --env AUTORUN_LARAVEL_CONFIG_CACHE=false --env AUTORUN_LARAVEL_ROUTE_CACHE=false --env AUTORUN_LARAVEL_VIEW_CACHE=false --env AUTORUN_LARAVEL_EVENT_CACHE=false)
with_scout=(--volume "$fixtures_dir:/fixtures:ro" --env APP_BASE_DIR=/fixtures/laravel-scout)

test_does_nothing_unless_autorun_is_enabled() {
run_automations --env AUTORUN_ENABLED=false
Expand Down Expand Up @@ -78,3 +79,23 @@ test_skips_a_missing_laravel_app_when_asked() {
run_automations --env APP_BASE_DIR=/var/www/missing --env AUTORUN_LARAVEL_SKIP_IF_NOT_FOUND=true
expect_calls ""
}

test_syncs_scout_index_settings_last() {
run_automations "${with_scout[@]}"
expect_calls "storage:link;config:clear;migrate --force;optimize;scout:sync-index-settings"
}

test_skips_scout_engines_without_index_settings() {
run_automations "${with_scout[@]}" "${no_migration_or_storage_link[@]}" "${no_optimizations[@]}" --env FAKE_SCOUT_DRIVER=database
expect_calls ""
}

test_skips_scout_sync_when_disabled() {
run_automations "${with_scout[@]}" "${no_migration_or_storage_link[@]}" "${no_optimizations[@]}" --env AUTORUN_LARAVEL_SCOUT_SYNC_INDEX_SETTINGS=false
expect_calls ""
}

test_keeps_starting_when_the_scout_sync_fails() {
run_automations "${with_scout[@]}" "${no_migration_or_storage_link[@]}" "${no_optimizations[@]}" --env FAKE_ARTISAN_FAILS=scout:sync-index-settings
expect_calls "scout:sync-index-settings"
}
24 changes: 24 additions & 0 deletions src/common/etc/entrypoint.d/50-laravel-automations.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ script_name="laravel-automations"
: "${AUTORUN_LARAVEL_MIGRATION_SKIP_DB_CHECK:=false}"
: "${AUTORUN_LARAVEL_MIGRATION_TIMEOUT:=30}"

# Set default values for Laravel Scout
: "${AUTORUN_LARAVEL_SCOUT_SYNC_INDEX_SETTINGS:=true}"

# Set default values for Laravel version
INSTALLED_LARAVEL_VERSION=""

Expand Down Expand Up @@ -255,6 +258,23 @@ artisan_optimize() {
return 0
}

artisan_scout_sync_index_settings() {
if [ ! -d "$APP_BASE_DIR/vendor/laravel/scout" ]; then
debug_log "Laravel Scout not installed, skipping index settings sync."
return 0
fi

if ! scout_check_output=$(php "$AUTORUN_LIB_DIR/laravel/scout-has-index-settings.php" "$APP_BASE_DIR" 2>&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 ',' ' '
}
Expand Down Expand Up @@ -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)."
Expand Down
39 changes: 39 additions & 0 deletions src/common/etc/entrypoint.d/lib/laravel/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Boots the Laravel app in $appBaseDir for the other helpers in this folder.
* Exits 1 with the reason on STDERR when the app can't be booted.
*
* @package serversideup/php
*/

function bootstrap_laravel(string $appBaseDir): void
{
if (!is_dir($appBaseDir)) {
fwrite(STDERR, "Error: App base directory does not exist: {$appBaseDir}\n");
exit(1);
}

$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);
}

try {
require $vendorAutoload;

$app = require_once $bootstrapApp;
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
} catch (Throwable $e) {
fwrite(STDERR, "Error bootstrapping Laravel: {$e->getMessage()}\n");
exit(1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Check whether Laravel Scout has index settings to sync
*
* `scout:sync-index-settings` prints an error on every start for engines that
* can't take index settings, such as the database and collection drivers. This
* runs the same checks first, so the automations only call it when it has work.
*
* Usage: php scout-has-index-settings.php /path/to/app/base/dir
*
* Exit codes:
* 0 - The configured engine supports index settings, and some are configured
* 1 - Nothing to sync, or the app or engine couldn't be loaded
*
* @package serversideup/php
*/

use Laravel\Scout\EngineManager;

if ($argc !== 2) {
fwrite(STDERR, "Usage: php scout-has-index-settings.php /path/to/app/base/dir\n");
exit(1);
}

require __DIR__ . '/bootstrap.php';
bootstrap_laravel($argv[1]);

try {
$driver = config('scout.driver');
$engine = app(EngineManager::class)->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);
34 changes: 2 additions & 32 deletions src/common/etc/entrypoint.d/lib/laravel/test-db-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down