Iterate over output variables per target

Regarding: https://octopus.com/docs/deployment-process/variables/output-variables#Outputvariables-Outputfrommultiplemachines

I have 10 deployment targets and for each I have a scoped variable (Project > Variables > Scoped for a target). It is something like a list of connecting strings.

Now, in my last Email step, I want to figure out which “connection string files” are updated. What is the best/easiest way to do this?

I was thinking to set an output variable per deployment target and gather it in the email step. How would I do this or are there better ways?

I also thought about getting it directly from the Variables but I do not know which deployment targets are deploying right?

My pseudo code thinking:

var result = ""
foreach (key in Octopus.Action[StepA].Output[] ) # so all targets that have put something here
{
     var data = key in Octopus.Action[StepA].Output[key] 
     result += data
}

Hi @GerjanOnline,

Thanks for getting in touch! I’ve done a bit of testing here to try to get a good solution, and firstly I’ll admit I’m not super familiar with C#, but I was able to get what I think you’re after with the following PowerShell.

  1. Step1 sets the output variable via Set-OctopusVariable -name "OutputVar" -value "#{Variable}", where #{Variable} is defined in the project, with unique values each scoped to a single machine.

  2. Step2 then grabs all of the relevant output variables set only by the machines the first step was run on.

#{each machine in Octopus.Action[Step1Name].Output}
write-verbose "#{machine}"
$server = "#{machine}"
$machineVar = $OctopusParameters["Octopus.Action[Set Variable].Output[$server].OutputVar"]
write-host "$machineVar"
#{/each}

I hope this helps (and isn’t too much of a pain to translate to C#!). Let me know how you go, and don’t hesitate to reach out if you have any further questions or concerns moving forward. :slight_smile:

Best regards,

Kenny

What is this kind of syntax? Is this for the email template?

Because #{each machine in Octopus.Action[Step1Name].Output} is not valid PowerShell right?

Btw: PowerShell is fine, I also use PS. It was just pseudo code and it happens that it looks more like C# :wink:

Hi @GerjanOnline,

Thanks for following up! You’re correct, this isn’t valid PowerShell, but we have an open-source component called Octostache that provides this variable substitution functionality, and this extended syntax. You can refer to the source code for this if you’re interested. :slight_smile:

And you can refer to the additional extended syntax options in the following doc page.

I hope this helps! Let me know if you have any questions or concerns moving forward. :slight_smile:

Best regards,

Kenny

So do you mean I can put your code snippet into a variable?

Im searching for how to implement step 2. In my case that is the email step but of course I can create a separate gather step (ie PS) and use it in my email template.

Im mostly confused because of the write host statements inside it, but also the variables

Hi @GerjanOnline,

Thanks for following up, and firstly I’m sorry about getting sidetracked there and neglecting the fact that this is an email step. Powershell scripts can’t be run in email steps, though you can perform variable substitution in them. So I believe the following should get what you’re after in an email step (keeping step 1 that creates the output variables the same):

#{each machine in Octopus.Action[Set Variable].Output}
Machine name: #{machine}
Variable name: #{Octopus.Action[Set Variable].Output[#{machine}].OutputVar}"
#{/each}

Let me know if this helps!

Best regards,

Kenny

Great, thanks a a lot!

But i’m still curious: can you solve this also in PowerShell only? So create an “aggregate result variable”.

Hi @GerjanOnline,

You’re quite welcome! Theoretically it would be possible to do this in PowerShell only. I haven’t done it myself, but you could store the results from some PowerShell logic into an output variable, then call that output variable in the email step.

I hope that helps! Let me know if I can try to help with anything else! :slight_smile:

Best regards,

Kenny

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.

For anyone coming across this question in the future. I answered the same question for a KB + Ask Octopus video at How can I aggregate output variable values across targets into a single value?