Question

What's the best way to add many environment variables to an App Platform service?

Posted November 30, 2020 896 views
DigitalOcean App Platform

We’re attempting to build a Laravel app on the App Platform, and for those unaware, the default .env file for a Laravel project is pretty long (ours is about twice this size):

APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

Obviously copy/pasting them all into the web UI isn’t ideal, and neither is committing the .env file to version control.

What’s the best way of getting these environment variables into the app?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

My strategy has been to locally save the yaml file:

https://cloud.digitalocean.com/apps/download-spec/your-app-name.yaml

Then edit and don’t submit to version control but use doctl to update my app https://developers.digitalocean.com/documentation/v2/#update-an-app

# init with API keys, see docs
doctl auth init

# list your apps
doctl apps list #list all 

# then update app with edited YAML
doctl apps update <app-id> --spec <path-to-yaml>

More on doctl and app platform: https://www.digitalocean.com/docs/app-platform/references/command-line/

Works like a charm.

  • This would be the best option currently. I agree that it isn’t ideal to add each env var one by one—we are exploring ways to make this an easier experience.

    One of the ideas that came up is to parse .env.example when first creating an app and populating the environment variables with it so you would only have to fill out the values. Would that be helpful? Happy to hear any other suggestions as well :)

    • That would certainly be better than nothing, but still not ideal (my .env file has almost 200 lines).

      Two quick ideas:

      • A text field (or file uploader) where we could paste the entire .env file, which would then parse client-side into the individual variable fields (make sure to include a way to delete all the existing fields in one fell swoop if a new .env needs to be uploaded).
      • A command line uploader (I don’t love their implementation — it only works on single apps, not the entire app deploy pipeline, and can’t take an entire .env as an argument — but Heroku uses heroku config:set).

      I’m not sure if those ideas play well with the concepts outlined in the discussion on this question, however.

    • I like the Laravel Forge way, just a textarea you can paste your envs in. Truth be told: it’s a hassle now, but only once