From 9ec49a2d3ef0ee1c07e649179b4072771f6128ac Mon Sep 17 00:00:00 2001 From: twerthi Date: Mon, 21 Apr 2025 13:19:47 -0700 Subject: [PATCH 1/2] Fixing template --- .../automate-manual-intervention-response.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/step-templates/automate-manual-intervention-response.json b/step-templates/automate-manual-intervention-response.json index e298880fc..a3a90fc9c 100644 --- a/step-templates/automate-manual-intervention-response.json +++ b/step-templates/automate-manual-intervention-response.json @@ -3,13 +3,13 @@ "Name": "Automate Manual Intervention Response", "Description": "This template will search for deployments that have been paused due to Manual Intervention or Guided Failure and automate the response.", "ActionType": "Octopus.Script", - "Version": 2, + "Version": 3, "CommunityActionTemplateId": null, "Packages": [], "Properties": { "Octopus.Action.Script.ScriptSource": "Inline", "Octopus.Action.Script.Syntax": "PowerShell", - "Octopus.Action.Script.ScriptBody": "function Get-OctopusItems\n{\n # Define parameters\n param(\n $OctopusUri,\n $ApiKey,\n $SkipCount = 0\n )\n\n # Define working variables\n $items = @()\n $skipQueryString = \"\"\n $headers = @{\"X-Octopus-ApiKey\"=\"$ApiKey\"}\n\n # Check to see if there there is already a querystring\n if ($octopusUri.Contains(\"?\"))\n {\n $skipQueryString = \"&skip=\"\n }\n else\n {\n $skipQueryString = \"?skip=\"\n }\n\n $skipQueryString += $SkipCount\n\n # Get intial set\n $resultSet = Invoke-RestMethod -Uri \"$($OctopusUri)$skipQueryString\" -Method GET -Headers $headers\n\n # Check to see if it returned an item collection\n if ($resultSet.Items)\n {\n # Store call results\n $items += $resultSet.Items\n\n # Check to see if resultset is bigger than page amount\n if (($resultSet.Items.Count -gt 0) -and ($resultSet.Items.Count -eq $resultSet.ItemsPerPage))\n {\n # Increment skip count\n $SkipCount += $resultSet.ItemsPerPage\n\n # Recurse\n $items += Get-OctopusItems -OctopusUri $OctopusUri -ApiKey $ApiKey -SkipCount $SkipCount\n }\n }\n else\n {\n return $resultSet\n }\n\n\n # Return results\n return ,$items\n}\n\n$automaticResponseOctopusUrl = $OctopusParameters['AutomateResponse.Octopus.Url']\n$automaticResponseApiKey = $OctopusParameters['AutomateResponse.Api.Key']\n$automaticResponseReasonNotes = $OctopusParameters['AutomateResponse.Reason.Notes']\n$automaticResponseManualInterventionResponseType = $OctopusParameters['AutomateResponse.ManualIntervention']\n$automaticResponseGuidedFailureResponseType = $OctopusParameters['AutomateResponse.GuidedFailure']\n$header = @{ \"X-Octopus-ApiKey\" = $automaticResponseApiKey }\n\n# Validate response type input\nif (![string]::IsNullOrWhitespace($automaticResponseManualInterventionResponseType) -and ![string]::IsNullOrWhitespace($automaticResponseGuidedFailureResponseType))\n{\n\t# Fail step\n Write-Error \"Cannot have both a Manual Intervention and Guided Failure selections.\"\n}\n\nif ([string]::IsNullOrWhitespace($automaticResponseManualInterventionResponseType) -and [string]::IsNullOrWhitespace($automaticResponseGuidedFailureResponseType))\n{\n\t# Fail step\n Write-Error \"Please select either a Manual Intervention or Guidded Failure response type.\"\n}\n\n# Get space\n$spaceId = $OctopusParameters['Octopus.Space.Id']\n\n# Get project\n$projectId = $OctopusParameters['Octopus.Project.Id']\n\n# Get the environemtn\n$environmentId = $OctopusParameters['Octopus.Environment.Id']\n\n# Get currently executing deployments for project\nWrite-Host \"Searching for executing deployments ...\"\n$executingDeployments = Get-OctopusItems -OctopusUri \"$automaticResponseOctopusUrl/api/$($spaceId)/deployments?projects=$($projectId)&taskState=Executing&environments=$($environmentId)\" -ApiKey $automaticResponseApiKey\n\n# Check to see if anything was returned for the environment\nif ($executingDeployments -is [array])\n{\n # Loop through executing deployments\n foreach ($deployment in $executingDeployments)\n {\n # Get object for manual intervention\n Write-Host \"Checking $($deployment.Id) for manual interventions ...\"\n $manualIntervention = Get-OctopusItems -OctopusUri \"$automaticResponseOctopusUrl/api/$($spaceId)/interruptions?regarding=$($deployment.Id)&pendingOnly=true\" -ApiKey $automaticResponseApiKey\n\n # Check to see if a manual intervention was returned\n if ($null -ne $manualIntervention.Id)\n {\n # Take responsibility\n Write-Host \"Auto taking resonsibility for manual intervention ...\"\n Invoke-RestMethod -Method Put -Uri \"$automaticResponseOctopusUrl/api/$($spaceId)/interruptions/$($manualIntervention.Id)/responsible\" -Headers $header\n\n # Create response object\n $jsonBody = @{\n Notes = $automaticResponseReasonNotes\n }\n\n # Check to see if manual intervention is empty\n if (![string]::IsNullOrWhiteSpace($automaticResponseManualInterventionResponseType))\n {\n # Add the manual intervention type\n Write-Host \"Submitting $automaticResponseManualInterventionResponseType as response ...\"\n $jsonBody.Add(\"Result\", $automaticResponseManualInterventionResponseType)\n }\n\n # Check to see if the guided failure is empty\n if (![string]::IsNullOrWhiteSpace($automaticResponseGuidedFailureResponseType))\n {\n # Add the guided failure response\n Write-Host \"Submitting $automaticResponseGuidedFailureResponseType as response ...\"\n $jsonBody.Add(\"Guidance\", $automaticResponseGuidedFailureResponseType)\n }\n\n # Post to server\n Invoke-RestMethod -Method Post -Uri \"$automaticResponseOctopusUrl/api/$($spaceId)/interruptions/$($manualIntervention.Id)/submit\" -Body ($jsonBody | ConvertTo-Json -Depth 10) -Headers $header\n }\n }\n}" + "Octopus.Action.Script.ScriptBody": "function Get-OctopusItems\n{\n # Define parameters\n param(\n $OctopusUri,\n $ApiKey,\n $SkipCount = 0\n )\n\n # Define working variables\n $items = @()\n $skipQueryString = \"\"\n $headers = @{\"X-Octopus-ApiKey\"=\"$ApiKey\"}\n\n # Check to see if there there is already a querystring\n if ($octopusUri.Contains(\"?\"))\n {\n $skipQueryString = \"&skip=\"\n }\n else\n {\n $skipQueryString = \"?skip=\"\n }\n\n $skipQueryString += $SkipCount\n\n # Get intial set\n $resultSet = Invoke-RestMethod -Uri \"$($OctopusUri)$skipQueryString\" -Method GET -Headers $headers\n\n # Check to see if it returned an item collection\n if ($null -ne $resultSet.Items)\n {\n # Store call results\n $items += $resultSet.Items\n\n # Check to see if resultset is bigger than page amount\n if (($resultSet.Items.Count -gt 0) -and ($resultSet.Items.Count -eq $resultSet.ItemsPerPage))\n {\n # Increment skip count\n $SkipCount += $resultSet.ItemsPerPage\n\n # Recurse\n $items += Get-OctopusItems -OctopusUri $OctopusUri -ApiKey $ApiKey -SkipCount $SkipCount\n }\n }\n else\n {\n return $resultSet\n }\n\n\n # Return results\n return ,$items\n}\n\n$automaticResponseOctopusUrl = $OctopusParameters['AutomateResponse.Octopus.Url']\n$automaticResponseApiKey = $OctopusParameters['AutomateResponse.Api.Key']\n$automaticResponseReasonNotes = $OctopusParameters['AutomateResponse.Reason.Notes']\n$automaticResponseManualInterventionResponseType = $OctopusParameters['AutomateResponse.ManualIntervention']\n$automaticResponseGuidedFailureResponseType = $OctopusParameters['AutomateResponse.GuidedFailure']\n$header = @{ \"X-Octopus-ApiKey\" = $automaticResponseApiKey }\n\n# Validate response type input\nif (![string]::IsNullOrWhitespace($automaticResponseManualInterventionResponseType) -and ![string]::IsNullOrWhitespace($automaticResponseGuidedFailureResponseType))\n{\n\t# Fail step\n Write-Error \"Cannot have both a Manual Intervention and Guided Failure selections.\"\n}\n\nif ([string]::IsNullOrWhitespace($automaticResponseManualInterventionResponseType) -and [string]::IsNullOrWhitespace($automaticResponseGuidedFailureResponseType))\n{\n\t# Fail step\n Write-Error \"Please select either a Manual Intervention or Guided Failure response type.\"\n}\n\n# Get space\n$spaceId = $OctopusParameters['Octopus.Space.Id']\n\n# Get project\n$projectId = $OctopusParameters['Octopus.Project.Id']\n\n# Get the environment\n$environmentId = $OctopusParameters['Octopus.Environment.Id']\n\n# Get currently executing deployments for project\nWrite-Host \"Searching for executing deployments ...\"\n$executingDeployments = Get-OctopusItems -OctopusUri \"$automaticResponseOctopusUrl/api/$($spaceId)/deployments?projects=$($projectId)&taskState=Queued&environments=$($environmentId)\" -ApiKey $automaticResponseApiKey\n\n# Check to see if anything was returned for the environment\nif ($executingDeployments -is [array])\n{\n # Loop through executing deployments\n foreach ($deployment in $executingDeployments)\n {\n # Get object for manual intervention\n Write-Host \"Checking $($deployment.Id) for manual interventions ...\"\n $manualIntervention = Get-OctopusItems -OctopusUri \"$automaticResponseOctopusUrl/api/$($spaceId)/interruptions?regarding=$($deployment.Id)&pendingOnly=true\" -ApiKey $automaticResponseApiKey\n\n # Check to see if a manual intervention was returned\n if ($null -ne $manualIntervention.Id)\n {\n # Take responsibility\n Write-Host \"Auto taking resonsibility for manual intervention ...\"\n Invoke-RestMethod -Method Put -Uri \"$automaticResponseOctopusUrl/api/$($spaceId)/interruptions/$($manualIntervention.Id)/responsible\" -Headers $header\n\n # Create response object\n $jsonBody = @{\n Notes = $automaticResponseReasonNotes\n }\n\n # Check to see if manual intervention is empty\n if (![string]::IsNullOrWhiteSpace($automaticResponseManualInterventionResponseType))\n {\n # Add the manual intervention type\n Write-Host \"Submitting $automaticResponseManualInterventionResponseType as response ...\"\n $jsonBody.Add(\"Result\", $automaticResponseManualInterventionResponseType)\n }\n\n # Check to see if the guided failure is empty\n if (![string]::IsNullOrWhiteSpace($automaticResponseGuidedFailureResponseType))\n {\n # Add the guided failure response\n Write-Host \"Submitting $automaticResponseGuidedFailureResponseType as response ...\"\n $jsonBody.Add(\"Guidance\", $automaticResponseGuidedFailureResponseType)\n }\n\n # Post to server\n Invoke-RestMethod -Method Post -Uri \"$automaticResponseOctopusUrl/api/$($spaceId)/interruptions/$($manualIntervention.Id)/submit\" -Body ($jsonBody | ConvertTo-Json -Depth 10) -Headers $header\n }\n }\n}" }, "Parameters": [ { @@ -66,10 +66,10 @@ } ], "$Meta": { - "ExportedAt": "2021-10-01T17:52:01.610Z", - "OctopusVersion": "2021.2.7580", + "ExportedAt": "2025-04-21T20:17:19.315Z", + "OctopusVersion": "2025.2.7176", "Type": "ActionTemplate" }, "LastModifiedBy": "twerthi", "Category": "octopus" - } \ No newline at end of file + } From 96940cb93109bcbd961a982706046a243367a1df Mon Sep 17 00:00:00 2001 From: twerthi Date: Mon, 21 Apr 2025 15:43:51 -0700 Subject: [PATCH 2/2] Fixed API calls to get correct deployments depending on what the user chose --- step-templates/automate-manual-intervention-response.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/step-templates/automate-manual-intervention-response.json b/step-templates/automate-manual-intervention-response.json index a3a90fc9c..4f62dfb24 100644 --- a/step-templates/automate-manual-intervention-response.json +++ b/step-templates/automate-manual-intervention-response.json @@ -9,7 +9,7 @@ "Properties": { "Octopus.Action.Script.ScriptSource": "Inline", "Octopus.Action.Script.Syntax": "PowerShell", - "Octopus.Action.Script.ScriptBody": "function Get-OctopusItems\n{\n # Define parameters\n param(\n $OctopusUri,\n $ApiKey,\n $SkipCount = 0\n )\n\n # Define working variables\n $items = @()\n $skipQueryString = \"\"\n $headers = @{\"X-Octopus-ApiKey\"=\"$ApiKey\"}\n\n # Check to see if there there is already a querystring\n if ($octopusUri.Contains(\"?\"))\n {\n $skipQueryString = \"&skip=\"\n }\n else\n {\n $skipQueryString = \"?skip=\"\n }\n\n $skipQueryString += $SkipCount\n\n # Get intial set\n $resultSet = Invoke-RestMethod -Uri \"$($OctopusUri)$skipQueryString\" -Method GET -Headers $headers\n\n # Check to see if it returned an item collection\n if ($null -ne $resultSet.Items)\n {\n # Store call results\n $items += $resultSet.Items\n\n # Check to see if resultset is bigger than page amount\n if (($resultSet.Items.Count -gt 0) -and ($resultSet.Items.Count -eq $resultSet.ItemsPerPage))\n {\n # Increment skip count\n $SkipCount += $resultSet.ItemsPerPage\n\n # Recurse\n $items += Get-OctopusItems -OctopusUri $OctopusUri -ApiKey $ApiKey -SkipCount $SkipCount\n }\n }\n else\n {\n return $resultSet\n }\n\n\n # Return results\n return ,$items\n}\n\n$automaticResponseOctopusUrl = $OctopusParameters['AutomateResponse.Octopus.Url']\n$automaticResponseApiKey = $OctopusParameters['AutomateResponse.Api.Key']\n$automaticResponseReasonNotes = $OctopusParameters['AutomateResponse.Reason.Notes']\n$automaticResponseManualInterventionResponseType = $OctopusParameters['AutomateResponse.ManualIntervention']\n$automaticResponseGuidedFailureResponseType = $OctopusParameters['AutomateResponse.GuidedFailure']\n$header = @{ \"X-Octopus-ApiKey\" = $automaticResponseApiKey }\n\n# Validate response type input\nif (![string]::IsNullOrWhitespace($automaticResponseManualInterventionResponseType) -and ![string]::IsNullOrWhitespace($automaticResponseGuidedFailureResponseType))\n{\n\t# Fail step\n Write-Error \"Cannot have both a Manual Intervention and Guided Failure selections.\"\n}\n\nif ([string]::IsNullOrWhitespace($automaticResponseManualInterventionResponseType) -and [string]::IsNullOrWhitespace($automaticResponseGuidedFailureResponseType))\n{\n\t# Fail step\n Write-Error \"Please select either a Manual Intervention or Guided Failure response type.\"\n}\n\n# Get space\n$spaceId = $OctopusParameters['Octopus.Space.Id']\n\n# Get project\n$projectId = $OctopusParameters['Octopus.Project.Id']\n\n# Get the environment\n$environmentId = $OctopusParameters['Octopus.Environment.Id']\n\n# Get currently executing deployments for project\nWrite-Host \"Searching for executing deployments ...\"\n$executingDeployments = Get-OctopusItems -OctopusUri \"$automaticResponseOctopusUrl/api/$($spaceId)/deployments?projects=$($projectId)&taskState=Queued&environments=$($environmentId)\" -ApiKey $automaticResponseApiKey\n\n# Check to see if anything was returned for the environment\nif ($executingDeployments -is [array])\n{\n # Loop through executing deployments\n foreach ($deployment in $executingDeployments)\n {\n # Get object for manual intervention\n Write-Host \"Checking $($deployment.Id) for manual interventions ...\"\n $manualIntervention = Get-OctopusItems -OctopusUri \"$automaticResponseOctopusUrl/api/$($spaceId)/interruptions?regarding=$($deployment.Id)&pendingOnly=true\" -ApiKey $automaticResponseApiKey\n\n # Check to see if a manual intervention was returned\n if ($null -ne $manualIntervention.Id)\n {\n # Take responsibility\n Write-Host \"Auto taking resonsibility for manual intervention ...\"\n Invoke-RestMethod -Method Put -Uri \"$automaticResponseOctopusUrl/api/$($spaceId)/interruptions/$($manualIntervention.Id)/responsible\" -Headers $header\n\n # Create response object\n $jsonBody = @{\n Notes = $automaticResponseReasonNotes\n }\n\n # Check to see if manual intervention is empty\n if (![string]::IsNullOrWhiteSpace($automaticResponseManualInterventionResponseType))\n {\n # Add the manual intervention type\n Write-Host \"Submitting $automaticResponseManualInterventionResponseType as response ...\"\n $jsonBody.Add(\"Result\", $automaticResponseManualInterventionResponseType)\n }\n\n # Check to see if the guided failure is empty\n if (![string]::IsNullOrWhiteSpace($automaticResponseGuidedFailureResponseType))\n {\n # Add the guided failure response\n Write-Host \"Submitting $automaticResponseGuidedFailureResponseType as response ...\"\n $jsonBody.Add(\"Guidance\", $automaticResponseGuidedFailureResponseType)\n }\n\n # Post to server\n Invoke-RestMethod -Method Post -Uri \"$automaticResponseOctopusUrl/api/$($spaceId)/interruptions/$($manualIntervention.Id)/submit\" -Body ($jsonBody | ConvertTo-Json -Depth 10) -Headers $header\n }\n }\n}" + "Octopus.Action.Script.ScriptBody": "function Get-OctopusItems\n{\n # Define parameters\n param(\n $OctopusUri,\n $ApiKey,\n $SkipCount = 0\n )\n\n # Define working variables\n $items = @()\n $skipQueryString = \"\"\n $headers = @{\"X-Octopus-ApiKey\"=\"$ApiKey\"}\n\n # Check to see if there there is already a querystring\n if ($octopusUri.Contains(\"?\"))\n {\n $skipQueryString = \"&skip=\"\n }\n else\n {\n $skipQueryString = \"?skip=\"\n }\n\n $skipQueryString += $SkipCount\n\n # Get intial set\n $resultSet = Invoke-RestMethod -Uri \"$($OctopusUri)$skipQueryString\" -Method GET -Headers $headers\n\n # Check to see if it returned an item collection\n if ($null -ne $resultSet.Items)\n {\n # Store call results\n $items += $resultSet.Items\n\n # Check to see if resultset is bigger than page amount\n if (($resultSet.Items.Count -gt 0) -and ($resultSet.Items.Count -eq $resultSet.ItemsPerPage))\n {\n # Increment skip count\n $SkipCount += $resultSet.ItemsPerPage\n\n # Recurse\n $items += Get-OctopusItems -OctopusUri $OctopusUri -ApiKey $ApiKey -SkipCount $SkipCount\n }\n }\n else\n {\n return $resultSet\n }\n\n\n # Return results\n return ,$items\n}\n\n$automaticResponseOctopusUrl = $OctopusParameters['AutomateResponse.Octopus.Url']\n$automaticResponseApiKey = $OctopusParameters['AutomateResponse.Api.Key']\n$automaticResponseReasonNotes = $OctopusParameters['AutomateResponse.Reason.Notes']\n$automaticResponseManualInterventionResponseType = $OctopusParameters['AutomateResponse.ManualIntervention']\n$automaticResponseGuidedFailureResponseType = $OctopusParameters['AutomateResponse.GuidedFailure']\n$header = @{ \"X-Octopus-ApiKey\" = $automaticResponseApiKey }\n\n# Validate response type input\nif (![string]::IsNullOrWhitespace($automaticResponseManualInterventionResponseType) -and ![string]::IsNullOrWhitespace($automaticResponseGuidedFailureResponseType))\n{\n\t# Fail step\n Write-Error \"Cannot have both a Manual Intervention and Guided Failure selections.\"\n}\n\nif ([string]::IsNullOrWhitespace($automaticResponseManualInterventionResponseType) -and [string]::IsNullOrWhitespace($automaticResponseGuidedFailureResponseType))\n{\n\t# Fail step\n Write-Error \"Please select either a Manual Intervention or Guided Failure response type.\"\n}\n\n# Get space\n$spaceId = $OctopusParameters['Octopus.Space.Id']\n\n# Get project\n$projectId = $OctopusParameters['Octopus.Project.Id']\n\n# Get the environment\n$environmentId = $OctopusParameters['Octopus.Environment.Id']\n\nif (![string]::IsNullOrWhitespace($automaticResponseGuidedFailureResponseType))\n{\n# Get currently executing deployments for project - this is for Guided Failure as they're in an executing state\n Write-Host \"Searching for executing deployments ...\"\n $executingDeployments = Get-OctopusItems -OctopusUri \"$automaticResponseOctopusUrl/api/$($spaceId)/deployments?projects=$($projectId)&taskState=Executing&environments=$($environmentId)\" -ApiKey $automaticResponseApiKey\n}\n\nif (![string]::IsNullOrWhitespace($automaticResponseManualInterventionResponseType))\n{\n Write-Host \"Searching for queued deployments ...\"\n # Get queued deployments - this is for \n $executingDeployments = Get-OctopusItems -OctopusUri \"$automaticResponseOctopusUrl/api/$($spaceId)/deployments?projects=$($projectId)&taskState=Queued&environments=$($environmentId)\" -ApiKey $automaticResponseApiKey\n}\n\n# Check to see if anything was returned for the environment\nif ($executingDeployments -is [array])\n{\n # Loop through executing deployments\n foreach ($deployment in $executingDeployments)\n {\n # Get object for manual intervention\n Write-Host \"Checking $($deployment.Id) for manual interventions ...\"\n $manualIntervention = Get-OctopusItems -OctopusUri \"$automaticResponseOctopusUrl/api/$($spaceId)/interruptions?regarding=$($deployment.Id)&pendingOnly=true\" -ApiKey $automaticResponseApiKey\n\n # Check to see if a manual intervention was returned\n if ($null -ne $manualIntervention.Id)\n {\n # Take responsibility\n Write-Host \"Auto taking resonsibility for manual intervention ...\"\n Invoke-RestMethod -Method Put -Uri \"$automaticResponseOctopusUrl/api/$($spaceId)/interruptions/$($manualIntervention.Id)/responsible\" -Headers $header\n\n # Create response object\n $jsonBody = @{\n Notes = $automaticResponseReasonNotes\n }\n\n # Check to see if manual intervention is empty\n if (![string]::IsNullOrWhiteSpace($automaticResponseManualInterventionResponseType))\n {\n # Add the manual intervention type\n Write-Host \"Submitting $automaticResponseManualInterventionResponseType as response ...\"\n $jsonBody.Add(\"Result\", $automaticResponseManualInterventionResponseType)\n }\n\n # Check to see if the guided failure is empty\n if (![string]::IsNullOrWhiteSpace($automaticResponseGuidedFailureResponseType))\n {\n # Add the guided failure response\n Write-Host \"Submitting $automaticResponseGuidedFailureResponseType as response ...\"\n $jsonBody.Add(\"Guidance\", $automaticResponseGuidedFailureResponseType)\n }\n\n # Post to server\n Invoke-RestMethod -Method Post -Uri \"$automaticResponseOctopusUrl/api/$($spaceId)/interruptions/$($manualIntervention.Id)/submit\" -Body ($jsonBody | ConvertTo-Json -Depth 10) -Headers $header\n }\n }\n}" }, "Parameters": [ {