1. Overview

Jenkins pipeline is a powerful tool used to automate software development processes. Developers use it to build, test, distribute, publish, and deploy software. In addition to this, the Jenkins pipeline permits developers to create a repeatable and scalable process that can be easily modified as their software development needs change.

In this tutorial, we’ll discuss how to get the output of a shell command executed in the pipeline.

2. Importance of Capturing Output

The shell command is a powerful feature of the Jenkins pipeline that allows developers to execute shell scripts within their pipeline script. The Jenkins agent executes shell commands and prints output to the Jenkins console. A Jenkins pipeline uses variables to store data for access and modification throughout the script.

We can define variables either at the top of the pipeline script or within a stage block. Capturing the output of a shell command into a variable is important as it permits developers to use the output of one shell command as input for another. Hence, developers can use the pipeline script to store the output of a shell command.

3. Using the sh returnstdout

The sh step command is a built-in Jenkins pipeline step that allows developers to execute a shell command and capture its output into a variable. Using shell commands, we can assign variables to strings. In a Jenkins pipeline, we can use the sh step to execute a shell command within a pipeline. Let’s look at a pipeline job to execute a command and store its output:

pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                script {
                    def output = sh(returnStdout: true, script: 'pwd')
                    echo "Output: ${output}"
                }
            }
        }
    }
}

The pipeline job above defines a stage “Example” along with its steps to execute, including the script element. This special step enables the execution of arbitrary Groovy code within the pipeline. Also, it is utilized to define a Groovy closure that captures shell output. Here, we performed the pwd command inside the script element of the pipeline using the sh returnStdout method. Also, we stored the result in the output variable. The option “returnStdout: true” instructs Jenkins to return the standard output of the shell command. The option “script: ‘pwd'” specifies the shell command to execute.

Finally, the echo command prints the value of the “output” variable to the Jenkins console using the echo step.

4. Using Command Substitution

Command substitution is a shell feature that allows developers to execute a command within a string and use its output as part of the string. In the Jenkins pipeline, we can use command substitution to capture the output of a shell command into a variable:

pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                script {
                    def output = sh(script: "echo \$(ls)", returnStdout: true)
                    echo "Output: ${output}"
                }
            }
        }
    }
}

In this case, the sh step executes the command “echo $(ls)” and captures its output into a variable using command substitution. Furthermore, the pipeline job captures the output of the “ls” command into the “output” variable and prints it to the Jenkins console upon execution.

5. Other Possible Cases

Jenkins pipeline provides two other important options which are very useful in storing the result to further use in the pipeline job. The returnStatus method captures the exit status of a shell command, whereas returnStdoutTrim is helpful in trimming the output for further use.

5.1. Using the returnStatus

The Jenkins pipeline’s sh step uses the returnStatus option to capture the exit status of a shell command. By default, the sh step returns the standard output of the shell command as the result of the step. However, when returnStatus is set to true, the exit status of the shell command is returned instead of the standard output.

Let’s look at the pipeline job with returnStatus method:

pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                script {
                    def status = sh(returnStatus: true, script: 'ls /test')
                    if (status != 0) {
                        echo "Error: Command exited with status ${status}"
                    } else {
                        echo "Command executed successfully"
                    }
                }
            }
        }
    }
}

In this example, the sh step executes the ls command and captures the output into the status variable using returnStatus. Since the directory /test doesn’t exist, the ls command will exit with a non-zero status, indicating an error. The if statement checks if the status variable is non-zero, and if so, prints an error message to the Jenkins console.

Developers can perform error checking by capturing the exit status of shell commands. Furthermore, they can also implement logic based on the success or failure of a command.

5.2. Using the Trim Method

In the following snippet The returnStdout parameter is set to true, which means that the standard output of the command will be captured and returned as the result of the step. Additionally, we can also use the trim() method to get the cleansed output of the command in a variable:

pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                script {
                  def output = sh(returnStdout: true, script: 'echo "    hello    "').trim()
                  echo "Output: '${output}'"
                }
            }
        }
    }
}

The trim() method will remove any leading and trailing whitespace characters from the output of the echo command. Furthermore, the Jenkins console will display the resulting output enclosed in single quotes to highlight any whitespace characters.

6. Conclusion

In this article, we discussed two different methods for capturing the output of a shell command executed in the Jenkins pipeline into a variable.

First, we looked into capturing the output using sh returnStdout. After that, we explored the same using command substitution. Both methods can capture the output of a shell command. Despite this, the returnStdout method of sh is easier to read and simpler than command substitution.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.