Laravel missing .env file (cp .env.example) in CI
.env is gitignored, so a fresh checkout has none. Laravel then reads only real environment variables and defaults, which usually leaves APP_KEY, DB, and other settings empty. Copy .env.example to .env as the first setup step.
What this error means
Cascading failures on a fresh checkout: empty APP_KEY, wrong DB connection, or "No application encryption key". Locally it works because .env exists; CI never had one.
$ php artisan migrate
INFO Preparing database.
Illuminate\Database\QueryException
SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost'Common causes
.env is gitignored and never created in CI
Laravel ships .env in .gitignore, so it is absent after checkout. Every value falls back to defaults or empty, breaking DB, cache, and encryption.
The example file has placeholder values
.env.example ships with placeholder DB credentials that do not match the CI service, so even after copying you must override the relevant vars.
How to fix it
Copy the example env first, then override
- Add
cp .env.example .envas the first step after checkout. - Override DB_* and other CI-specific values via job env.
- Generate the key and continue with migrate and test.
- run: cp .env.example .env
- run: php artisan key:generate
env:
DB_CONNECTION: mysql
DB_HOST: 127.0.0.1
DB_DATABASE: testing
DB_USERNAME: root
DB_PASSWORD: passwordUse .env.testing for the test suite
Commit a .env.testing with CI-safe defaults so --env=testing reads a known file instead of relying on a copied .env.
- run: php artisan test --env=testingHow to prevent it
- Always
cp .env.example .envas the first CI step. - Keep
.env.examplecurrent with every required key. - Override CI-specific values (DB, cache, queue) through job env.