|
| 1 | +{ |
| 2 | + "Id": "829b2777-144c-498b-b6aa-2b387da76ead", |
| 3 | + "Name": "Octopus - Re-deploy previous version", |
| 4 | + "Description": "Re-deploy the previous version of a project.", |
| 5 | + "ActionType": "Octopus.Script", |
| 6 | + "Version": 1, |
| 7 | + "CommunityActionTemplateId": null, |
| 8 | + "Packages": [], |
| 9 | + "GitDependencies": [], |
| 10 | + "Properties": { |
| 11 | + "Octopus.Action.Script.ScriptSource": "Inline", |
| 12 | + "Octopus.Action.Script.Syntax": "PowerShell", |
| 13 | + "Octopus.Action.Script.ScriptBody": "function Get-OctopusItems\n{\n\t# Define parameters\n param(\n \t$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 Write-Host \"Calling $OctopusUri$skipQueryString\"\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 # Return results\n return $items\n}\n\n# Define variables\n$octopusApiKey = $OctopusParameters['redeploy.api.key']\n$octopusUri = $OctopusParameters['redeploy.octopus.server.uri']\n$octopusReleaseNumber = $OctopusParameters['redeploy.release.number']\n$octopusReleaseId = $null\n$header = @{ \"X-Octopus-ApiKey\" = $octopusApiKey }\n$spaceId = $OctopusParameters['Octopus.Space.Id']\n$environmentId = $OctopusParameters['Octopus.Environment.Id']\n$projectId = $OctopusParameters['Octopus.Project.Id']\n$promptedVariables = $OctopusParameters['redeploy.prompted.variables']\n$usePreviousPromptedVariables = [System.Convert]::ToBoolean($OctopusParameters['redeploy.prompted.useexisting'])\n$deploymentFormValues = @{}\n\nif ($octopusUri.EndsWith(\"/\") -eq $true)\n{\n # Add trailing slash\n $octopusUri = $octopusUri.Substring(0, ($octopusUri.Length - 1))\n}\n\n# Check to see if a release number was provided\nif ([string]::IsNullOrWhitespace($octopusReleaseNumber))\n{\n # Get the previous release number\n $octopusReleaseId = $OctopusParameters['Octopus.Release.PreviousForEnvironment.Id']\n \n $release = Get-OctopusItems -OctopusUri \"$octopusUri/api/$spaceId/releases/$octopusReleaseId\" -ApiKey $octopusApiKey\n}\nelse\n{\n # Get the specific release\n $release = Get-OctopusItems -OctopusUri \"$octopusUri/api/$spaceId/projects/$projectId/releases?searchByVersion=$octopusReleaseNumber\" -ApiKey $octopusApiKey\n \n # Record the id\n $octopusReleaseId = $release.Id\n} \n \n# Verify result\nif ($null -ne $release)\n{\n # Get deployments\n $deployments = Get-OctopusItems -OctopusUri \"$octopusUri/api/$spaceId/releases/$($release.Id)/deployments\" -ApiKey $octopusApiKey\n \n # Ensure this release has been deployed to this environment\n $deployment = ($deployments | Where-Object {$_.EnvironmentId -eq $environmentId})\n \n if ($null -eq $deployment)\n {\n Write-Error \"Error: $octopusReleaseNumber has not been deployed to $($OctopusParameters['Octopus.Environment.Name'])!\"\n }\n \n # Get the task\n if ($deployment.Links.Task -is [array])\n {\n # Get the last attempt\n $taskLink = $deployment.Links.Task[-1]\n }\n else\n {\n $taskLink = $deployment.Links.Task\n }\n \n $serverTask = Invoke-RestMethod -Method Get -Uri \"$octopusUri$($taskLink)\" -Headers $header\n \n # Ensure it was successful before continuing\n if ($serverTask.State -eq \"Failed\")\n {\n Write-Error \"The previous deployment of $($release.Version) to $($OctopusParameters['Octopus.Environment.Name']) was not successful, unable to re-deploy.\"\n }\n \n try\n {\n $deploymentVariables = Invoke-RestMethod -Method Get -Uri \"$octopusUri/api/$spaceId/variables/variableset-$($serverTask.Arguments.DeploymentId)\" -Headers $header\n }\n catch\n {\n if ($_.Exception.Response.StatusCode -eq \"NotFound\")\n {\n $deploymentVariables = $null\n }\n else\n {\n throw\n }\n }\n \n # Get only prompted variables\n $deploymentVariables = ($deploymentVariables.Variables | Where-Object {$null -ne $_.Prompt})\n}\nelse\n{\n Write-Error \"Unable to find release version $octopusReleaseNumber!\"\n}\n\n# Check to see if there prompted variables that need to be included\nif ($usePreviousPromptedVariables -or ![string]::IsNullOrWhitespace($promptedVariables))\n{\n # Ensure the previous deployment variables were retrieved\n if ($null -eq $deploymentVariables)\n {\n throw \"Error: Unable to retrieve previous deployment variables!\"\n }\n \n if ($usePreviousPromptedVariables)\n {\n # Create list\n $promptedValueList = @()\n foreach ($variable in $deploymentVariables)\n {\n $promptedValueList += \"$($variable.Name)=$($variable.Value)\"\n }\n }\n else\n {\n $promptedValueList = @(($promptedVariables -Split \"`n\").Trim())\n }\n\n # Get deployment preview for prompted variables\n $deploymentPreview = Invoke-RestMethod \"$OctopusUri/api/$spaceId/releases/$octopusReleaseId/deployments/preview/$($environmentId)?includeDisabledSteps=true\" -Headers $header \n \n foreach($element in $deploymentPreview.Form.Elements)\n {\n $nameToSearchFor = $element.Control.Name\n $uniqueName = $element.Name\n $isRequired = $element.Control.Required\n \n $promptedVariablefound = $false\n \n Write-Host \"Looking for the prompted variable value for $nameToSearchFor\"\n foreach ($promptedValue in $promptedValueList)\n {\n $splitValue = $promptedValue -Split \"=\"\n Write-Host \"Comparing $nameToSearchFor with provided prompted variable $($promptedValue[$nameToSearchFor])\"\n if ($splitValue.Length -gt 1)\n {\n if ($nameToSearchFor -eq $splitValue[0].Trim())\n {\n Write-Host \"Found the prompted variable value $nameToSearchFor\"\n $deploymentFormValues[$uniqueName] = $splitValue[1].Trim()\n $promptedVariableFound = $true\n break\n }\n }\n }\n \n if ($promptedVariableFound -eq $false -and $isRequired -eq $true)\n {\n Write-Highlight \"Unable to find a value for the required prompted variable $nameToSearchFor, exiting\"\n Exit 1\n }\n }\n \n}\n\n# Create json object to deploy the release\n$deploymentBody = @{\n ReleaseId = $octopusReleaseId\n EnvironmentId = $environmentId\n}\n\n# Check to see if there were any Prompted Variables\nif ($deploymentFormValues.Count -gt 0)\n{\n $deploymentBody.Add(\"FormValues\", $deploymentFormValues)\n}\n\n# Submit deployment\nWrite-Host \"Submitting release $($release.Version) to $($OctopusParameters['Octopus.Environment.Name'])\"\n$submittedRelease = (Invoke-RestMethod -Uri \"$octopusUri/api/$spaceId/deployments\" -Method POST -Headers $header -Body ($deploymentBody | ConvertTo-Json -Depth 10))\n\nWrite-Host \"[View the re-deployment]($octopusUri$($submittedRelease.Links.Web))\"" |
| 14 | + }, |
| 15 | + "Parameters": [ |
| 16 | + { |
| 17 | + "Id": "9f8d7b00-417c-406e-8788-326fd67d22b3", |
| 18 | + "Name": "redeploy.octopus.server.uri", |
| 19 | + "Label": "Octopus URI", |
| 20 | + "HelpText": "Provide the URI to your Octopus server.", |
| 21 | + "DefaultValue": "#{Octopus.Web.ServerUri}", |
| 22 | + "DisplaySettings": { |
| 23 | + "Octopus.ControlType": "SingleLineText" |
| 24 | + } |
| 25 | + }, |
| 26 | + { |
| 27 | + "Id": "7b2ed439-b215-42fe-8971-3839626988fd", |
| 28 | + "Name": "redeploy.api.key", |
| 29 | + "Label": "API Key", |
| 30 | + "HelpText": "Provide an API key with permission to re-deploy the previous version.", |
| 31 | + "DefaultValue": "", |
| 32 | + "DisplaySettings": { |
| 33 | + "Octopus.ControlType": "Sensitive" |
| 34 | + } |
| 35 | + }, |
| 36 | + { |
| 37 | + "Id": "92461617-cab6-4dc7-8fdd-0fcfc08c5d66", |
| 38 | + "Name": "redeploy.release.number", |
| 39 | + "Label": "Release number", |
| 40 | + "HelpText": "(Optional)\nProvide a release number to re-deploy. Leave blank to use the immediate previous release number.", |
| 41 | + "DefaultValue": "", |
| 42 | + "DisplaySettings": { |
| 43 | + "Octopus.ControlType": "SingleLineText" |
| 44 | + } |
| 45 | + }, |
| 46 | + { |
| 47 | + "Id": "47eaaeaf-e414-4b34-ba7d-7d84715fd2ea", |
| 48 | + "Name": "redeploy.prompted.variables", |
| 49 | + "Label": "Prompted variables", |
| 50 | + "HelpText": "Enter any prompted variables and their values in the format `VariableName=VariableValue`. Enter one entry per line.\n\nExample: `MyVar=MyValue`", |
| 51 | + "DefaultValue": "", |
| 52 | + "DisplaySettings": { |
| 53 | + "Octopus.ControlType": "MultiLineText" |
| 54 | + } |
| 55 | + }, |
| 56 | + { |
| 57 | + "Id": "c0137294-b4e3-406e-a36d-fd7aca2731e7", |
| 58 | + "Name": "redeploy.prompted.useexisting", |
| 59 | + "Label": "Use previous prompted variables", |
| 60 | + "HelpText": "Tick this box to use the same prompted variables and values from the previous deployment.\n\nNote: This setting will override anything in the `Prompted variables` input with the values from the previous deployment. In addition, it will use the values from the latest deployment of the Release.\n\nNote: Please be aware that this template uses the API to retrieve variable which will not work sensitive values.", |
| 61 | + "DefaultValue": "False", |
| 62 | + "DisplaySettings": { |
| 63 | + "Octopus.ControlType": "Checkbox" |
| 64 | + } |
| 65 | + } |
| 66 | + ], |
| 67 | + "StepPackageId": "Octopus.Script", |
| 68 | + "$Meta": { |
| 69 | + "ExportedAt": "2024-06-18T23:04:33.802Z", |
| 70 | + "OctopusVersion": "2024.2.9220", |
| 71 | + "Type": "ActionTemplate" |
| 72 | + }, |
| 73 | + "LastModifiedBy": "twerthi", |
| 74 | + "Category": "octopus" |
| 75 | +} |
0 commit comments