1

I'm teaching myself Jenkins and am trying to direct execution flow of a 'pipeline job via declarative syntax' based on result of Test Stage, which I try to implement via the pytest exit code into a shell variable (i.e. if success then send success email and deploy, if failure then send failure email and exit script..)

This works for me when I export if I set a new shell variable in the CLI. It also seems to work in this Jenkins script to the point when I receive the pytest exit code and assign it to my shell variable. However after this, when I try to read the shell variable I've just set, it seems to have a null value. Any ideas?

stage('Test') {
    steps {
        sh 'export pytestExitCode'
        sh 'echo "pytestExitCode before being assigned the pytest exit code is: "$pytestExitCode'
        sh 'pytest'
        sh 'echo "Pytest completed with exit code: "$?'
        sh 'pytestExitCode=$?'
        sh 'echo $pytestExitCode'
        sh 'echo "pytestExitCode after being assigned the pytest exit code is: "$pytestExitCode'
    }
}

1 Answer 1

1

Jenkins runs each sh step in a new shell session. Thus, exported variables will only be valid inside the script you define as parameter for the sh step.

For example:

sh 'export variable=value'
sh 'echo "variable: $variable"'

will output

[Pipeline] sh
+ export 'variable=value'
[Pipeline] sh
+ echo 'variable: '
variable: 

If you only need the variables inside the shell script, you can combine the sh steps into a single sh step. Note that Jenkins uses -xe shell flags by default so the script execution will stop on first non-zero exit code. See steps reference for details.

For example:

sh '''
    export variable=value
    echo "variable: $variable"
'''

will output

[Pipeline] sh
+ export 'variable=value'
+ echo 'variable: value'
variable: value

If you need the value in other steps, you could use groovy variables instead. For those you could use returnStatus or returnStdout parameters of the sh step. See steps reference for details.

For example:

script {
    exitCode = sh returnStatus: true, script: 'exit 3'
}
echo "$exitCode"

will output

[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ exit 3
[Pipeline] }
[Pipeline] // script
[Pipeline] echo
3

Finally, for executing step conditionally based on stage success or failure, see also the success and failure blocks provided by the pipeline or stage post sections.

Jenkins also marks stage as failed if shell script fails (i.e., returns with non-zero exit code) and skips stages following the failed stage. Thus, there is usually no need to manually check the exit code.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your detailed explanation, @kangasta. It' has been a great help. cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.