1

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.

The Spartan
  • 411
  • 1
  • 3
  • 13

1 Answers1

1

By storing in variable?

./my_build_script.sh
get_status=$?

Now you can do something with variable: $get_status

  • Will that variable be available at the pipeline script-level? Say, in another stage in the pipeline, I wanted to say something like `when { expression { $get_status == 200 } }`. – The Spartan Jan 22 '18 at 16:15
  • Yes, see this link [pass-variables-between-stages](https://stackoverflow.com/questions/44099851/how-do-i-pass-variables-between-stages-in-a-declarative-jenkins-pipeline) – network_newbie Jan 22 '18 at 16:34
  • In that example, the variable is set at the pipeline level, not the "Execute shell" level down in the job that the pipeline runs. – The Spartan Jan 22 '18 at 16:37
  • The other easiest way, is to save variable value in file: `echo $get_status > status_file.txt` And you can get this file everywhere in your pipeline. In other stage you will be able to do: `get_status=$(cat status_file.txt)` – network_newbie Jan 22 '18 at 16:42
  • Sure, that works, but it's not a very elegant solution (clutters disk space, I/O). I'll keep looking. – The Spartan Jan 22 '18 at 16:48
  • Of course! there are also memory I/O;, like: `export $get_status` and it will be your linux session temporarily env variable – network_newbie Jan 22 '18 at 16:52