Retrieve Name of Machine that has been deployed

Hi, I have a “Deploy a Package” step (an Octopus step template) and would like to retrieve the name of the machines that this step have deployed to and email the result to users. Any idea how we can do this?
Regards
B.

Hi,
You should be able to create a “Send an email” step and include the following in the message body:

Package was deployed to:
#{each machine in Octopus.Action[Deploy a Package].Output}
  #{machine} 
#{/each}

Regards,
Mark

I’ve actually just asked the same question in a new post (albeit in a less succinct way :slight_smile: ).

I’ve just set up a small test project/environment/lifecycle to try this out and I can’t get it to work. For the Deploy a Package step I’ve just used a “Run a script” step so that I don’t have to actually create a package to test this, so I’m just wondering if this is why its not working for me. Does the step that Octopus.Action[].Output references have to be a “Deploy a package” step for this to work?

Also, will this work for sending an email prior to the step that Octopus.Action[].Output references? In other words does the step have to have been executed before the variable will be populated?

Thanks,
Dave

Hi Dave,

I’m using a bit of a hack in the way we handle output variables to do this, which is why it isn’t working for you. Some of our steps (such as Deploy a Package), write out an Output Variable (https://octopus.com/docs/deploying-applications/variables/output-variables). In later steps you can access output variables from the same machine with:

Octopus.Action[_step_name_].Output.VariableName

But more crucially, you can access Output variables from other machines with:

Octopus.Action[step_name].Output[machine_name].VariableName

That’s what lets me use the #{each} syntax, it iterates through indexed variables.

So in your case, Run a Script doesn’t produce any Output variables by default, so there is nothing in the variable list to index. However, if you added

Set-OctopusVariable -name "AnyVariable" -value "AnyValue"

To the script in your script step, it should start working, as that produces an output variable.

As to your other question, you have probably guessed by now, but no, this won’t work before the steps have run, as the output variables don’t exist yet. I think the best you can do there is check the variable Octopus.Environment.MachinesInRole[your-role-name] for the role (or roles) your step targets. But that will only include machine Ids, not their friendly name. So you could check that in a script step, then call the API to convert from Id to Name, then write the result to an Output variable that your email step reads. Quite a very hoops to jump through I’m afraid.

Your script is going to look something like:

$machineIds = $OctopusParameters['Octopus.Environment.MachinesInRole[Your-Target-Role]']

$machines = ($machineIds -Split "," | ForEach-Object {
    $header = @{ "X-Octopus-ApiKey" = "API-YOURAPIKEY" }
    $machine = (Invoke-WebRequest http://your.octopus.server/api/machines/$_ -Headers $header).content | ConvertFrom-Json
    $machine.Name
}) -join ', '

Set-OctopusVariable -name "Machines" -value $machines

but I’m not sure how the MachinesInRole variable handles things like users excluding machines from the deployment and that sort of thing, so probably safer to stick to post deploy if you can.

Regards,
Mark

I’m trying to run the following script:
#{each machine in Octopus.Action[Deploy Schemes.xml].Output}
write-verbose “#{machine}”
$server = “#{machine}”
$SchemeDeployed = $OctopusParameter[“Octopus.Action[Deploy Schemes.xml].Output[${server}].SchemeDeployed”]
#{/each}

I’m getting the following error:
Cannot index into a null array.
At D:\Octopus\Work\20171019081354-31952-1934\Script.ps1:4 char:5

  • $SchemeDeployed = $OctopusParameter["Octopus.Action[Deploy Scheme ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (:slight_smile: [], ParentContainsErrorRecordException
  • FullyQualifiedErrorId : NullArray

Any idea what’s wrong with this code.

Hi,

I think it’s just that it should be $OctopusParameters, note the s on the end.

Regards,
Mark

Doh. That fixed it.
Thanks