Custom PowerShell steps variable scoping

I’ve been building/customizing a number of deploy steps in PowerShell, and ran into a very odd issue. I customized the VDir step template, and it wasn’t behaving correctly. The AppPool was getting set, regardless of the parameter in the step (which was empty, to indicate no AppPool). I realized that the AppPool setting was an Octopus variable/parameter in a previous Deploy Website step. After changing the variable/parameter name in the custom step and PowerShell, it started to work. The only explanation I have is that the variables/parameters are shared among steps when the value is computed.

If this is the case, how do I use different values for multiple steps in the same project? Is there some way to scope the parameter so the value is specific to the step? Can I define a parameter with a variable in it to force scoping?

Thanks,
Erick

Hi Erick,

Thanks for getting in touch!

Are you able to send me some screenshots? Without seeing the step template you’re using or your deployment process, it’s a little hard to tell what the problem is.

Variables are shared across the whole deployment, however parameters in steps should only be relevant to the specific step they run on. That means if you’re using a variable like $MyVariable in your PowerShell, and you also have that variable defined for your project, they’ll clash. Similarly, if you reuse that name for a step parameter, there’ll be a clash there as well.

I hope that helps!

Damo

Hey Damo,

I think I’ve been running into this. We have a custom step that is used in a few different contexts, and it seems like the values are leaking between steps. The step is fairly simple:

$configToolFolder = $OctopusParameters['ConfigToolPath']
$templateFile = $OctopusParameters['TemplateFile']
$configFile = $OctopusParameters['ConfigFile']
$machineName = $OctopusParameters['MachineName']
$finalPath = $OctopusParameters['FinalPath']

if ($machineName -ne $null) {
  $machineName = "-m $($machineName)"
}

$configTool = "$($configToolFolder)\tools\BuildConfig.exe"

$command = "$($configTool) -t $($templateFile) -c $($configFile) $machineName"
write-host $command

iex $command 

if($LastExitCode -gt 0) {
    exit $LastExitCode
}
else {
    if ($finalPath) {
        Copy-Item $templateFile -Destination $finalPath -Force
    }

    exit 0
}

but when there are many projects running at the same time (targeting different servers and projects), it seems like the parameter from one step is used in a different step. This would make sense if the PowerShell is not executed in a scope. Does Octopus execute custom steps in a scope? If not, I can do it fairly easily.

Thanks,
Erick

Hi Erickt,

(Damian is away currently)

Each step is sent to Tentacle separately, and each step is run within a completely separate PowerShell.exe process, so there’s no way values could leak from one step to another. If the steps run in parallel (they’ll still have a separate PowerShell.exe instance) it could be that the commands you are running are being invoked in a different order than you expect? If you could turn on OctopusPrintVariables and then send a deployment log that shows the script printing the wrong set of variables, that might help us to understand/reproduce the issue?

http://docs.octopusdeploy.com/display/OD/Debug+problems+with+Octopus+variables

Paul