How Do I Get Psake to Return Non-Zero Return Code on Build Failure #58

Closed
bjmarte opened this Issue Mar 1, 2013 · 10 comments

Comments

Projects
None yet
8 participants
@bjmarte

bjmarte commented Mar 1, 2013

We can't figure out the proper way to get the newer versions of Psake tor return an error code when the build fails. We need this to happen in order to correctly integrate with our CI system (cruisecontrol.net, but I think it would be the same problem with any CI tool). All of the Wiki articles about integrating with CI tools are either way out of date or just don't make sense to us. We had to modify the end of the catch block in the "Invoke-psake" method as follows:

Before Modification:

    if (!$psake.run_by_psake_build_tester) {
        # if we are running in a nested scope (i.e. running a psake script from a psake script) then we need to re-throw the exception
        # so that the parent script will fail otherwise the parent script will report a successful build 
        $inNestedScope = ($psake.context.count -gt 1)
        if ( $inNestedScope ) {
            throw $_
        } else {
            Write-ColoredOutput $error_message -foregroundcolor Red
        }

    }

After Modification:

            throw $_

We could probably add back in the part about the build tester, I think the issue is with the nested scope, but we are using the tester so we stopped tweaking after we got correct failures.

Is psake just not meant to return the error all the way out? Is there some magic property to set to get this to happen? I've seen mention of magic properties in a few articles but they are all old and we couldn't find references to such properties in the actual code.

@JamesDawson

This comment has been minimized.

Show comment Hide comment
@JamesDawson

JamesDawson May 3, 2013

Depending on how you are doing the logging within your CI tool (this was from a TFS scenario where we didn't rely on STDOUT anyway), you might find a variant on the following works for you...

Wrap the call to Invoke-Psake inside of another PowerShell script:

run-psake.ps1

Import-Module psake
Invoke-Psake .\default.ps1
return $psake.build_success

Then from inside the CI tool, we ran the above script thus:

powershell.exe -NonInteractive -Command "&{ $result = & .\run-psake.ps1; exit !$result }"

Thinking about it, if you were willing to have a complicated command-line in your CI tool configuration then you could skip the run-psake.ps1 script and potentially avoid losing STDOUT output:

powershell.exe -NonInteractive -Command "&{ Import-Module psake; Invoke-Psake .\default.ps1; exit !($psake.build_success) }"

Depending on how you are doing the logging within your CI tool (this was from a TFS scenario where we didn't rely on STDOUT anyway), you might find a variant on the following works for you...

Wrap the call to Invoke-Psake inside of another PowerShell script:

run-psake.ps1

Import-Module psake
Invoke-Psake .\default.ps1
return $psake.build_success

Then from inside the CI tool, we ran the above script thus:

powershell.exe -NonInteractive -Command "&{ $result = & .\run-psake.ps1; exit !$result }"

Thinking about it, if you were willing to have a complicated command-line in your CI tool configuration then you could skip the run-psake.ps1 script and potentially avoid losing STDOUT output:

powershell.exe -NonInteractive -Command "&{ Import-Module psake; Invoke-Psake .\default.ps1; exit !($psake.build_success) }"
@KyleGobel

This comment has been minimized.

Show comment Hide comment
@KyleGobel

KyleGobel Dec 12, 2013

Somewhat related

all my builds are showing as Build Succeeded, even though they are not

task -name Build -description "builds outdated source files" -action {
        Exec {
                        msbuild $srcFolder/$solutionName /t:Build "/p:Configuration=$config" /v:minimal /nologo
        }
};

Shows the compile errors still, but ends with Build Succeeded!, which doesn't allow a CI server much to work with, does psake rely on msbuild verbosity level being set on something specific? or am i doing something else wrong here.

Somewhat related

all my builds are showing as Build Succeeded, even though they are not

task -name Build -description "builds outdated source files" -action {
        Exec {
                        msbuild $srcFolder/$solutionName /t:Build "/p:Configuration=$config" /v:minimal /nologo
        }
};

Shows the compile errors still, but ends with Build Succeeded!, which doesn't allow a CI server much to work with, does psake rely on msbuild verbosity level being set on something specific? or am i doing something else wrong here.

@damianh

This comment has been minimized.

Show comment Hide comment
@damianh

damianh Mar 31, 2014

Contributor

@KyleGobel Is this still an issue? We don't have problems with exec failing the build with non-zero exit codes. https://github.com/damianh/psake/blob/master-ps3/specs/using_exec_and_nonzero_lastexitcode_should_fail.ps1

Closing. Please re-open if you have a minimal repro that demonstrates the problem. Cheers.

Contributor

damianh commented Mar 31, 2014

@KyleGobel Is this still an issue? We don't have problems with exec failing the build with non-zero exit codes. https://github.com/damianh/psake/blob/master-ps3/specs/using_exec_and_nonzero_lastexitcode_should_fail.ps1

Closing. Please re-open if you have a minimal repro that demonstrates the problem. Cheers.

@damianh damianh closed this Mar 31, 2014

@KyleGobel

This comment has been minimized.

Show comment Hide comment
@KyleGobel

KyleGobel Apr 2, 2014

nope, this issue i believe was fixed when a pull request a couple months back was reverted.

thanks

nope, this issue i believe was fixed when a pull request a couple months back was reverted.

thanks

@damianh

This comment has been minimized.

Show comment Hide comment
@damianh

damianh Apr 2, 2014

Contributor

Thanks for letting me know.

Contributor

damianh commented Apr 2, 2014

Thanks for letting me know.

@mcobrien

This comment has been minimized.

Show comment Hide comment
@mcobrien

mcobrien Aug 14, 2014

Maybe I'm misunderstanding the issue, but this still doesn't seem to work for me. With the following default.ps1, running Invoke-Psake prints an error message, then returns $? as True and $LASTEXITCODE as 0.

task default -depends compile
task compile {
    exec { dir does-not-exist }
}

Similarly, this default.ps1 returns success.

task default -depends compile
task compile {
    throw "An error occurred"
}

Maybe I'm misunderstanding the issue, but this still doesn't seem to work for me. With the following default.ps1, running Invoke-Psake prints an error message, then returns $? as True and $LASTEXITCODE as 0.

task default -depends compile
task compile {
    exec { dir does-not-exist }
}

Similarly, this default.ps1 returns success.

task default -depends compile
task compile {
    throw "An error occurred"
}
@earldean

This comment has been minimized.

Show comment Hide comment
@earldean

earldean Jul 26, 2016

I am using Jenkins, and I had this problem. When an error occurs I use exec { cmd /c exit (1) }

Jenkins does not fail when exceptions are thrown from a Psake script.

I am using Jenkins, and I had this problem. When an error occurs I use exec { cmd /c exit (1) }

Jenkins does not fail when exceptions are thrown from a Psake script.

@drinkbird

This comment has been minimized.

Show comment Hide comment
@drinkbird

drinkbird Dec 5, 2016

@JamesDawson Thanks for the solution.

I'm using a script to bootstrap psake and invoke the actual build script. Adding exit (!$psake.build_success) as a last statement of the bootstrapper script did the trick.

@JamesDawson Thanks for the solution.

I'm using a script to bootstrap psake and invoke the actual build script. Adding exit (!$psake.build_success) as a last statement of the bootstrapper script did the trick.

@Midacts

This comment has been minimized.

Show comment Hide comment
@Midacts

Midacts Sep 15, 2017

@drinkbird Learning about the $psake variable and your reply helped me figure out my TFS wohs.
Thanks for your help.

Midacts commented Sep 15, 2017

@drinkbird Learning about the $psake variable and your reply helped me figure out my TFS wohs.
Thanks for your help.

@drinkbird

This comment has been minimized.

Show comment Hide comment
@drinkbird

drinkbird Sep 15, 2017

Much appreciated @Midacts :)

Much appreciated @Midacts :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment