Currently sh has no meaningful return value, and throws an exception if the exit status is not zero. Would be nice to have an option to have it return the exit code (zero or not) as an integer value:

      def r = sh script: 'someCommand', returnStatus: true
      

      Current workaround:

      sh 'someCommand; echo $? > status'
      def r = readFile('status').trim()
      

      Or to have it return its standard output (akin to shell backticks):

      def lines = sh(script: 'dumpStuff.sh', returnStdout: true).split("\r?\n")
      

      Workaround:

      sh 'dumpStuff.sh > result'
      def lines = readFile('result').split("\r?\n")
      

      Or to have it take something on standard input:

      sh script: 'loadStuff.sh', stdin: someText
      

      Workaround:

      writeFile file: 'input', text: someText
      sh 'loadStuff.sh < input'
      

      Probably requires some API changes in durable-task.

          [JENKINS-26133] Shell script taking/returning output/status

          Jesse Glick created issue -
          Jesse Glick made changes -
          Link New: This issue is duplicated by JENKINS-26812 [ JENKINS-26812 ]
          Jesse Glick made changes -
          Link New: This issue is duplicated by JENKINS-28302 [ JENKINS-28302 ]

          Jesse Glick added a comment -

          Capture of exit status as a return value can be handled directly in DurableTaskStep.Execution, by simply returning Integer rather than returning void or throwing AbortException.

          Capture of standard output is a little trickier because we would like to continue to direct standard error to the build log, and durable-task controllers currently mix them in the same stream. The API likely needs to be changed so that when launching the process you can request separate streams. (jenkins-out.txt vs. jenkins-err.txt I suppose, and two log position fields.) DurableTaskStep.Execution could then maintain a ByteArrayOutputStream-like buffer (but Serializable) collecting the output to be returned. Alternately, the controller could just return the complete standard output when the process exits, alongside the status code, which would make for a simpler API change, under the assumption that future clients do not want to stream output.

          Likewise, sending standard input would require an API addition in durable-task. Streaming the input would be very awkward for a durable task (providing an InputStream is not possible) so you would probably have to provide the byte[] content up front.

          Both API changes could potentially be avoided by just making DurableTaskStep.Execution modify the script to use redirects as shown in the workarounds here, using a random temporary file. There are some tricky parts here, too, though: the file ought to be outside the workspace to avoid clashes with scripts which expect to “own” the whole directory tree (cf. discussion in JENKINS-27152); and to handle multiline scripts, or even scripts with shebangs (#!/usr/bin/python), you really need to save the user-provided script into another temporary file, then do a little dance to make it into a complete executable that could be called from the redirecting wrapper script (and here cf. JENKINS-29877).

          Useful also for bat. According to answers here, Bourne shell syntax works for batch scripts too.

          Jesse Glick added a comment - Capture of exit status as a return value can be handled directly in DurableTaskStep.Execution , by simply returning Integer rather than returning void or throwing AbortException . Capture of standard output is a little trickier because we would like to continue to direct standard error to the build log, and durable-task controllers currently mix them in the same stream. The API likely needs to be changed so that when launching the process you can request separate streams. ( jenkins-out.txt vs. jenkins-err.txt I suppose, and two log position fields.) DurableTaskStep.Execution could then maintain a ByteArrayOutputStream -like buffer (but Serializable ) collecting the output to be returned. Alternately, the controller could just return the complete standard output when the process exits, alongside the status code, which would make for a simpler API change, under the assumption that future clients do not want to stream output. Likewise, sending standard input would require an API addition in durable-task . Streaming the input would be very awkward for a durable task (providing an InputStream is not possible) so you would probably have to provide the byte[] content up front. Both API changes could potentially be avoided by just making DurableTaskStep.Execution modify the script to use redirects as shown in the workarounds here, using a random temporary file. There are some tricky parts here, too, though: the file ought to be outside the workspace to avoid clashes with scripts which expect to “own” the whole directory tree (cf. discussion in JENKINS-27152 ); and to handle multiline scripts, or even scripts with shebangs ( #!/usr/bin/python ), you really need to save the user-provided script into another temporary file, then do a little dance to make it into a complete executable that could be called from the redirecting wrapper script (and here cf. JENKINS-29877 ). Useful also for bat . According to answers here , Bourne shell syntax works for batch scripts too.
          Jesse Glick made changes -
          Link New: This issue is related to JENKINS-29877 [ JENKINS-29877 ]
          Jesse Glick made changes -
          Link New: This issue is related to JENKINS-27152 [ JENKINS-27152 ]
          Jesse Glick made changes -
          Link New: This issue is duplicated by JENKINS-33173 [ JENKINS-33173 ]
          Jesse Glick made changes -
          Link New: This issue is related to JENKINS-34331 [ JENKINS-34331 ]
          Jesse Glick made changes -
          Link Original: This issue is related to JENKINS-34331 [ JENKINS-34331 ]
          Jesse Glick made changes -
          Link New: This issue is duplicated by JENKINS-34331 [ JENKINS-34331 ]

            jglick Jesse Glick
            jglick Jesse Glick
            Votes:
            50 Vote for this issue
            Watchers:
            65 Start watching this issue

              Created:
              Updated:
              Resolved: