Skip to content

Instantly share code, notes, and snippets.

@knalli
Last active April 27, 2020 08:10
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 knalli/6b7aae886b0c1f24b56c4c32c547eaa7 to your computer and use it in GitHub Desktop.
Save knalli/6b7aae886b0c1f24b56c4c32c547eaa7 to your computer and use it in GitHub Desktop.
Jenkinsfile with custom function for reading a file as environment variables
node {
stage('Demo') {
echo "Hello world"
withConfigEnv('env') {
echo "Here we go:"
sh "printenv"
}
}
}
// utility function: read file & provide as envionment vars
def withConfigEnv(String configFileId = 'env', Closure body) {
echo "DEBUG: configFileId=$configFileId"
configFileProvider([configFile(fileId: 'global.env', variable: 'tempGlobalFile'), configFile(fileId: configFileId, variable: 'tempLocalFile')]) {
def globalContent = readFile tempGlobalFile
withEnv(globalContent.split("\n")) {
def localContent = readFile tempLocalFile
withEnv(localContent.split("\n")) {
body()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment