Check the status of Invoke-Build call
GreatTeacherBasshead opened this issue · comments
Hi Roman,
I use Atlassian's Bamboo as a build server, and it has some "limitations" and "nuances"...
For instance, when I call Invoke-Build @params
, and a validation of a parameter fails, it thinks that its ok:
Invoke-Build.ps1: E:\builds\Cleanup.build.ps1:22
Line |
22 | Invoke-Build @params
| ~~~~~~~
| Cannot validate argument on parameter 'workingDir'. The " Test-Path $_ " validation script for
| the argument with value "B:\" did not return a result of True. Determine why the validation script
| failed, and then try the command again.
Finished task 'Clean Branches' with result: Success
I found out that I can call it like this:
Invoke-Build @params -Result result
if ($result.Error) {
exit 1
}
then I have the right state:
Invoke-Build.ps1: E:\builds\Cleanup.build.ps1:22
Line |
22 | Invoke-Build @params -Result result
| ~~~~~~~
| Cannot validate argument on parameter 'workingDir'. The " Test-Path $_ " validation script for
| the argument with value "B:\" did not return a result of True. Determine why the validation script
| failed, and then try the command again.
Failing task since return code of [C:\Program Files\PowerShell\7\pwsh.exe .\Cleanup.build.ps1 CleanBranches] was 1 while expected 0
Finished task 'Clean Branches' with result: Failed
So my question is: is it a proper way (using $result.Error
) to check the build status?
Yes, in your scenario (see below alternative), it is the legit way.
Invoke-Build
cannot do much about it. It is called for getting the dynamic parameters, this call succeeds. Then PowerShell validates and finds the parameters invalid and writes an error. Unfortunately this error is not terminating by PowerShell design.
But you can tell PowerShell to write the terminating error. Set $ErrorActionPreference = 'Stop'
before calling Invoke-Build
. In this case you do not have to use -Result
and check for error. The call will fail, just like on other "normal" build failures.
Thanks!