Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This script publishes an Octopus Deploy Runbook and then runs that Runbook
$ErrorActionPreference = "Stop";
# Define working variables
$octopusURL = "https://octopus.url"
$octopusAPIKey = "YouR API Key"
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }
$spaceName = "Your Space Name"
$projectName = "Your Project name"
$runbookName = "Your runbook name"
$environmentNames = @("Test", "Production") # You can specify multiples here or just a since environment
$environmentIds = @()
# Get space
$spaces = Invoke-RestMethod -Uri "$octopusURL/api/spaces?partialName=$([uri]::EscapeDataString($spaceName))&skip=0&take=100" -Headers $header
$space = $spaces.Items | Where-Object { $_.Name -eq $spaceName }
# Get project
$projects = Invoke-RestMethod -Uri "$octopusURL/api/$($space.Id)/projects?partialName=$([uri]::EscapeDataString($projectName))&skip=0&take=100" -Headers $header
$project = $projects.Items | Where-Object { $_.Name -eq $projectName }
# Get runbook
$runbooks = Invoke-RestMethod -Uri "$octopusURL/api/$($space.Id)/projects/$($project.Id)/runbooks?partialName=$([uri]::EscapeDataString($runbookName))&skip=0&take=100" -Headers $header
$runbook = $runbooks.Items | Where-Object { $_.Name -eq $runbookName }
# Get environments
$environments = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/environments/all" -Headers $header) | Where-Object {$environmentNames -contains $_.Name}
foreach ($environment in $environments)
{
$environmentIds += $environment.Id
}
# Get a runbook snapshot template
$runbookSnapshotTemplate = Invoke-RestMethod -Uri "$octopusURL/api/$($space.Id)/runbookProcesses/$($runbook.RunbookProcessId)/runbookSnapshotTemplate" -Headers $header
# Create a runbook snapshot
$body = @{
ProjectId = $project.Id
RunbookId = $runbook.Id
Name = $runbookSnapshotTemplate.NextNameIncrement
Notes = $null
SelectedPackages = @()
}
# Include latest built-in feed packages
foreach($package in $runbookSnapshotTemplate.Packages)
{
if($package.FeedId -eq "feeds-builtin") {
# Get latest package version
$packages = Invoke-RestMethod -Uri "$octopusURL/api/$($space.Id)/feeds/feeds-builtin/packages/versions?packageId=$($package.PackageId)&take=1" -Headers $header
$latestPackage = $packages.Items | Select-Object -First 1
$package = @{
ActionName = $package.ActionName
Version = $latestPackage.Version
PackageReferenceName = $package.PackageReferenceName
}
$body.SelectedPackages += $package
}
}
$body = $body | ConvertTo-Json -Depth 10
$runbookPublishedSnapshot = Invoke-RestMethod -Method Post -Uri "$octopusURL/api/$($space.Id)/runbookSnapshots?publish=true" -Body $body -Headers $header
# Re-get runbook
$runbook = Invoke-RestMethod -Uri "$octopusURL/api/$($space.Id)/runbooks/$($runbook.Id)" -Headers $header
# Publish the snapshot
$runbook.PublishedRunbookSnapshotId = $runbookPublishedSnapshot.Id
Invoke-RestMethod -Method Put -Uri "$octopusURL/api/$($space.Id)/runbooks/$($runbook.Id)" -Body ($runbook | ConvertTo-Json -Depth 10) -Headers $header
Write-Host "Published runbook snapshot: $($runbookPublishedSnapshot.Id) ($($runbookPublishedSnapshot.Name))"
# Re-Get Runbook info to run
$runbook = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/runbooks/all" -Headers $header) | Where-Object {$_.Name -eq $runbookName -and $_.ProjectId -eq $project.Id}
# Run runbook per selected environment
foreach ($environmentId in $environmentIds)
{
# Create json payload
$jsonPayload = @{
RunbookId = $runbook.Id
RunbookSnapshotId = $runbook.PublishedRunbookSnapshotId
EnvironmentId = $environmentId
TenantId = $tenantId
SkipActions = @()
SpecificMachineIds = @()
ExcludedMachineIds = @()
}
# Run runbook
Invoke-RestMethod -Method Post -Uri "$octopusURL/api/$($space.Id)/runbookRuns" -Body ($jsonPayload | ConvertTo-Json -Depth 10) -Headers $header
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment