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

bat pipeline with returnstdout=true echos command

      We updated to Jenkins LTS 2.401.1 via dockerhub. Since that version all our pipeline builds broke because the bat pipeline step seems to also return the command call itself in the result which it did not before. Powershell works.

       

      See the following code to reproduce the problem

      stage('Test') {
        steps {
          script {
            def result = bat(label: '', returnStdout: true, script: 'echo echotestresponse')
            echo "output from bat: $result"
            def psResult = powershell(label: '', returnStdout: true, script: 'echo echotestresponse')
            echo "output from powershell: $psResult"
          }
        }
      }

      Output is:

      17:17:18  [Pipeline] stage
      17:17:18  [Pipeline] { (Test)
      17:17:18  [Pipeline] script
      17:17:18  [Pipeline] {
      17:17:18  [Pipeline] bat
      17:17:18  [Pipeline] echo
      17:17:18  output from bat: 
      17:17:18  jenkins@HMJENKINS4 d:\jenkins\workspace\MPA_jenkins-fixing>echo echotestresponse 
      17:17:18  echotestresponse
      17:17:18  
      17:17:18  [Pipeline] powershell
      17:17:19  [Pipeline] echo
      17:17:19  output from powershell: echotestresponse
      17:17:19  
      17:17:19  [Pipeline] }
      17:17:19  [Pipeline] // script
      17:17:19  [Pipeline] }
      17:17:19  [Pipeline] // stage 

      Additionally there is also a new line character at start of the result now.

       

      A workaround seems to be to prefix any command with @ or @echo off explicitely.

          [JENKINS-71436] bat pipeline with returnstdout=true echos command

          Daniel added a comment -

          Yes the output is there, but not returned and assigned to the variable. That is the problem.

          We use it to get some infos from maven command like the pom version.

          Daniel added a comment - Yes the output is there, but not returned and assigned to the variable. That is the problem. We use it to get some infos from maven command like the pom version.

          Markus Winter added a comment -

          The variable must be set otherwise the output wouldn't appear after the text 

          output from bat:
          

          If this was the output of the step itself would have to appear before that line

          Markus Winter added a comment - The variable must be set otherwise the output wouldn't appear after the text  output from bat: If this was the output of the step itself would have to appear before that line

          Daniel added a comment -

          I have seen the same and it confused me too. So I added a sleep between the echo and the bat execution.

          See the output:

          17:51:28  [Pipeline] stage
          17:51:28  [Pipeline] { (Test)
          17:51:28  [Pipeline] script
          17:51:28  [Pipeline] {
          17:51:28  [Pipeline] bat
          17:51:28  [Pipeline] sleep
          17:51:28  Sleeping for 5 sec
          17:51:33  [Pipeline] echo
          17:51:33  output from bat: 
          17:51:33  jenkins@HMJENKINS4 d:\jenkins\workspace\MPA_jenkins-fixing>echo echotestresponse 
          17:51:33  echotestresponse
          17:51:33  
          17:51:33  [Pipeline] powershell
          17:51:34  [Pipeline] echo
          17:51:34  output from powershell: echotestresponse
          17:51:34  
          17:51:34  [Pipeline] }
          17:51:34  [Pipeline] // script
          17:51:34  [Pipeline] }
          17:51:34  [Pipeline] // stage 

          Code

          stage('Test') {
            steps {
              script {
                def result = bat(label: '', returnStdout: true, script: 'echo echotestresponse')
                sleep(time:5, unit:"SECONDS")
                echo "output from bat: $result"
                def psResult = powershell(label: '', returnStdout: true, script: 'echo echotestresponse')
                echo "output from powershell: $psResult"
              }
            }
          } 

           

          Daniel added a comment - I have seen the same and it confused me too. So I added a sleep between the echo and the bat execution. See the output: 17:51:28 [Pipeline] stage 17:51:28 [Pipeline] { (Test) 17:51:28 [Pipeline] script 17:51:28 [Pipeline] { 17:51:28 [Pipeline] bat 17:51:28 [Pipeline] sleep 17:51:28 Sleeping for 5 sec 17:51:33 [Pipeline] echo 17:51:33 output from bat: 17:51:33 jenkins@HMJENKINS4 d:\jenkins\workspace\MPA_jenkins-fixing>echo echotestresponse 17:51:33 echotestresponse 17:51:33 17:51:33 [Pipeline] powershell 17:51:34 [Pipeline] echo 17:51:34 output from powershell: echotestresponse 17:51:34 17:51:34 [Pipeline] } 17:51:34 [Pipeline] // script 17:51:34 [Pipeline] } 17:51:34 [Pipeline] // stage Code stage( 'Test' ) { steps { script { def result = bat(label: '', returnStdout: true , script: ' echo echotestresponse') sleep(time:5, unit: "SECONDS" ) echo "output from bat: $result" def psResult = powershell(label: '', returnStdout: true , script: ' echo echotestresponse') echo "output from powershell: $psResult" } } }  

          Markus Winter added a comment -

          I suggest to quote the output. Then it is clear that the variable is properly set.

          stage('Test') {
            steps {
              script {
                def result = bat(label: '', returnStdout: true, script: 'echo echotestresponse')
                sleep(time:5, unit:"SECONDS")
                echo """output from bat: 
          '$result'
          After output
          """
                def psResult = powershell(label: '', returnStdout: true, script: 'echo echotestresponse')
                echo "output from powershell: $psResult"
              }
            }
          } 
          

          This result in:

          [Pipeline] bat
          [Pipeline] sleep
          Sleeping for 5 sec
          [Pipeline] echo
          output from bat: 
          '
          c:\Jenkins\agent\workspace\pipeline>echo echotestresponse 
          echotestresponse
          '
          After output
          
          [Pipeline] }
          [Pipeline] // node
          [Pipeline] End of Pipeline
          Finished: SUCCESS
          
          

          Markus Winter added a comment - I suggest to quote the output. Then it is clear that the variable is properly set. stage( 'Test' ) { steps { script { def result = bat(label: '', returnStdout: true , script: ' echo echotestresponse') sleep(time:5, unit: "SECONDS" ) echo """output from bat: '$result' After output """ def psResult = powershell(label: '', returnStdout: true , script: ' echo echotestresponse') echo "output from powershell: $psResult" } } } This result in: [Pipeline] bat [Pipeline] sleep Sleeping for 5 sec [Pipeline] echo output from bat: ' c:\Jenkins\agent\workspace\pipeline>echo echotestresponse echotestresponse ' After output [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Finished: SUCCESS

          Daniel added a comment - - edited

          Interesting. There seems to be a change in the bat step which causes to echo the command line in the result. This was not the case before.

          I checked quoting the output between ==> and <== for our maven call and it can be seen clearly.

          19:22:31  maven execution result==>jenkins@HMJENKINS4 d:\jenkins\workspace\MPA_jenkins-fixing>mvn help:evaluate -Dexpression=project.version -q -DforceStdout 
          19:22:31  3.28.0-SNAPSHOT<== 

          In older Jenkins versions this worked properly and only returned the second line.

          Daniel added a comment - - edited Interesting. There seems to be a change in the bat step which causes to echo the command line in the result. This was not the case before. I checked quoting the output between ==> and <== for our maven call and it can be seen clearly. 19:22:31 maven execution result==>jenkins@HMJENKINS4 d:\jenkins\workspace\MPA_jenkins-fixing>mvn help:evaluate -Dexpression=project.version -q -DforceStdout 19:22:31 3.28.0-SNAPSHOT<== In older Jenkins versions this worked properly and only returned the second line.

          Markus Winter added a comment -

          Which versions of https://plugins.jenkins.io/durable-task/ and https://plugins.jenkins.io/workflow-durable-task-step/ do you have installed before upgrading Jenkins to 2.401.1

          This is not related to the core version.

          Markus Winter added a comment - Which versions of https://plugins.jenkins.io/durable-task/ and https://plugins.jenkins.io/workflow-durable-task-step/ do you have installed before upgrading Jenkins to 2.401.1 This is not related to the core version.

          Markus Winter added a comment -

          If you don't want the first line to be printed you might just use

           def result = bat(label: '', returnStdout: true, script: '@echo echotestresponse')
          

          I don't think that there was a change in behaviour. 

          Markus Winter added a comment - If you don't want the first line to be printed you might just use def result = bat(label: '', returnStdout: true , script: ' @echo echotestresponse') I don't think that there was a change in behaviour. 

          Daniel added a comment -

          Yes that works as expected. But there was clearly a change between our last Jenkins deployment and the current LTS version. Immediately after update all our builds broke.

          It wasn't even needed to trim the output, it just worked without additional new lines. It was exactly the output of the command before, nothing else.

           

          Do you have any pointers to the source code for this bat-step? I would like to investigate that further.

          Daniel added a comment - Yes that works as expected. But there was clearly a change between our last Jenkins deployment and the current LTS version. Immediately after update all our builds broke. It wasn't even needed to trim the output, it just worked without additional new lines. It was exactly the output of the command before, nothing else.   Do you have any pointers to the source code for this bat-step? I would like to investigate that further.

          Daniel added a comment -

          I changed the bug report to reflect the new findings.

          Daniel added a comment - I changed the bug report to reflect the new findings.

          Daniel added a comment -

          Since the documentation clearly states that @ should be added I will close that bug report. Even though I don't understand why it worked in previous versions without problem.

          Executes a Batch script. Multiple lines allowed. When using the returnStdout flag, you probably wish to prefix this with @, lest the command itself be included in the output. 

          Thanks for your help.

          Daniel added a comment - Since the documentation clearly states that @ should be added I will close that bug report. Even though I don't understand why it worked in previous versions without problem. Executes a Batch script. Multiple lines allowed. When using the returnStdout flag, you probably wish to prefix this with @, lest the command itself be included in the output. Thanks for your help.

            Unassigned Unassigned
            dko Daniel
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: