Last active
April 27, 2020 08:10
-
-
Save knalli/6b7aae886b0c1f24b56c4c32c547eaa7 to your computer and use it in GitHub Desktop.
Jenkinsfile with custom function for reading a file as environment variables
This file contains 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
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