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

env vars are not consistent

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open (View Workflow)
    • Priority: Minor
    • Resolution: Unresolved
    • Component/s: docker-workflow-plugin
    • Labels:
      None
    • Environment:
      jenkins 2.287
      docker-plugin: 1.2.2
      docker-workflow: 1.2.6
    • Similar Issues:

      Description

      I'm trying to do something like this:

      node {
          stage('reproduce') {
              sh """
                  echo '1'
                  printenv PATH
                  echo "$PATH"
              """
              docker.image('python:3.6.4-slim').inside {
                  sh """
                      echo '2'
                      printenv PATH
                      echo "$PATH"
                  """
                  withEnv(["PATH=$PATH:/my/custom/path"]) {
                      sh """
                          echo '3'
                          printenv PATH
                          echo "$PATH"
                      """
                  }
              }
          }
      }
      

      sh (1) gives me:

      + printenv PATH
      /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
      + echo /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 
      /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

      all is good so far

      sh (2) gives me:

      + printenv PATH
      /usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
      + echo /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 
      /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

      which is already confusing

      sh (3) gives:

      + printenv PATH
      /usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
      + echo /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/my/custom/path 
      /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/my/custom/path

       

      I have couple of questions about this:

      • why printenv and echo in sh(2) show different results? sounds like printenv prints original env which comes from docker image while echo prints value from host jenkins.
      • why withEnv which is executed inside docker container does not modify docker's PATH but modifies Jenkins's PATH.

        Attachments

          Activity

          spugachev Stanislav Pugachov created issue -
          spugachev Stanislav Pugachov made changes -
          Field Original Value New Value
          Description I'm trying to do something like this:
          {code:java}
          node {
              stage('reproduce') {
                  sh """
                      echo '1'
                      printenv PATH
                      echo "$PATH"
                  """
                  docker.image('python:3.6.4-slim').inside {
                      sh """
                          echo '2'
                          printenv PATH
                          echo "$PATH"
                      """
                      withEnv(["PATH=$PATH:/my/custom/path"]) {
                          sh """
                              echo '3'
                              printenv PATH
                              echo "$PATH"
                          """
                      }
                  }
              }
          }
          {code}
          sh (1) gives me:
          {noformat}
          + printenv PATH /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
          + echo /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin{noformat}
          all is good so far

          sh (2) gives me:
          {noformat}
          + printenv PATH /usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
          + echo /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin{noformat}
          which is already confusing

          sh (3) gives:
          {noformat}
          + printenv PATH /usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
          + echo /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/my/custom/path /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/my/custom/path{noformat}
           

          I have couple of questions about this:
           * why printenv and echo in sh(2) show different results? sounds like printenv prints original env which comes from docker image while echo prints value from host jenkins.
           * why withEnv which is executed inside docker container does not modify docker's PATH but modifies Jenkins's PATH.
          I'm trying to do something like this:
          {code:java}
          node {
              stage('reproduce') {
                  sh """
                      echo '1'
                      printenv PATH
                      echo "$PATH"
                  """
                  docker.image('python:3.6.4-slim').inside {
                      sh """
                          echo '2'
                          printenv PATH
                          echo "$PATH"
                      """
                      withEnv(["PATH=$PATH:/my/custom/path"]) {
                          sh """
                              echo '3'
                              printenv PATH
                              echo "$PATH"
                          """
                      }
                  }
              }
          }
          {code}
          sh (1) gives me:
          {noformat}
          + printenv PATH
          /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
          + echo /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
          /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin{noformat}
          all is good so far

          sh (2) gives me:
          {noformat}
          + printenv PATH
          /usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
          + echo /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
          /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin{noformat}
          which is already confusing

          sh (3) gives:
          {noformat}
          + printenv PATH
          /usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
          + echo /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/my/custom/path
          /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/my/custom/path{noformat}
           

          I have couple of questions about this:
           * why printenv and echo in sh(2) show different results? sounds like printenv prints original env which comes from docker image while echo prints value from host jenkins.
           * why withEnv which is executed inside docker container does not modify docker's PATH but modifies Jenkins's PATH.
          Hide
          mpokorny Martin added a comment -

          This also does not work for us. But we use a workaround like

          sh makeEnvList(envList) + ' echo "$PATH" '

          ...

          String makeEnvList(env){
          String s = '\n'
          for(int i = 0; i < env.size(); i++)
          s += 'export ' + env[i]
          return s
          }

          Ps. Written on my phone keyboard. Therefore is possible that the snippet has wrong syntax. But I think like example it is enough.

          Show
          mpokorny Martin added a comment - This also does not work for us. But we use a workaround like sh makeEnvList(envList) + ' echo "$PATH" ' ... String makeEnvList(env){ String s = '\n' for(int i = 0; i < env.size(); i++) s += 'export ' + env [i] return s } Ps. Written on my phone keyboard. Therefore is possible that the snippet has wrong syntax. But I think like example it is enough.

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            spugachev Stanislav Pugachov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated: