0

Background:

Here is a snippet of code: In this example, this makefile will build:

def build() {

    command = """ #!/bin/bash
    ls -la
    make clean || :
    make all -k 2>&1 
    """

    try {
        result = sh(returnStdout: true, script: command)
    }

}

Let's assume that the make was unsuccessful. We can create a failed build by adding a -z flag.

def build_fail() {

    command_fail = """ #!/bin/bash
    ls -la
    make clean || :
    make all -k -z 2>&1 
    """

    try {
        result_fail = sh(returnStdout: true, script: command_fail)
        println (result_fail)
    }

    catch (Exception e) {
        println (e)
    }
// It will return back Jenkins error code 2 only, but I need more details 

}

The issue

When running the second snippet of code, I need to be able to see not just the error code of successful or not successful, but the information along with it.

I need

  1. To see where the makefile is failing in stdout and
  2. Write the message to a file

I can see that the build is failing because of the -z flag but it's not actually being printed from stdout, it's from the shell script. Eg:

What didn't work

  1. Redirecting stderr to stdout (snippet 2 doesn't work)

  2. Capturing using script fileName... exit I try to capture the output by enclosing it in script fileName ... exit

    try {
        script output.txt
        result_fail = sh(returnStdout: true, script: command_fail)
        exit
    }

The output was:

It does not describe what is actually happening inside the process.

  1. Pipes and Tee

I tried using pipes and Tees from answers around stackoverflow and other stackexchange site but when redirecting input to files and doing cat fileName, they would be empty.

Gerold Broser
  • 13,129
  • 4
  • 41
  • 97
Ditto
  • 11
  • 2
  • 4
  • Take a look at [JENKINS-44930](https://issues.jenkins.io/browse/JENKINS-44930?focusedCommentId=326165&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel), it is has some useful workarounds. – Noam Helmer Oct 25 '21 at 16:17
  • Does [How to return stdout and stderr together with the status from a Jenkins Pipeline sh script step](https://stackoverflow.com/a/68967643/1744774) help you? – Gerold Broser Oct 25 '21 at 19:49
  • @Ditto, i can't understand an issue. `Can not capture output of Jenkins shell script` - capture where? capture into variable or you want to see it in jenkins job log? – daggett Oct 25 '21 at 22:32

0 Answers0