ENV Files

Background

One of the potential drawbacks for using JSON configuration is that those files eventually need to be shared and potentially checked into version control. Ideally, it would be possible for the .cfConfig.json to be checked into a repo so everyone would know what settings needed to be set but the actual passwords (keys, tokens, private stuff) would be pulled from someplace locally. That is the problem that .env files solve.

Env files do NOT get checked into repos because they are designed to contain secret information which should not be broadcast.

Hiding our secrets

  1. In CommandBox ,install the commandBox DotEnv module by typing install commandbox-dotenv.

  2. In the root of your site, create a file called .env.

  3. You can either edit the file in your IDE or use dotenv set name value . We're going to set several values in our .env. dotenv set DB_USER username dotenv set DB_NAME datbasename dotenv set DB_PASSWORD password dotenv set DB_DATABASE dbName dotenv set DB_CLASS com.mysql.cj.jdbc.Driver dotenv set DB_SCHEMA schemaName dotenv set DB_BUNDLE com.mysql.cj dotenv set DB_BUNDLEVERSION 8.0.15 dotenv set DB_TYPE mysql dotenv set DB_HOST justenough.cyqisqc1fx3i.us-west-2.rds.amazonaws.com

  4. In the .cfConfig.json file, replace the database username with ${DB_USER} the database password with ${DB_PASSWORD} the database name with {$DB_DATABASE} the class with ${DB_CLASS} the database name with ${DB_NAME} the host with ${DB_HOST}

  5. Restart the server with server restart

  6. Open the site in the browser and see what happens.

You might ask, "But how will other people on the team know what settings need to go into the .env file?". This is an excellent quesion and we'll address it in the next session.

Last updated