Skip to content

Instantly share code, notes, and snippets.

@ThabetAmer
Last active January 18, 2020 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThabetAmer/e39a7b12fb87caa11c2f8689bd929dfa to your computer and use it in GitHub Desktop.
Save ThabetAmer/e39a7b12fb87caa11c2f8689bd929dfa to your computer and use it in GitHub Desktop.
Groovy Function for Jenkins pipelines
#!/usr/bin/env groovy
/**
* Reads environment variables from a property file, and loads them into Jenkins ENV
* @author thabet.amer@gmail.com
* @since Jenkins 2.204.1
* @param String path
* @return String array
*
* Example property file:
* # Environment variables for Jenkins builds
* VAR1=SAMPLE-VAR
*
*/
def loadEnvvars(String path) {
try {
Map envvars = readProperties(interpolate: true, file: path) ?: null
if (!envvars) { throw new Exception("unable to read properties file " + path) }
keys = envvars.keySet()
for(key in keys) {
value = envvars["${key}"]
env."${key}" = "${value}"
}
return envvars
}
catch(err) {
echo "ERROR: caught in loadEnvvars ${err}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment