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

Newline added to environment variables when running agents in Linux container from Windows controller

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major
    • core, docker, kubernetes-plugin
    • Jenkins Server: Windows 2012 R2, Jenkins 2.263.4, ssh-agent-plugin 1.22
      Agents: jenkins/inbound-agent:4.3-4 Docker container running under Kubernetes client version 1.16.8 and server version 1.17.5

      I have the following Jenkinsfile, executing in a Linux container under Kubernetes. My Jenkins server is version 2.263.4 running on Windows 2012 R2. Any variable I define in my environments section shows up in the sh action with a newline at the end:
       

      pipeline {
        agent {
          kubernetes {
            label UUID.randomUUID().toString()
            yaml """
      # ..snip...
      """
          }
        }
      
        environment {
          VAR1 = 'VALUE 1'
          VAR2 = 'VALUE 2'
        }
      
        stages {
          stage('One') {
            steps {
              container('docker') {
                sh 'echo -n "$PATH"'
                sh 'echo -n "$VAR1"'
                sh 'echo -n "$VAR2"'
              }
            }
          }
        }
      } 

      Which results in this output:

      [Pipeline] sh
      + echo -n /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
      /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
      [Pipeline] sh
      + echo -n 'VALUE 1
      '
      VALUE 1
      [Pipeline] sh
      + echo -n 'VALUE 2
      '
      VALUE 2

      As you can see, the PATH environment variable has no newline in the shell command, but the two variables from the Jenkinsfile have newlines at the end of their command and are surrounded by single quotes, even though they are surrounded by double-quotes in my sh command.

      The problem happens when I use these values as parameters to other commands. For example:

      sh 'git clone -b $BRANCH $REMOTE source'
      

      ends up running this command:

      + git clone -b 'BranchValue
        ' 'RemoteValue
      + ' source
      

      This doesn't happen when I run on Windows. This step:

            steps {
                powershell '"\'$env:VAR1\'"'
                powershell '"\'$env:VAR2\'"'
            }
      

      shows no newlines:

      [Pipeline] powershell
      'VALUE 1'
      [Pipeline] powershell
      'VALUE 2'
      

      I don't have a non-container Linux on which to run, but at least one user reports it doesn't happen on Linux outside docker.

            csanchez Carlos Sanchez
            splatteredbits Aaron Jensen
            Votes:
            2 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: