Laravel config:cache serving stale env values in CI
After php artisan config:cache, Laravel loads a compiled config file and stops reading .env at runtime. env() calls outside config files return null. If the cache was built with an empty APP_KEY or wrong DB, every later step uses the stale values.
What this error means
Config looks correct in .env but the app behaves as if values are empty or wrong. APP_KEY is missing, DB points at the wrong host, or feature flags are off, all because a cached config predates the real env.
$ php artisan config:cache
INFO Configuration cached successfully.
$ php artisan test
RuntimeException: No application encryption key has been specified.Common causes
config:cache ran before env was populated
The cache froze an empty or placeholder config. Later steps that set APP_KEY or DB in .env have no effect because the cache is read first.
env() used outside config files
Once config is cached, env() outside config/*.php returns null. Code that reads env('SOMETHING') directly breaks in CI.
How to fix it
Clear the config cache in CI, or cache last
- Prefer not caching config during tests so .env is read live.
- If something cached it, run
php artisan config:clearbefore tests. - If you must cache, do it only after APP_KEY and DB are set.
- run: php artisan config:clear
- run: php artisan testRead config(), not env(), in app code
Move env() reads into config/*.php and call config('x') everywhere else so cached config keeps working.
// config/services.php
'stripe' => ['key' => env('STRIPE_KEY')],
// app code
config('services.stripe.key');How to prevent it
- Avoid
config:cachein the test job; let .env be read live. - Never call
env()outside config files. - If caching, run it only after all env values are final.