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

Using Environment Variables in Plugins is not Possible / not Documented

    XMLWordPrintable

Details

    • Bug
    • Status: Open (View Workflow)
    • Major
    • Resolution: Unresolved
    • core
    • Jenkins 2.263.1 and Ubuntu 20.04 and OpenJDK 8 / 11, but I assume every OS / Java Combination

    Description

      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.

      Attachments

        Issue Links

          Activity

            jglick Jesse Glick added a comment -

            You are using the wrong method overload. See JENKINS-29144.

            jglick Jesse Glick added a comment - You are using the wrong method overload. See JENKINS-29144 .

            People

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

              Dates

                Created:
                Updated: