Goal
Restore the deployment process steps of a project after they were deleted, by using the Octopus REST API to copy the deployment process snapshot from an earlier release back onto the project. This applies to projects that don't use Config as Code, on both Octopus Cloud and self-hosted Octopus Server. Octopus has no built-in way to recover deleted steps.
Prerequisites
- A release of the project that was created before the steps were deleted. Each release stores a snapshot of the deployment process, and that snapshot is the source of the restored steps. Any changes made to the process after that release was created are not recovered.
If no release exists from before the steps were deleted, for example the project is new and has never had a release, follow Restore from the audit log instead. - An API key for a user who can view the project and its releases, edit the project's deployment process, and view the audit log. Viewing the audit log is only needed if you restore from the audit log.
- The name of the space that contains the project, for example
Default. - PowerShell on a machine that can reach your Octopus instance, whether it's Octopus Cloud or a self-hosted Octopus Server.
Steps
1. Choose a release
Find the version number of a release that was created before the steps were deleted. For example, open the project's Releases page and choose a release that was created while the process was still intact.
2. Save the script
Copy the following script into a new file named Restore-DeploymentProcess.ps1, and review it so you understand what it does before you run it. The script:
- Finds the space by name.
- Finds the project by name.
- Finds the release by version number.
- Reads the deployment process snapshot stored in that release, and stops with an error if the snapshot contains no steps.
- Clears the step and action IDs so they are created again as new steps.
- Replaces the steps in the project's current deployment process with the steps from the snapshot.
$ErrorActionPreference = "Stop"
$OctopusUrl = "https://octopus-url"
$ApiKey = "API-XXXXXXXXXXX"
$SpaceName = ""
$ProjectName = ""
$ReleaseVersion = ""
$headers = @{
"X-Octopus-ApiKey" = $ApiKey
"x-octopus-user-agent" = "Deployment process recovery script"
}
function Get-Octopus($url) {
Invoke-RestMethod -Uri $url -Headers $headers
}
function Clear-StepIds($steps) {
foreach ($step in $steps) {
$step.Id = $null
foreach ($action in $step.Actions) {
$action.Id = $null
}
}
return @($steps)
}
# Get space
$spaces = Get-Octopus "${OctopusUrl}/api/spaces/all"
$space = $spaces | Where-Object { $_.Name -eq $SpaceName } | Select-Object -First 1
if (-not $space) {
throw "Space '$SpaceName' was not found."
}
Write-Host "Using space '$($space.Name)' with ID $($space.Id)"
# Get project
$projectList = Get-Octopus "${OctopusUrl}/api/$($space.Id)/projects?name=$([uri]::EscapeDataString($ProjectName))"
$project = $projectList.Items | Where-Object { $_.Name -eq $ProjectName } | Select-Object -First 1
if (-not $project) {
throw "Project '$ProjectName' was not found."
}
# Get specific release
$encodedReleaseVersion = [uri]::EscapeDataString($ReleaseVersion)
$releaseList = Get-Octopus "$OctopusUrl/api/$($space.Id)/projects/$($project.Id)/releases?searchByVersion=$encodedReleaseVersion&skip=0&take=100"
$targetRelease = $releaseList.Items |
Where-Object { $_.Version -eq $ReleaseVersion } |
Select-Object -First 1
if (-not $targetRelease) {
throw "Release '$ReleaseVersion' was not found for project '$ProjectName'."
}
Write-Host "Using deployment process snapshot from release: $($targetRelease.Version)"
# Get deployment process snapshot from release
if ($targetRelease.Links.ProjectDeploymentProcessSnapshot) {
$snapshotUrl = $OctopusUrl + $targetRelease.Links.ProjectDeploymentProcessSnapshot
}
elseif ($targetRelease.Links.DeploymentProcessSnapshot) {
$snapshotUrl = $OctopusUrl + $targetRelease.Links.DeploymentProcessSnapshot
}
else {
$targetRelease.Links | ConvertTo-Json -Depth 10
throw "Could not find a deployment process snapshot link on release '$ReleaseVersion'."
}
$snapshotProcess = Get-Octopus $snapshotUrl
if (-not $snapshotProcess.Steps -or $snapshotProcess.Steps.Count -eq 0) {
throw "Release '$ReleaseVersion' snapshot does not contain any deployment steps."
}
# Get current project deployment process
$currentProcessUrl = $OctopusUrl + $project.Links.DeploymentProcess
$currentProcess = Get-Octopus $currentProcessUrl
# Replace current steps with snapshot steps
$currentProcess.Steps = @(Clear-StepIds $snapshotProcess.Steps)
$body = $currentProcess | ConvertTo-Json -Depth 20
Invoke-WebRequest `
-Uri $currentProcessUrl `
-Headers $headers `
-Method Put `
-Body $body `
-ContentType "application/json"
Write-Host "Recovered deployment process for '$ProjectName' from release '$($targetRelease.Version)'."
3. Set the variables
Edit the variables at the top of the script:
-
$OctopusUrl: the base URL of your Octopus instance, without a trailing slash or a path. Use the URL you sign in to the Octopus Web Portal with: - Octopus Cloud:
https://your-instance.octopus.app - Octopus Server:
https://octopus.example.com -
$ApiKey: the API key you created for this task. -
$SpaceName: the exact name of the space that contains the project, for exampleDefault. -
$ProjectName: the exact name of the project whose steps were deleted. -
$ReleaseVersion: the exact version number of the release you chose in the first step.
4. Run the script
Run the script in PowerShell:
.\Restore-DeploymentProcess.ps1
Restore from the audit log if no release exists
Use this method only when there is no release from before the steps were deleted. The audit log records the deployment process as it was when the steps were deleted, so you can restore from that event instead of from a release. This also works for a brand new project that has never had a release.
You need the same API key, space name and PowerShell access as the main method, plus audit log retention that still covers the date of the deletion.
1. Find the deletion event
- In the Octopus Web Portal, go to Configuration then Audit.
- Filter by the project, and narrow the date range to when the steps were deleted.
- Find the event where the deployment process was modified and the steps were removed, and open it. Check that the event details list the deleted steps.
- Copy the event ID, for example
Events-12345. The Octopus Web Portal might not show the event ID. If you can't find it, select Export to download the audit events for the filtered results, then find the event in the exported file.
2. Save and run the script
Copy the following script into a new file named Restore-DeploymentProcessFromAudit.ps1, set the variables at the top, then run it with .\Restore-DeploymentProcessFromAudit.ps1. $OctopusUrl, $ApiKey, $SpaceName and $ProjectName take the same values as in the main method. $EventId is the event ID you copied in the previous step.
The script reads the deleted steps from the audit event and stops with an error if the event doesn't record removed steps. It skips any step whose name already exists in the project, clears the step and action IDs so the remaining steps are created again as new steps, and adds them to the project's current deployment process.
$ErrorActionPreference = "Stop"
$OctopusUrl = "https://octopus-url"
$ApiKey = "API-XXXXXXXXXXX"
$SpaceName = ""
$ProjectName = ""
$EventId = ""
$headers = @{
"X-Octopus-ApiKey" = $ApiKey
"x-octopus-user-agent" = "Deployment process recovery script"
}
function Get-Octopus($url) {
Invoke-RestMethod -Uri $url -Headers $headers
}
# Get space
$spaces = Get-Octopus "${OctopusUrl}/api/spaces/all"
$space = $spaces | Where-Object { $_.Name -eq $SpaceName } | Select-Object -First 1
if (-not $space) {
throw "Space '$SpaceName' was not found."
}
# Get project
$projectList = Get-Octopus "${OctopusUrl}/api/$($space.Id)/projects?name=$([uri]::EscapeDataString($ProjectName))"
$project = $projectList.Items | Where-Object { $_.Name -eq $ProjectName } | Select-Object -First 1
if (-not $project) {
throw "Project '$ProjectName' was not found."
}
# Get the deleted steps from the audit event
$event = Get-Octopus "${OctopusUrl}/api/$($space.Id)/events/${EventId}"
# Make sure the event is one where steps were removed, not edited
$removals = @($event.ChangeDetails.Differences | Where-Object { $_.op -eq "remove" })
if ($removals.Count -eq 0) {
throw "Event '$EventId' does not record any removed steps. Check that you copied the ID of the deletion event."
}
$deletedSteps = @($event.ChangeDetails.DocumentContext.Steps)
if ($deletedSteps.Count -eq 0) {
throw "Event '$EventId' does not contain any deployment steps."
}
# Get the current deployment process
$currentProcessUrl = $OctopusUrl + $project.Links.DeploymentProcess
$currentProcess = Get-Octopus $currentProcessUrl
$currentSteps = @($currentProcess.Steps)
# Skip any step that already exists in the process, so nothing is duplicated
$stepsToRestore = @()
foreach ($step in $deletedSteps) {
if ($currentSteps | Where-Object { $_.Name -eq $step.Name }) {
Write-Host "Skipping '$($step.Name)': a step with this name already exists."
continue
}
$step.Id = $null
foreach ($action in $step.Actions) {
$action.Id = $null
}
$stepsToRestore += $step
}
if ($stepsToRestore.Count -eq 0) {
throw "All steps in event '$EventId' already exist in the project. Nothing to restore."
}
Write-Host "Restoring $($stepsToRestore.Count) step(s):"
$stepsToRestore | ForEach-Object { Write-Host " - $($_.Name)" }
# Add the deleted steps to the current deployment process
$currentProcess.Steps = $currentSteps + $stepsToRestore
$body = $currentProcess | ConvertTo-Json -Depth 20
Invoke-WebRequest `
-Uri $currentProcessUrl `
-Headers $headers `
-Method Put `
-Body $body `
-ContentType "application/json"
Write-Host "Restored $($stepsToRestore.Count) step(s) for '$ProjectName' from event '$EventId'."
The audit log method adds the deleted steps to whatever steps the project already has, whereas the release method replaces them. Restored steps are added to the end of the process, so check the step order once the script finishes.
Then follow the steps in the next section to verify the result.
Verify
- Confirm the script finished without errors and printed
Recovered deployment process for '<project name>' from release '<release version>'. - In the Octopus Web Portal, open the project and select Process. Confirm the steps from the release are listed.
- Open a few of the restored steps and confirm their settings match what you expect, then create a test release and deploy it to a non-production environment.
Related
- Deployment process: how deployment processes and steps work in Octopus.
- How to create an API key: how to create the API key the script needs.
- Octopus REST API: an overview of the API the script calls.