Skip to content

Instantly share code, notes, and snippets.

@ericblue
Created January 30, 2023 02:16
Show Gist options
  • Save ericblue/acb761461519ec0c9fae03222a604416 to your computer and use it in GitHub Desktop.
Save ericblue/acb761461519ec0c9fae03222a604416 to your computer and use it in GitHub Desktop.
Gradle - Support loading of environment variables from a custom .env file prior to run (works in IDE and CLI)
// Loads environments variables from files contained in .env located in the CWD'
// See: https://stackoverflow.com/questions/36322536/how-to-set-an-environment-variable-from-a-gradle-build
run.doFirst {
if (project.file('.env').exists()) {
file('.env').readLines().each() {
if (!it.isEmpty() && !it.startsWith("#")) {
def (key, value) = it.tokenize('=')
if (System.getenv(key) == null) {
println "Setting environment variable for ${key}"
environment key, value
}
}
}
} else {
println "No custom environment variables set"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment