简体   繁体   中英

Exporting environment variables for local development and heroku deployment

I would like to setup some files for development, staging and production with environment variables, for example:

application_root/development.env

KEY1=value1
KEY2=value2

There would be similar files staging.env and production.env .

I am looking for a couple different bash scripts which would allow the loading of all these variables in either development or staging/production.

In local development I want to effectively run export KEY1=value1 for each line in the file.

For staging/production I will be deploying to Heroku and would like to effectively run heroku config:set -a herokuappname KEY1=value1 for each line in the staging or production.env files.

I know there are some gems designed for doing this but it seems like this might be pretty simple. I also like the flexibility of having the .env files as simple lists of keys and values and not specifically being tied to any language/framework. If I would have to change something about the way these variables need to be loaded it would be a matter of changing the script but not the .env files.

In the simplest form, you can load the key-value pairs into a bash array as follows:

IFS=$'\n' read -d '' -ra nameValuePairs < ./development.env

In Bash v4+, it's even simpler:

readarray -t nameValuePairs < ./development.env

You can then pass the resulting "${nameValuePairs[@]}" array to commands such as export or heroku config:set ... ; eg:

export "${nameValuePairs[@]}"

Note, however, that the above only works as intended if the input *.env file meets all of the following criteria:

  • the keys are syntactically valid shell variable names and the lines have the form <key>=<value> , with no whitespace around =
  • the lines contain no quoting and no leading or trailing whitespace
  • there are no empty/blank lines or comment lines in the file.
  • the values are confined to a single line each.

A different approach is needed with files that do not adhere to this strict format; for instance, this related question deals with files that may contain quoted values.


Below is the source code for a bash script named load_env (the .sh suffix is generally not necessary and ambiguous):

  • You'd invoke it with the *.env file of interest, and it would perform the appropriate action (running heroku config:set … or export ) based on the filename .

  • However, as stated, you must source the script (using source or its effective bash alias, . ) in order to create environment variables ( export ) visible to the current shell.
    To prevent obscure failures, the script complains if you pass a development.env file and have invoked the script without sourcing.

Examples:

./load_env ./staging.dev  
. ./load_env ./development.dev   # !! Note the need to source

load_env source code

#!/usr/bin/env bash

# Helper function that keeps its aux. variables localized.
# Note that the function itself remains defined after sourced invocation, however.
configOrExport() {

  local envFile=$1 doConfig=0 doExport=0 appName

  case "$(basename "$envFile" '.env')" in
    staging)
      doConfig=1
      # Set the desired app name here.
      appName=stagingapp
      ;;
    production)
      doConfig=1
      # Set the desired app name here.
      appName=productionapp
      ;;
    development)
      doExport=1
      ;;
    *)
      echo "ERROR: Invalid or missing *.env file name: $(basename "$envFile" '.env')" >&2; exit 2
  esac

  # Make sure the file exists and is readable.
  [[ -r "$envFile" ]] || { echo "ERROR: *.env file not found or not readable: $envFile" >&2; exit 2; }

  # If variables must be exported, make sure the script is being sourced.
  [[ $doExport -eq 1 && $0 == "$BASH_SOURCE" ]] && { echo "ERROR: To define environment variables, you must *source* this script." >&2; exit 2; }

  # Read all key-value pairs from the *.env file into an array.
  # Note: This assumes that:
  #        - the keys are syntactically valid shell variable names
  #        - the lines contain no quoting and no leading or trailing whitespace
  #        - there are no empty/blank lines or comment lines in the file.
  IFS=$'\n' read -d '' -ra nameValuePairs < "$envFile"

  # Run configuration command.
  (( doConfig )) && { heroku config:set -a "$appName" "${nameValuePairs[@]}" || exit; }

  # Export variables (define as environment variables).
  (( doExport )) && { export "${nameValuePairs[@]}" || exit; }

}

# Invoke the helper function.
configOrExport "$@"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM