Retrieving Octopus Environment Variable within health check custom script

Hello

I have a custom Machine policy that runs a custom powershell script to perform health check for tentacle endpoints.

The script does a few things, but the thing it fails on is retrieving the machines environment. I want to know this as the alert i want to get would be different if the machine was in the Test environment compared to the production environment.

Here is a snipit of the code:

$envName  = $OctopusParameters['Octopus.Environment.Name']
$serverName  = $OctopusParameters['Octopus.Machine.Name']
$envID  = $OctopusParameters['Octopus.Environment.Id']

write-host "Env Name: $envName"
write-host "Env ID: $envID"
write-host "Server Name: $serverName"

The server name will output fine, but the Env ID or Env name will not. Is this a problem because i’m calling these from within a healthcheck and not from within a deployment step?

My octopus server version is 3.12.4

Thanks

Hi,

Thanks for reaching out!

Is this a problem because i’m calling these from within a healthcheck and not from within a deployment step?

That’s exactly why.

You could use this API script to get the names of the Environments of a machine during a health check: https://github.com/OctopusDeploy/OctopusDeploy-Api/blob/master/REST/PowerShell/Targets/GetMachineEnvironments.ps1

Hope that helps!
Dalmiro

Hi

Thanks that will work for now. My only issue is that i have to expose the API key within the script.

It would be great as a future release if you could grant Octopus Machines roles such as AWS does, eg AWS Service Role for an EC2 Instance, so that the machine has an API key embedded.

Anyway Ive got it working, just prefer to not expose the key.

Thanks

Hi Mark,

I recommend you to create a service account that only has access to see machines and environments (no create/delete projects,releases,deployments,etc rights), and then create an API for that account and use it on your script.

That way, even though that API key will be exposed on the script, it wouldn’t be useful for anyone really.

About that feature proposal, it doesn’t sound bad. Would you mind submitting it in Uservoice?

https://octopusdeploy.uservoice.com/

Thanks,
Dalmiro