Octopus.Script exported 2025-10-13 by mcasperson belongs to ‘Octopus’ category.
Checks for the presence of targets with the specified tag. If no targets are found, the deployment will fail with the provided message.
Parameters
When steps based on the template are included in a project’s deployment process, the parameters below can be set.
Octopus API Key
CheckTargets.Octopus.Api.Key =
The API key used to query the Octopus instance.
Target Tag to Check
CheckTargets.Octopus.Role =
The name of the target tag to check for.
Message to display if tags not found
CheckTargets.Message =
An optional custom message to display if no targets are found with the specified tag.
Script body
Steps based on this template will execute the following PowerShell script.
$errorCollection = @()
$setupValid = $false
Write-Host "Checking for deployment targets ..."
try
{
# Check to make sure targets have been created
if ([string]::IsNullOrWhitespace("#{Octopus.Web.ServerUri}"))
{
$octopusUrl = "#{Octopus.Web.BaseUrl}"
}
else
{
$octopusUrl = "#{Octopus.Web.ServerUri}"
}
$apiKey = "#{CheckTargets.Octopus.Api.Key}"
$role = "#{CheckTargets.Octopus.Role}"
$message = "#{CheckTargets.Message}"
if (![string]::IsNullOrWhitespace($apiKey) -and $apiKey.StartsWith("API-"))
{
$spaceId = "#{Octopus.Space.Id}"
$headers = @{ "X-Octopus-ApiKey" = "$apiKey" }
try
{
$roleTargets = Invoke-RestMethod -Method Get -Uri "$octopusUrl/api/$spaceId/machines?roles=$role" -Headers $headers
if ($roleTargets.Items.Count -lt 1)
{
$errorCollection += @("Expected at least 1 target for tag $role, but found $( $roleTargets.Items.Count ). $message")
}
}
catch
{
$errorCollection += @("Failed to retrieve role targets: $( $_.Exception.Message )")
}
if ($errorCollection.Count -gt 0)
{
foreach ($item in $errorCollection)
{
Write-Highlight "$item"
}
}
else
{
$setupValid = $true
Write-Host "Setup valid!"
}
}
else
{
Write-Highlight "The project variable CheckTargets.Octopus.Api.Key has not been configured, unable to check deployment targets."
}
Set-OctopusVariable -Name SetupValid -Value $setupValid
} catch {
Write-Verbose "Fatal error occurred:"
Write-Verbose "$($_.Exception.Message)"
}
Provided under the Apache License version 2.0.
To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.
{
"Id": "81444e7f-d77a-47db-b287-0f1ab5793880",
"Name": "Octopus - Check Targets Available",
"Description": "Checks for the presence of targets with the specified tag. If no targets are found, the deployment will fail with the provided message.",
"Version": 1,
"ExportedAt": "2025-10-13T00:52:34.019Z",
"ActionType": "Octopus.Script",
"Author": "mcasperson",
"Packages": [],
"Parameters": [
{
"Id": "e9713772-81f8-4f0f-af80-10c3ec0bfb17",
"Name": "CheckTargets.Octopus.Api.Key",
"Label": "Octopus API Key",
"HelpText": "The API key used to query the Octopus instance.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "Sensitive"
}
},
{
"Id": "f2c126ad-e907-4fb8-84a3-a5fb80dd6688",
"Name": "CheckTargets.Octopus.Role",
"Label": "Target Tag to Check",
"HelpText": "The name of the target tag to check for.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "2d5257e4-288a-456c-ba18-bd2b3c1c1351",
"Name": "CheckTargets.Message",
"Label": "Message to display if tags not found",
"HelpText": "An optional custom message to display if no targets are found with the specified tag.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
}
],
"Properties": {
"OctopusUseBundledTooling": "False",
"Octopus.Action.Script.ScriptSource": "Inline",
"Octopus.Action.Script.Syntax": "PowerShell",
"Octopus.Action.Script.ScriptBody": "$errorCollection = @()\n$setupValid = $false\n\nWrite-Host \"Checking for deployment targets ...\"\n\ntry\n{\n # Check to make sure targets have been created\n if ([string]::IsNullOrWhitespace(\"#{Octopus.Web.ServerUri}\"))\n {\n $octopusUrl = \"#{Octopus.Web.BaseUrl}\"\n }\n else\n {\n $octopusUrl = \"#{Octopus.Web.ServerUri}\"\n }\n\n $apiKey = \"#{CheckTargets.Octopus.Api.Key}\"\n $role = \"#{CheckTargets.Octopus.Role}\"\n $message = \"#{CheckTargets.Message}\"\n\n if (![string]::IsNullOrWhitespace($apiKey) -and $apiKey.StartsWith(\"API-\"))\n {\n $spaceId = \"#{Octopus.Space.Id}\"\n $headers = @{ \"X-Octopus-ApiKey\" = \"$apiKey\" }\n\n try\n {\n $roleTargets = Invoke-RestMethod -Method Get -Uri \"$octopusUrl/api/$spaceId/machines?roles=$role\" -Headers $headers\n if ($roleTargets.Items.Count -lt 1)\n {\n $errorCollection += @(\"Expected at least 1 target for tag $role, but found $( $roleTargets.Items.Count ). $message\")\n }\n }\n catch\n {\n $errorCollection += @(\"Failed to retrieve role targets: $( $_.Exception.Message )\")\n }\n\n if ($errorCollection.Count -gt 0)\n {\n foreach ($item in $errorCollection)\n {\n Write-Highlight \"$item\"\n }\n }\n else\n {\n $setupValid = $true\n Write-Host \"Setup valid!\"\n }\n }\n else\n {\n Write-Highlight \"The project variable CheckTargets.Octopus.Api.Key has not been configured, unable to check deployment targets.\"\n }\n\n Set-OctopusVariable -Name SetupValid -Value $setupValid\n} catch {\n Write-Verbose \"Fatal error occurred:\"\n Write-Verbose \"$($_.Exception.Message)\"\n}"
},
"Category": "Octopus",
"HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/a381802920158308/step-templates/octopus-check-roles.json",
"Website": "/step-templates/81444e7f-d77a-47db-b287-0f1ab5793880",
"Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAC1QTFRFT6Tl////L5Pg8vj9Y67omsvwPJrisdfzfbzs5fL7y+T32Ov5isLucLXqvt31CJPHWwAABMJJREFUeNrs3deW4jAMAFDF3U75/89dlp0ZhiU4blJEjvQ8hYubLJsA00UCBCIQgQhEIAIRiEAEIhCBCEQgAhGIQAQiEIEIhD8kJm+t+QprfdKfB9HbYpx6CWfspj8HMi+gMgHL/AmQA8W3JTKH+ALFvzCeL0RbpyoCPE9IJeNOSQwh5Z3qd6yRGWQ2qi2cZQWxqj1WzQYSjeoJmJlAklOd4VlArOqPhQEkqBERToeMcfRJBk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