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

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

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved (View Workflow)
    • Priority: Minor
    • Resolution: Not A Defect
    • Component/s: durable-task-plugin
    • Labels:
    • Environment:
    • Similar Issues:

      Description

      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

        Attachments

          Activity

          Hide
          fnaum 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
              } 
          }
          Show
          fnaum 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 } }
          Hide
          jglick Jesse Glick added a comment -

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

          Show
          jglick Jesse Glick added a comment - IIRC you can use @echo off . But probably best to use powershell .
          Hide
          fnaum 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

          Show
          fnaum 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
          Hide
          jglick 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”.

          Show
          jglick 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”.

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            fnaum Federico Naum
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: