[英]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.