Skip to content

Commit 9a65550

Browse files
authored
Update Octopus check targets script to version 2 and enhance error messaging for API key configuration (#1654)
1 parent a7f224b commit 9a65550

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

step-templates/octopus-check-roles.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
"Name": "Octopus - Check Targets Available",
44
"Description": "Checks for the presence of targets with the specified tag. If no targets are found, the deployment will fail with the provided message.",
55
"ActionType": "Octopus.Script",
6-
"Version": 1,
6+
"Version": 2,
77
"CommunityActionTemplateId": null,
88
"Packages": [],
99
"GitDependencies": [],
1010
"Properties": {
1111
"OctopusUseBundledTooling": "False",
1212
"Octopus.Action.Script.ScriptSource": "Inline",
1313
"Octopus.Action.Script.Syntax": "PowerShell",
14-
"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}"
14+
"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 Write-Highlight \"See the [Octopus documentation](https://octopus.com/docs/octopus-rest-api/how-to-create-an-api-key) for details on creating API keys.\"\n Write-Highlight \"Once you have an API key, add it to the $($OctopusParameters['Octopus.Step.Name']) step to enable the ability to check for targets in this space.\"\n }\n\n Set-OctopusVariable -Name SetupValid -Value $setupValid\n} catch {\n Write-Verbose \"Fatal error occurred:\"\n Write-Verbose \"$($_.Exception.Message)\"\n}"
1515
},
1616
"Parameters": [
1717
{

0 commit comments

Comments
 (0)