Jenkins Jobs Windows powershell脚本$ lastexitcode没有让jenkins工作失败

时间:2016-11-18 13:11:37

标签: windows powershell jenkins

if (Test-Path "c:\Temp\some.msi")
{
    start-process  -Wait -FilePath "msiexec.exe" -ArgumentList -somearguments
}   
else        
{
    Write-Host "MSI does not exist at the specified location....c:\Temp\"
    exit 1
}

$lastexitcode
  1. .msi在指定的路径中不存在,它会转到else,并打印else中的内容。
  2. 现在我添加了代码EXIT 1,$ lastexitcode的值将是1.但jenkins windows powershell作业显示已完成:成功。
  3. 有人可以指出我做错了什么。我想要jenkins工作失败的标记。我是詹金斯的新手。

1 个答案:

答案 0 :(得分:1)

您在退出前忘记了;

if (Test-Path "c:\Temp\some.msi") { start-process -Wait -FilePath "msiexec.exe" -ArgumentList -somearguments}
else { Write-Host "MSI does not exist at the specified location....c:\Temp\"; Exit 1 }

所以Jenkins中的PowerShell插件非常迟钝,你必须强制退出代码,这样才能理解错误。我发现为我工作的是:

if ($error) { $error; exit 1 }

$error是由PowerShell引擎填充的变量,其中包含执行期间发生的错误。所以如果它存在>有错误>我们需要使构建失败。当然它不适用于try \ catch块。但作为替代方案。