从脚本返回的值未分配给在jenkins声明式管道阶段中声明的变量 [英] Value returned from a script does not assigned to a variable declared in jenkins declarative pipeline stage

查看:149
本文介绍了从脚本返回的值未分配给在jenkins声明式管道阶段中声明的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为自动化测试添加jenkins声明性管道.在测试运行阶段,我想从日志中提取失败的测试.我正在使用Groovy函数提取测试结果.此功能不是詹金斯管道的一部分.这是另一个脚本文件.该函数运行良好,并构建了一个包含故障详细信息的字符串.在管道阶段内,我正在调用此函数并将返回的字符串分配给另一个变量.但是,当我回显变量值时,它会打印出空字符串.

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}'
                }
        }
    }
}

此处'sh'echo $ {result}''打印空字符串.但是'extractTestResult()'返回一个非空字符串.

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

我也无法在帖子部分使用环境变量'failure_msg',它返回错误'groovy.lang.MissingPropertyException:无此类属性:class:groovy.lang.Binding的failure_msg'

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'

有人可以帮我吗?

即使我固定了字符串插值后,我也得到了相同的结果 错误.那是因为詹金斯不允许在内部使用"sh" 泊坞窗容器. jenkins问题中有一个打开的故障单 板

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

推荐答案

我建议使用全局变量来保存错误消息.我的猜测是该变量在您的作用域中不存在.

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)

这篇关于从脚本返回的值未分配给在jenkins声明式管道阶段中声明的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆