Kotlin - dotenv

Introduction

Storing configuration in the environment is one of the tenets of a twelve-factor app. Anything that is likely to change between deployment environments–such as resource handles for databases or credentials for external services–should be extracted from the code into environment variables.

But it is not always practical to set environment variables on development machines or continuous integration servers where multiple projects are run. Dotenv load variables from a .env file into ENV when the environment is bootstrapped.

dotenv-kotlin

dotenv-kotlin load environment variables from a .env file.

Environment variables listed in the host environment override those in .env.

Use dotenv.get("...") instead of Java’s System.getenv(...).

module.yaml
dependencies:
- io.github.cdimascio:dotenv-kotlin:6.5.1

Create a .env file in the root of your project

# formatted as key=value
MY_ENV_VAR1=some_value
MY_EVV_VAR2=some_value
MY_ENV_MULTI_LINE="some
multiline
value"
import io.github.cdimascio.dotenv.dotenv
val dotenv = dotenv()
dotenv["MY_ENV_VAR1"]