-1

I am working on adding a jenkins Declarative pipeline for automation testing. In the test run stage i want to extract the failed tests from the log. i am using a groovy function for extracting the test result. this function is not a part of the jenkins pipeline. It is another script file. The function works fine and it build a string containing the failure details. Inside a pipeline stage i am calling this function and assinging the returned string to another variable. But when i echo the variable value it prints empty string.

pipeline {
    agent {
        kubernetes {
            yamlFile 'kubernetesPod.yml'
        }
    }
    environment{
        failure_msg = ""
    }
    stages {
        stage('Run Test') {
            steps {
                container('ansible') {
                    script {
                        def notify = load('src/TestResult.groovy')
                        def result = notify.extractTestResult("${WORKSPACE}/testreport.xml")
                        sh "${result}"
                        if (result != "") {
                            failure_msg = failure_msg + result
                        }
                    }

                }  
            }
        }
    post {
        always {
            script {
                sh 'echo Failure message.............${failure_msg}'
                }
        }
    }
}

here 'sh 'echo ${result}'' print empty string. But 'extractTestResult()' returns a non-empty string.

Also i am not able to use the environment variable 'failure_msg' in post section it return an error 'groovy.lang.MissingPropertyException: No such property: failure_msg for class: groovy.lang.Binding'

can anyone please help me with this ?

EDIT:

Even after i fixed the string interpolation, i was getting the same error. That was because jenkins does not allow using 'sh' inside docker container. there is an open bug ticket in jenkins issue board

Sruthi CP
  • 341
  • 3
  • 13
  • 1
    Can you please try if your issue disappears if you use the correct string interpolation (https://jenkins.io/doc/book/pipeline/jenkinsfile/#string-interpolation) on your sh-command. Try: sh "${failure_msg}" – Michael Kemmerzell Nov 21 '19 at 10:14
  • Another issue could be that the variable does not exist in the post-stage-scope because it was declared in the scope of the steps-block. – Michael Kemmerzell Nov 21 '19 at 10:16
  • Agree with @mkemmerz. It's the incorrect string interpolation. In addition, `failure_msg` here is not being set as an environment variable, but Groovy global variable. The difference is that environment variables (`env.failure_msg`) are available to the sub shells called by the pipeline script whereas Groovy global variables are not. On using double quotes with `sh`, it is Groovy that interpolates the variable and not shell. – Dibakar Aditya Nov 21 '19 at 10:32

1 Answers1

2

I would suggest to use a global variable for holding the error message. My guess is that the variable is not existing in your scope.

def FAILURE_MSG // Global Variable

pipeline {
    ...
    stages {
        stage(...
            steps {
                container('ansible') {
                    script {
                        ...
                        if (result != "") {
                            FAILURE_MSG = FAILURE_MSG + result
                        }
                    }    
                }  
            }
        }
    post {
        always {
            script {
                sh "${FAILURE_MSG}" // Hint: Use correct String Interpolation
                }
        }
    }
}

(Similar SO question can be found here)

Michael Kemmerzell
  • 4,802
  • 4
  • 27
  • 43