This plugin contributes the keepEnv step for Jenkins Pipelines. It is similar to the withEnv step, but only retains environment variables that are defined in the argument, or are "system" environment variables.
It is similar in function to the "Keep Only Specified Environment Variables" option for some build steps introduced in Jenkins 2.248.
The following Pipeline demonstrates how this plugin works:
node {
withEnv(['foo=bar', 'baz=qux', 'PATH=/usr/bin:/bin:/foo']) {
sh 'env'
keepEnv(['foo', 'WORKSPACE']) {
sh 'env'
}
}
}
-
The second invocation of
envwill not listbazor most Jenkins-contributed environment variables, likeBUILD_IDorNODE_NAME. -
The variables
foo(from the surroundingwithEnvblock),WORKSPACE(contributed by Jenkins), a few essential Jenkins-contributed variables, as well as system-contributed variables (likePATH) will still be available. -
System-contributed environment variables that are not set to be kept will be reset to their default value (
PATHin the example) instead of being undefined.
Refer to our contribution guidelines
Licensed under MIT, see LICENSE