Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-65151

Using Environment Variables in Plugins is not Possible / not Documented

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major
    • core
    • Jenkins 2.263.1 and Ubuntu 20.04 and OpenJDK 8 / 11, but I assume every OS / Java Combination

      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.

            Unassigned Unassigned
            dagere David Georg Reichelt
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: