1. Overview

Jenkins is an automation tool to build, test, run, debug and deploy software. In a Jenkins build, when an “Execute shell” step fails, the entire build may fail. Teams using Jenkins for their software deployment processes can find this frustrating.

In this tutorial, we’ll learn how to prevent Jenkins builds from failing when the “Execute shell” step fails.

2. Understanding the Problem

The “Execute shell” step allows users to execute shell commands as part of a build. These commands can include anything from compiling code to running tests. Nevertheless, if any of these commands fail, the whole build will fail. This can be frustrating if the failure isn’t critical and doesn’t prevent the rest of the build from completing successfully.

Prevention of build failures is critical to ensure a smooth software deployment workflow. Whenever a build fails, it disrupts the entire process of software development and deployment. Additionally, build failures make it difficult to find the root cause of the issue, which eventually increases further delays. Prevention of build failures can enhance the efficiency and effectiveness of the development process.

3. Using the set Command

The set +e and set -e are commands used in Unix shell scripts to regulate the behavior of an error-prone script. In the shell, the command set +e disables immediate exit when a non-zero status exits a command. This means that if any command in the script exits with a non-zero status (indicating an error), the script will continue to run rather than exit immediately.

In Jenkins, the “Execute shell” job type allows us to run shell commands as part of the build process. The combination of set +e and set -e commands tells Jenkins to continue the job even if the “Execute shell” step exits with an error. At the beginning of the script, we’ll disable the exit with set +e, and in the end, we’ll disable it with set -e to re-enable the exit on the error feature. If any commands fail after set -e, the script will exit, and any subsequent commands won’t execute. To illustrate, let’s look at the script:

$ set +e
$ ./test_script.sh
$ set -e

With the above method, the build can proceed even with any failure. Despite the advantages of this approach, it can be hard to trace the root cause.

4. Using the || true Command

In shell scripts, the || operator executes the next command only if its predecessor command fails. A true command returns a zero exit code, indicating success. The || true flag in the job tells Jenkins always to exit the “Execute shell” step with a 0 status code, indicating success. We can use || true to prevent the build from failing if the command exits with an error. Let’s add || true at the end of the test_script:

$ ./test_script.sh || true

Using this method, we won’t have any build problems, but it will hide the actual cause of the problem. Alternatively, we can use exit 0 instead of the true command to prevent build failures. To demonstrate, let’s check out the command:

$ ./test_script.sh ; exit 0

The above command will run the script and exit with a status code of 0, regardless of the script’s outcome.

5. Using the Pipeline Job

Jenkins pipelines are useful for automating and managing shell scripts. We can use try-catch blocks to handle errors and exceptions in a pipeline job. The try block contains the commands to execute. While the catch block contains the commands that are to be executed if an error occurs. This allows us to handle errors and exceptions most effectively. Let’s look at the pipeline job script:

pipeline {
    agent any
    stages {
        stage('tryCatch') {
            steps {
                script {
                    try {
                        sh 'test_script.sh'
                    } catch (e) {
                        echo "An error occurred: ${e}"
                    }
                }
            }
        }
    }
}

In our pipeline, we have defined a tryCatch stage for running the command. Within the stage, a single step runs the shell command “test_script.sh” using the sh function. The step is wrapped in a try-catch block so that if an error occurs while running the command, it will be caught. Also, an error message will be printed using the echo command, indicating that an error occurred and displaying the error message. Also, these try-and-catch blocks can only be used with pipeline jobs.

6. Conclusion

In this article, we explored several methods to prevent Jenkins build failures using the set, true, and exit commands. Also, we looked at handling errors and exceptions in the pipeline job to avoid build failures. Each method has its own pros and cons, so it is critical to choose the approach that most closely suits our needs. In addition, keeping our Jenkins environment up-to-date can also prevent build failures.

We can find the script for the try-catch pipeline job over on GitHub.

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