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

Allow sh to return exit status, stdout and stderr all at once

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      Like many of the commenters on -JENKINS-26133- I'd like to be able to capture the exit status and text written to standard out at the same time.

      My current use case is calling git merge --no-edit $branches and if there was an error sending a slack notification with the output. 

      The current workaround is:

      def status = sh(returnStatus: true, script: "git merge --no-edit $branches > merge_output.txt")
      if (status != 0) {
        currentBuild.result = 'FAILED'
        def output = readFile('merge_output.txt').trim()
        slackSend channel: SLACK_CHANNEL, message: "<${env.JOB_URL}|${env.JOB_NAME}> ran into an error merging the PR branches into the ${TARGET_BRANCH} branch:\n```\n${output}\n```\n<${env.BUILD_URL}/console|See the full output>", color: 'warning', tokenCredentialId: 'slack-token'
        error 'Merge conflict'
      }
      sh 'rm merge_output.txt'

      Which works but isn't a great developer experience... It would be great if I could request an object that contained: status, stdout, and stderr.

        Attachments

          Issue Links

            Activity

            Hide
            djviking Sverre Moe added a comment - - edited

            It would be better using a specific POJO than a Map.

            def returnObj = sh(returnStatus: true, returnStdout: true, returnStderr: true, script: "git merge --no-edit $branches")
            def status = returnObj.getStatus()
            def stdout = returnObj.getStdout()
            def stderr = returnObj.getStderr()
            

            Using a Map you would need to know the keys therein, but with a POJO you can use the javadoc to know the getters.
            Perhaps the parameter flags returnStatus, returnStdout, returnStderr would not be necessary. It could just return the POJO, and you could either use it or not.

            Show
            djviking Sverre Moe added a comment - - edited It would be better using a specific POJO than a Map. def returnObj = sh(returnStatus: true , returnStdout: true , returnStderr: true , script: "git merge --no-edit $branches" ) def status = returnObj.getStatus() def stdout = returnObj.getStdout() def stderr = returnObj.getStderr() Using a Map you would need to know the keys therein, but with a POJO you can use the javadoc to know the getters. Perhaps the parameter flags returnStatus, returnStdout, returnStderr would not be necessary. It could just return the POJO, and you could either use it or not.
            Hide
            fnasser Fernando Nasser added a comment -

            I'd rather keep the switches to avoid unnecessary overhead in the cases these are not necessary.

            Show
            fnasser Fernando Nasser added a comment - I'd rather keep the switches to avoid unnecessary overhead in the cases these are not necessary.
            Hide
            benhei Benjamin Heilbrunn added a comment -

            We already encountered multiple use cases where we needed to parse STDOUT in case a certain exit code happened. Would be great to have this feature.

            Show
            benhei Benjamin Heilbrunn added a comment - We already encountered multiple use cases where we needed to parse STDOUT in case a certain exit code happened. Would be great to have this feature.
            Hide
            rkumarrb Ramkumar Bangaru added a comment -

            In my case, I need it as my pipeline scripts need to branch out to different path based on the status and if the status is successful, based on the output of it. It makes a lot of sense to have both status and output returned by this function. 

            Show
            rkumarrb Ramkumar Bangaru added a comment - In my case, I need it as my pipeline scripts need to branch out to different path based on the status and if the status is successful, based on the output of it. It makes a lot of sense to have both status and output returned by this function. 
            Hide
            joanperez juan perez added a comment -

            We are using lots of python code called from the shared library, is a pain sh only returns an error code we would need a way to bubble the error message. Those solutions sounds good.

            Show
            joanperez juan perez added a comment - We are using lots of python code called from the shared library, is a pain sh only returns an error code we would need a way to bubble the error message. Those solutions sounds good.

              People

              • Assignee:
                Unassigned
                Reporter:
                drewish andrew morton
              • Votes:
                65 Vote for this issue
                Watchers:
                60 Start watching this issue

                Dates

                • Created:
                  Updated: