Using environment specific variables in Laravel Vapor with Mix

I went down a bit of rabbit hole today, to the point where I was about to send a PR to the laravel/vapor-cli repository.

Consider this:

If I run vapor deploy staging locally, and I have this in .env: 

STRIPE_KEY=pk_test
MIX_STRIPE_KEY=${STRIPE_KEY}

And this in .env.staging: 

STRIPE_KEY=pk_live
MIX_STRIPE_KEY=${STRIPE_KEY}

Then mix is going to build `process.env.MIX_STRIPE_KEY` with a value of `pk_test`. 

My line of thinking is I need the Vapor build to use my .env.staging file to build those assets, damnit!

Alas, there is a better solution, mix-env-file.

The steps are as follows, replace "staging" with your environment of choice:

  1. Make sure you have the correct .env.staging present with vapor env:pull staging
  2. Install the mix-env-file package and set it up per the instructions
  3. Run your webpack specifying ENV_FILE=./.env.staging
  4. Your done!

Even better, this works very easily with a Continuous Integration service like CircleCI: in your vapor.yml build steps, just pull the correct .env as above prior to building assets and your done.