Step Template stops execution if function from script module manually return exit code 0

Hi.

I have the following scenario:

A Step Template calls two powerhell functions from a Script Module :
Function-1
Function-2

Now, both Function-1 and Function-2 calls a Windows Program that does not (necessarily) return 0 when it executes sucessfully. Therefore I return 0 manually when approriate, by executing: Exit 0.

What then happens, is that the step is green (successful) but Function-2 is never called.

If I flip the order the functions are executed in the Step-Template, so that Function-2 is called before Function-1, then Function-2 is executed successfully and Function-1 is never called. So both functions are working by them selves.

When I simplify Function-1 and Function-2, I discover that doing Exit 0 effectively stops execution of subsequent code in the Step Template. You can reproduce the scenario by calling the following simplified functions from a Step Template:

function Function-1()
{
Write-output "Executing Function-1…"
Exit 0
}

function Function-2()
{
Write-output "Executing Function-2…"
Exit 0
}

Versions:
Octopus server version: 3.2.5
Tentacle version : 3.2.5
Calimari: 3.1.16

Please advise how to handle this.

HI,

Thanks for reaching out. This is a 100% Powershell question rather than an Octopus one.

If you run the command Exit , It’ll always exit the current script execution. That’s why the 2nd function is never executed.

You might wanna try something like this

function Function-1() 
{ 
    Write-output "Executing Function-1..."

    #This batch file only has the code "Exit 1" on it
    & C:\Tools\1.bat
    
    #Variable $LastExitcode holds the exit code of the last execution. In this case that would be our batch
    If($LASTEXITCODE -ne 0){

        #Using RETURN instead of EXIT to avoid exiting the script execution
        return 0
    }
    
    
}

function Function-2() 
{ 
    Write-output "Executing Function-2..." 
    #This batch file only has the code "Exit 0" on it
    & C:\Tools\0.bat

    #Variable $LastExitcode holds the exit code of the last execution. In this case that would be our batch
    If($LASTEXITCODE -ne 0){

        #Using RETURN instead of EXIT to avoid exiting the script execution
        return 0
    }
    
}

Please keep in mind that I didn’t test this script, but it should give you a better idea on how to approach this.

Regards,
Dalmiro

Ok. I did not catch that the scope of running the script is the Step Template and not the function and the external program executed in the function. It is obvious now that you explained it, thank you :slight_smile:

I have tested a bit with return 0 instead of exit 0. It does not really help. Each function returns 0 alright, but the Step Template still gets aware of the LASTEXITCODE behind the scenes, and fails the step if LASTEXITCODE is other than 0, regardless of the fact that each function returns 0.

Any other tricks you can think of?

I can do a work around of merging the two functions into one, but it kind of limits the posibility of smaller, reusable functions that can flow nicely.

Hi,

As soon as the first command fails, it writes to the error stream, Octopus notices that and fails the step.

I just tried this very dirty workaround (with my non-existing batch skills) and it seems to be working:

These are the 4 files I used to test

0.bat

EXIT 0

1.bat (the one that fails)

EXIT 1

Call0.bat

C:\Tools\0.bat
IF %ERRORLEVEL% EQU 1 EXIT 0

Call1.bat

C:\Tools\1.bat
IF %ERRORLEVEL% EQU 1 EXIT 0

The Powershell step in Octopus has

& "C:\Tools\Call1.bat"
& "C:\Tools\Call0.bat"

Think you can give that a try?

Forgot screenshots