简体   繁体   English

jenkins 上 windows。 声明性管道 jenkinsfile。 如何设置和获取正确的变量值

[英]jenkins on windows. Declarative pipeline jenkinsfile. how to set and get correct variable values

I use Jenkinsefile file to run the Stages.我使用 Jenkinsefile 文件来运行阶段。 It is in Jenkins pipeline installed on windows, Declarative pipeline.它位于 Jenkins 管道中,安装在声明性管道 windows 上。

On the begining I do:一开始我这样做:

pipeline {
    agent { label 'master'}
    environment {
        My_build_result       = 7
    }
....
Than
        stage('Test') {
            steps {
                echo 'Testing..'

                bat """
                cd Utils
                "C:\\Program Files\\MATLAB\\R2019b\\bin\\matlab.exe" -wait -nodisplay -nosplash -nodesktop -r "run('automatic_tests\\run_test.m');"
                echo %errorlevel%
                set /a My_build_result_temp = %errorlevel%
                set My_build_result = %My_build_result_temp%
                """     
                script {
                    My_build_result = bat(returnStatus:true , script: "exit (2)").trim()                                
                    echo "My_build_result ${env.My_build_result}"               
                    if (My_build_result != 0) {
                        echo "inside if"
                    }
                }
            }
        }

The variable My_build_result get value 7 at the begining Inside the bat section, it suppose to get value 0 from %errorlevel% Inside the script section it suppose to get value 2 BUT in the echo "My_build_result ${env.My_build_result}" I get print of 7 (and it goes inside the if sentense)变量 My_build_result 在开始时获得值 7 在 bat 部分中,它假设从 %errorlevel% 中获得值 0 在脚本部分中,它假设在 echo "My_build_result ${env.My_build_result}" 中获得值 2 但我得到打印7 (它进入 if 句中)

How do I define variable that can be set value in bat""" """ and in script """ """ section of the stage and also be familiar in another stages and in the post { always {.. }} at the end???如何定义可以在 bat"""""" 和脚本 """ """ 部分中设置值的变量,并且在其他阶段和帖子 { always {.. }} 中熟悉结束???

BTW: add env.before My_build_result (env.My_build_result ) does not work Thanks a lot顺便说一句:添加 env.before My_build_result (env.My_build_result ) 不起作用 非常感谢

In the first bat call, you are setting the environment variable only inside of the batch script environment.在第一个bat调用中,您仅在批处理脚本环境中设置环境变量。 Environment variable values that are assigned through set don't persist when the script ends.通过set分配的环境变量值在脚本结束时不会持续存在。 Think of these like local variables.把这些想象成局部变量。 Simply use returnStatus: true to return the last value of ERRORLEVEL .只需使用returnStatus: true即可返回ERRORLEVEL最后一个值。 There is no need to use %ERRORLEVEL% in the batch script here.此处无需在批处理脚本中使用%ERRORLEVEL%

steps {
    script {
        My_build_result = bat returnStatus: true, script: """
            cd Utils
            "C:\\Program Files\\MATLAB\\R2019b\\bin\\matlab.exe" -wait -nodisplay -nosplash -nodesktop -r "run('automatic_tests\\run_test.m');"
        """
        // My_build_result now has the value of ERRORLEVEL from the last command
        // called in the batch script. 
    }
}

In the 2nd bat call the 1st mistake is to call the trim() method.在第二个bat调用中,第一个错误是调用trim()方法。 Result type of bat step is Integer , when returnStatus: true is passed. bat步骤的结果类型为Integer ,当returnStatus: true被传递时。 The trim() method is only available when returnStdout: true is passed in which case the result type would be String . trim()方法仅在returnStdout: true传递时可用,在这种情况下,结果类型将为String The 2nd mistake is to use brackets around the exit code value.第二个错误是在退出代码值周围使用括号。 The fixed code should look like:固定代码应如下所示:

My_build_result = bat returnStatus: true, script: "exit 2"
// My_build_result now equals 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 jenkins 声明性管道中为变量设置预定义的动态名称 - How to set a pre-defined dynamic name for a variable in a jenkins declarative pipeline 纯声明性詹金斯流水线:跨阶段可变 - Pure declarative jenkins pipeline: variable across stages Jenkins 声明式流水线不支持 shell/bash 语法 - Jenkins Declarative Pipeline is not supporting shell/bash syntax 具有节点范围的Jenkins Pipeline变量 - Jenkins Pipeline variable with node scope 詹金斯(Jenkins):如何通过变量设置身体内容 - Jenkins: how to set body content from a variable "在 Jenkins 构建的 Windows Powershell 中设置的变量,在其他构建步骤中不可用" - variable set in Windows Powershell of Jenkins build, not available in other build steps 是否可以在管道中设置变量? - Is it possible to set variable in pipeline? 使用 bat (Windows) 并使用 Pipeline 通过 Jenkins 将提交消息字符串值保存在环境变量中? - Save commit message strng value in an envionment variable via Jenkins with bat (Windows) and using Pipeline? Jenkins 变量不适用于管道中的 sed 命令 - Jenkins variable not working with sed command in pipeline 如何设置“动态”变量值? - How to set “dynamically” variable values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM