When setting an environment variable, the variable is accessible in shell but not in a Jenkins plugin. For example, if I use the following pipeline
pipeline { agent any environment { GITUSER = 'MyExampleUser' } stages { stage('someTestStage') { steps { doStuff() } } stage('printViaShell') { steps { sh 'echo "$GITUSER"' } } } }
doStuff is not able to access $GITUSER, but the shell command is able to do so.
This has been discussed by https://stackoverflow.com/questions/65015553/accessing-jenkins-set-environment-variables-from-a-jenkins-plugin and https://stackoverflow.com/questions/14214249/jenkins-plugin-environment-variables but nobody knew a solution.
I've created a mwe: https://github.com/DaGeRe/example-no-environment With everything I tried in https://github.com/DaGeRe/example-no-environment/blob/main/src/main/java/de/example/DoStuffBuilder.java I was not able to get the environment variable (a workaround would be to print the environment to a file and read this file in the plugin, but this does not seem to be a clean solution for me).
I am not sure whether this is a bug or missing documentation, but I did not find anything about injection of environment variables to own plugins in https://www.jenkins.io/doc/developer/plugin-development/ or related documentation.
- duplicates
-
JENKINS-29144 SimpleBuildStep to receive EnvVars
-
- Resolved
-
[JENKINS-65151] Using Environment Variables in Plugins is not Possible / not Documented
Description |
Original:
When setting an environment variable, the variable is accessible in shell but not in a Jenkins plugin. For example, if I use the following pipeline
{{}} {code:java} pipeline { agent any environment { GITUSER = 'MyExampleUser' } stages { stage('someTestStage') { steps { doStuff() } } stage('printViaShell') { steps { sh 'echo "$GITUSER"' } } } }{code} {{}} doStuff is not able to access $GITUSER, but the shell command is able to do so. This has been discussed by [https://stackoverflow.com/questions/65015553/accessing-jenkins-set-environment-variables-from-a-jenkins-plugin] and [https://stackoverflow.com/questions/14214249/jenkins-plugin-environment-variables] but nobody knew a solution. I've created a mwe: [https://github.com/DaGeRe/example-no-environment |https://github.com/DaGeRe/example-no-environment]With everything I tried im [https://github.com/DaGeRe/example-no-environment/blob/main/src/main/java/de/example/DoStuffBuilder.java] I was not able to get the environment variable (a workaround would be to print the environment to a file and read this file in the plugin, but this does not seem to be a clean solution for me). I am not sure whether this is a bug or missing documentation, but I did not find anything about injection of environment variables to own plugins in [https://www.jenkins.io/doc/developer/plugin-development/] or related documentation. |
New:
When setting an environment variable, the variable is accessible in shell but not in a Jenkins plugin. For example, if I use the following pipeline
{code:java} pipeline { agent any environment { GITUSER = 'MyExampleUser' } stages { stage('someTestStage') { steps { doStuff() } } stage('printViaShell') { steps { sh 'echo "$GITUSER"' } } } }{code} doStuff is not able to access $GITUSER, but the shell command is able to do so. This has been discussed by [https://stackoverflow.com/questions/65015553/accessing-jenkins-set-environment-variables-from-a-jenkins-plugin] and [https://stackoverflow.com/questions/14214249/jenkins-plugin-environment-variables] but nobody knew a solution. I've created a mwe: [https://github.com/DaGeRe/example-no-environment |https://github.com/DaGeRe/example-no-environment]With everything I tried in [https://github.com/DaGeRe/example-no-environment/blob/main/src/main/java/de/example/DoStuffBuilder.java] I was not able to get the environment variable (a workaround would be to print the environment to a file and read this file in the plugin, but this does not seem to be a clean solution for me). I am not sure whether this is a bug or missing documentation, but I did not find anything about injection of environment variables to own plugins in [https://www.jenkins.io/doc/developer/plugin-development/] or related documentation. |
Link |
New:
This issue duplicates |