Skip to content
Latchkey

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
$ 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

  1. Add cp .env.example .env as the first step after checkout.
  2. Override DB_* and other CI-specific values via job env.
  3. Generate the key and continue with migrate and test.
.github/workflows/ci.yml
- 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: password

Use .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.

.github/workflows/ci.yml
- run: php artisan test --env=testing

How to prevent it

  • Always cp .env.example .env as the first CI step.
  • Keep .env.example current with every required key.
  • Override CI-specific values (DB, cache, queue) through job env.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →