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

bat(returnStdout: true) step returns the whole command not just the stdout

    • Icon: Bug Bug
    • Resolution: Not A Defect
    • Icon: Minor Minor
    • durable-task-plugin

      On a `pipeline` or `multibranch pipeline` job 

      The bat(returnStdout: true) step returns the whole command not just the stdout

      i.e

      node('windows') {
      
          stage('Checkout'){
              git('http://github.com/test/test')
              stdout = bat(returnStdout: true, script: 'git rev-parse HEAD')
              println("stdout ################ " + stdout + " ####################")
         }
      
      }
      

      returns

       

      stdout  ################ 
      C:\hudson\workspace\test_UT_pipeline_statuses-J5YZJ3FX5VOUPIOVLCDBLV55WCESHMXOR3K32NSM442TR2P4MNEQ>git rev-parse HEAD 
      ff0f0401354a0faa6cf9c08fd513beddb85997ec
       ####################
      

      instead of just

      ff0f0401354a0faa6cf9c08fd513beddb85997ec
      

      as it is done by the sh(returnStdout: true) function

       

      Workaround will be to discard the first line of the returned string

          [JENKINS-44569] bat(returnStdout: true) step returns the whole command not just the stdout

          Federico Naum added a comment -

          for now, I'm using this workaround.

             

          def getCommandOutput(cmd) {
              if (isUnix()){
                   return sh(returnStdout:true , script: '#!/bin/sh -e\n' + cmd).trim()
               } else{
                 stdout = bat(returnStdout:true , script: cmd).trim()
                 result = stdout.readLines().drop(1).join(" ")       
                 return result
              } 
          }

          Federico Naum added a comment - for now, I'm using this workaround.     def getCommandOutput(cmd) { if (isUnix()){ return sh(returnStdout: true , script: '#!/bin/sh -e\n' + cmd).trim() } else { stdout = bat(returnStdout: true , script: cmd).trim() result = stdout.readLines().drop(1).join( " " ) return result } }

          Jesse Glick added a comment -

          IIRC you can use @echo off. But probably best to use powershell.

          Jesse Glick added a comment - IIRC you can use @echo off . But probably best to use powershell .

          Federico Naum added a comment -

          Hi Jesse,

          Thank you for your reply, I could not try powershell because it is not installed in our windows machine

          Adding @echo off does work, but still feels like a lack of consistency in the defaults with it's sh() counterpart, I mean I dot need to add set +x  to make sh not to echo the commands

          Fede

          Federico Naum added a comment - Hi Jesse, Thank you for your reply, I could not try powershell because it is not installed in our windows machine Adding @echo off does work, but still feels like a lack of consistency in the defaults with it's sh() counterpart, I mean I dot need to add set +x   to make sh not to echo the commands Fede

          Jesse Glick added a comment -

          set -x apparently sends the command name to stderr; cmd.exe apparently neglects to do that. According to http://stackoverflow.com/a/8486061/12916 you can use @echo whatever. That is about the limit of my Windows batch scripting knowledge, other than “prefer PowerShell”.

          Jesse Glick added a comment - set -x apparently sends the command name to stderr; cmd.exe apparently neglects to do that. According to http://stackoverflow.com/a/8486061/12916  you can use @echo whatever . That is about the limit of my Windows batch scripting knowledge, other than “prefer PowerShell”.

          Yonnatan Bar added a comment -

          Just use the @ Operator
          In that case stdout = bat(returnStdout: true, script: '@git rev-parse HEAD') will give you only the output inside stdout

          Yonnatan Bar added a comment - Just use the @ Operator In that case stdout = bat(returnStdout: true, script: '@git rev-parse HEAD') will give you only the output inside stdout

            Unassigned Unassigned
            fnaum Federico Naum
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: