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

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

    XMLWordPrintable

Details

    • Bug
    • Status: Resolved (View Workflow)
    • Minor
    • Resolution: Not A Defect
    • durable-task-plugin

    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

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

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

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

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

          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”.
          yonnatan 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 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

          People

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

            Dates

              Created:
              Updated:
              Resolved: