You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins构建中PowerShell的LASTEXITCODE无法正常使用问题咨询

问题原因与解决办法

错误原因

Jenkinsfile基于Groovy语法解析,当你把PowerShell里的$LASTEXITCODE改成${LASTEXITCODE}时,Groovy会将其视为自身的变量去查找,而这个变量并不存在于Groovy的绑定环境中,因此抛出MissingPropertyException。

本地PowerShell执行时,$LASTEXITCODE是PowerShell的内置自动变量,用于存储上一条命令的退出码,不存在Groovy的语法解析环节,所以能正常运行。

解决办法

要让$LASTEXITCODE被PowerShell正确识别,需要避免Groovy对其进行插值解析,有两种常用方式:

  • 转义美元符号:在Groovy中用\$转义$,让PowerShell接收原始的$LASTEXITCODE变量:
\\check_pycodestyle.bat 2>&1 | tee pycodestyle.log; if (\$LASTEXITCODE -ne 0) {Write-Output "it worked again"}
  • 用单引号包裹命令:Groovy中用三个单引号(''')包裹整个PowerShell命令,单引号内的内容不会被Groovy插值,直接传递给PowerShell执行:
powershell '''
\\\\check_pycodestyle.bat 2>&1 | tee pycodestyle.log; if ($LASTEXITCODE -ne 0) {Write-Output "it worked again"}
'''

内容的提问来源于stack exchange,提问作者programmer_04_03

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 22:37:45