The Spartan
The Spartan

Reputation: 421

How can I capture the exit code of a job's "Execute shell" build step for use later in a pipeline?

I have a pipeline script that executes a build as one of its steps. Let's say it looks like this:

pipeline{
    stages {
        stage('Build'){
            steps{
                node('master'){
                    build job: 'my_build'
                }
            }
        }
        (other stages.........)
    }
}

Occasionally, that step ("Build") fails. I have a shell script in the "Build" job (an "Execute shell" step) that exits with a certain return code that I then exit the step with, like so:

#!/bin/bash
./my_build_script.sh
exit $?

The problem is, I'm not sure how to capture that exit code for use later in the pipeline.

What I'd like to achieve is only retrying the build on a particular exit code from that "Execute shell" step.

Upvotes: 1

Views: 765

Answers (1)

network_newbie
network_newbie

Reputation: 166

By storing in variable?

./my_build_script.sh
get_status=$?

Now you can do something with variable: $get_status

Upvotes: 1

Related Questions