Last active
March 1, 2023 07:35
-
-
Save silas/b2454f259ce600056d455e57351bdf68 to your computer and use it in GitHub Desktop.
Load `.env` into gradle run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def static getenv(path = ".env") { | |
def env = [:] | |
def file = new File(path) | |
if (file.exists()) { | |
file.eachLine { line -> | |
line = line.trim() | |
if (line != "" && !line.startsWith("#")) { | |
def pair = line.split("=", 2) | |
env[pair[0].trim()] = pair.length == 2 ? pair[1].trim() : "" | |
} | |
} | |
} | |
return env | |
} | |
run { | |
getenv().each { name, value -> environment name, value } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As I stumbled upon this gist from search, I put my improved version here. I fixed some issues, such as:
tokenize
splits line not into two substrings but into as many as'='
chars it have. Needs to be replaced withsplit('=', 2)
if
for thatIt's still not perfect as any comments in
.env
file would break everything, but it's a bit better already