-
-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy path50-laravel-automations.sh
More file actions
148 lines (127 loc) · 5.9 KB
/
Copy path50-laravel-automations.sh
File metadata and controls
148 lines (127 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/sh
script_name="laravel-automations"
test_db_connection() {
php -r "
require '$APP_BASE_DIR/vendor/autoload.php';
use Illuminate\Support\Facades\DB;
\$app = require_once '$APP_BASE_DIR/bootstrap/app.php';
\$kernel = \$app->make(Illuminate\Contracts\Console\Kernel::class);
\$kernel->bootstrap();
\$driver = DB::getDriverName();
if( \$driver === 'sqlite' ){
echo 'SQLite detected';
exit(0); // Assume SQLite is always ready
}
try {
DB::connection()->getPdo(); // Attempt to get PDO instance
if (DB::connection()->getDatabaseName()) {
exit(0); // Database exists and can be connected to, exit with status 0 (success)
} else {
echo 'Database name not found.';
exit(1); // Database name not found, exit with status 1 (failure)
}
} catch (Exception \$e) {
echo 'Database connection error: ' . \$e->getMessage();
exit(1); // Connection error, exit with status 1 (failure)
}
"
}
# Set default values for Laravel automations
: "${AUTORUN_ENABLED:=false}"
: "${AUTORUN_LARAVEL_MIGRATION_TIMEOUT:=30}"
if [ "$DISABLE_DEFAULT_CONFIG" = "false" ]; then
# Check to see if an Artisan file exists and assume it means Laravel is configured.
if [ -f "$APP_BASE_DIR/artisan" ] && [ "$AUTORUN_ENABLED" = "true" ]; then
echo "Checking for Laravel automations..."
############################################################################
# recreate storage
############################################################################
if [ "${AUTORUN_LARAVEL_STORAGE_RECREATE:=true}" = "true" ]; then
storage_paths='storage/app/public storage/framework/cache/data storage/framework/sessions storage/framework/testings storage/framework/views storage/logs'
for path in ${storage_paths}; do
if [ ! -d "$path" ]; then
mkdir -p "$path"
echo "✅ Recreated [$path] directory."
fi
done
############################################################################
# artisan migrate
############################################################################
if [ "${AUTORUN_LARAVEL_MIGRATION:=true}" = "true" ]; then
count=0
timeout=$AUTORUN_LARAVEL_MIGRATION_TIMEOUT
echo "🚀 Clearing Laravel cache before attempting migrations..."
php "$APP_BASE_DIR/artisan" config:clear
# Do not exit on error for this loop
set +e
echo "⚡️ Attempting database connection..."
while [ $count -lt "$timeout" ]; do
test_db_connection > /dev/null 2>&1
status=$?
if [ $status -eq 0 ]; then
echo "✅ Database connection successful."
break
else
echo "Waiting on database connection, retrying... $((timeout - count)) seconds left"
count=$((count + 1))
sleep 1
fi
done
# Re-enable exit on error
set -e
if [ $count -eq "$timeout" ]; then
echo "Database connection failed after multiple attempts."
return 1
fi
echo "🚀 Running migrations..."
if [ "${AUTORUN_LARAVEL_MIGRATION_ISOLATION:=false}" = "true" ]; then
php "$APP_BASE_DIR/artisan" migrate --force --isolated
else
php "$APP_BASE_DIR/artisan" migrate --force
fi
fi
############################################################################
# artisan storage:link
############################################################################
if [ "${AUTORUN_LARAVEL_STORAGE_LINK:=true}" = "true" ]; then
if [ -d "$APP_BASE_DIR/public/storage" ]; then
echo "✅ Storage already linked..."
else
echo "🔐 Linking the storage..."
php "$APP_BASE_DIR/artisan" storage:link
fi
fi
############################################################################
# artisan config:cache
############################################################################
if [ "${AUTORUN_LARAVEL_CONFIG_CACHE:=true}" = "true" ]; then
echo "🚀 Caching Laravel config..."
php "$APP_BASE_DIR/artisan" config:cache
fi
############################################################################
# artisan route:cache
############################################################################
if [ "${AUTORUN_LARAVEL_ROUTE_CACHE:=true}" = "true" ]; then
echo "🚀 Caching Laravel routes..."
php "$APP_BASE_DIR/artisan" route:cache
fi
############################################################################
# artisan view:cache
############################################################################
if [ "${AUTORUN_LARAVEL_VIEW_CACHE:=true}" = "true" ]; then
echo "🚀 Caching Laravel views..."
php "$APP_BASE_DIR/artisan" view:cache
fi
############################################################################
# artisan event:cache
############################################################################
if [ "${AUTORUN_LARAVEL_EVENT_CACHE:=true}" = "true" ]; then
echo "🚀 Caching Laravel events..."
php "$APP_BASE_DIR/artisan" event:cache
fi
fi
else
if [ "$LOG_OUTPUT_LEVEL" = "debug" ]; then
echo "👉 $script_name: DISABLE_DEFAULT_CONFIG does not equal 'false', so automations will NOT be performed."
fi
fi