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

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

    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
            diversario ilya s added a comment -

            Here's my use case for this:

            aws ecr describe-repositories --repository-names foo

            I want to check if a Docker repository exists. On success, this command returns a JSON blob in stdout. On failure, it returns a string into stderr. As of right now, what should be a simple case of pattern-matching on the stderr content when exit code != 0 turns into something completely different. I've not yet came to a solution, but it's disappointing that what looks like shell invocation step behaves nothing like the shell in practice.

            Show
            diversario ilya s added a comment - Here's my use case for this: aws ecr describe-repositories --repository-names foo I want to check if a Docker repository exists. On success, this command returns a JSON blob in stdout . On failure, it returns a string into stderr . As of right now, what should be a simple case of pattern-matching on the stderr content when exit code != 0 turns into something completely different. I've not yet came to a solution, but it's disappointing that what looks like shell invocation step behaves nothing like the shell in practice.
            Hide
            codiophile Erik Blomqvist added a comment -

            I can't believe that you've been debating this since 2017. It's a basic feature and wanting to do processing based on the return code and the output is not an "uncommon use case". Any chance we could get this ticket moving? 

            Show
            codiophile Erik Blomqvist added a comment - I can't believe that you've been debating this since 2017. It's a basic feature and wanting to do processing based on the return code and the output is not an "uncommon use case". Any chance we could get this ticket moving? 
            Hide
            uliulcarpatin Uliul Carpatin added a comment -

            +1 for this feature ..

            A solution to not break the compatibility would be to:

            • add an extra parameter, 'returnBothOutputs'
            • return as a list the two items [ returnStatusOptionContent , returnStdoutOptionContent ]
            Show
            uliulcarpatin Uliul Carpatin added a comment - +1 for this feature .. A solution to not break the compatibility would be to: add an extra parameter, 'returnBothOutputs' return as a list the two items [ returnStatusOptionContent , returnStdoutOptionContent ]
            Hide
            mlb_2010 Meghan Blanchard added a comment -

            I signed up just to be able to up-vote this ticket.  It is an incredibly fundamental requirement, I don't understand the controversy. 

            Show
            mlb_2010 Meghan Blanchard added a comment - I signed up just to be able to up-vote this ticket.  It is an incredibly fundamental requirement, I don't understand the controversy. 
            Hide
            nancyrobertson2005 Nancy Robertson added a comment -

            I like Uliul Carpatin's solution that does not break existing code.

            It or a variant thereof could also include stdErr...

            Show
            nancyrobertson2005 Nancy Robertson added a comment - I like Uliul Carpatin 's solution that does not break existing code. It or a variant thereof could also include stdErr...

              People

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

                Dates

                • Created:
                  Updated: