Exit code <> 0 in a "Powershell script" step doesn't fail the deployment

Hi,

I have not been able to fail a deployment when an error occurs in a powershell script that is executed inside a “Powershell script” step.

According to this blog post http://octopusdeploy.com/blog/powershell-exit-codes a simple “exit 1” in my powershell script should do the trick but this only seems to work with the scripts that are automatically triggered as part of a package deployment (e.g. PostDeploy.ps1).

Neither did setting the OctopusTreatWarningsAsErrors variable to True solve my case.

Any ideas?

Niek.

Hi Neik, we’ll try to get a fix out for this in the next couple of days.

Paul

Hi Paul,

Thanks that would be great.

I wonder how other people have managed to avoid this issue. I’m currently migrating a fully manual deployment process into a sequence of automated and manual steps in Octopus which makes that I can’t simply include all automated steps into the *Deploy.ps1 scripts of the package.

BTW I really like the tool you’re working on! Great feature set and excellent service :slight_smile:

Regards,

Niek.

Hi Niek,

I just tried to reproduce this but it appears to work fine. Here is the script I used in my Script Step:

write-host "Hello"
exit -1

When I run the deployment, I get:

Execute step PS against machine local
2013-07-19 00:06:17 INFO   Begin script run
2013-07-19 00:06:17 DEBUG  Running script against tentacle http://localhost:10933/
2013-07-19 00:06:19 ERROR  Running job on the tentacle failed:
2013-07-19 00:06:19 INFO   Tentacle error message: PowerShell script returned a non-zero exit code: -1
2013-07-19 00:06:19 INFO   Tentacle output follows:

2013-07-19 00:06:18 INFO   Hello
2013-07-19 00:06:18 INFO   ==============================================
2013-07-19 00:06:18 INFO   PowerShell exit code: -1
2013-07-19 00:06:18 INFO   ==============================================
2013-07-19 00:06:18 ERROR  Octopus.Shared.Activities.ActivityFailedException: PowerShell script returned a non-zero exit code: -1
   at Octopus.Tentacle.Services.Jobs.Script.ScriptJobExecutor.Execute(ScriptJob job, JobState state) in p:\GitHub\Octopus\source\Octopus.Tentacle\Services\Jobs\Script\ScriptJobExecutor.cs:line 55
   at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid3[T0,T1,T2](CallSite site, T0 arg0, T1 arg1, T2 arg2)
   at Octopus.Tentacle.Services.Jobs.JobQueue.RunDeploymentsOnBackgroundThread(Object ignored) in p:\GitHub\Octopus\source\Octopus.Tentacle\Services\Jobs\JobQueue.cs:line 80

And my deployment is marked as failed.

Can you check that you are running the latest release of Octopus, and that your Tentacles are also up to date?

Paul

Hi Paul,

You’re right, the exit code works just fine.

In my case the inline powershell script made a call to a ps1 file and I guess the exit -1 may not have been reached because of an exception that occurred before.

So the main take-away point here is to make sure that whatever happens, your script is able to return an exit code <> 0 when you want the deployment to fail.

In case others stumble on this, see here how my scripts look like:

@@@
param (
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$environment=$(Write-Host “Input parameter ‘environment’ is missing.”; $exit_code=2),
)

if($exit_code -gt 0)
{
exit $exit_code
}

$ErrorActionPreference = "Stop"
try
{
# do the real stuff here
}
catch
{
Write-Host “$($.Exception.Message)" -ForegroundColor Red
Write-Host "$($
.InvocationInfo.PositionMessage)” -ForegroundColor Red
exit 1
}

Thanks for the update Niek!

Thanks Niek, I was experiencing the same problem, only more frustrating as I’m triggering releases and deployments using Octo.exe which doesn’t report any errors. Amending my scripts now :slight_smile: