Slow Queries to Determine Latest Release

Hi,

We have version 2.6.5.1010 of Octopus Deploy installed on a t2.medium in AWS.

As part of our deployment scripts, we determine the latest successful release made to an environment (i.e. if I’m scaling in prod, I want to find the latest successful release made to the production environment so that I am in sync).

We do not use the “latest” deployment flag within Octopus itself because when we first started doing automated deployments, I have a vague memory of it not always returning the correct release. I cannot qualify this however, because I have forgotten exactly why we’re doing our own “get latest release” code.

The issue at hand is that our “get latest release” code is taking over a minute to execute. This code is written in Powershell, and uses the Octopus client libraries.

$endpoint = new-object Octopus.Client.OctopusServerEndpoint $octopusServerUrl, $octopusApiKey
$repository = new-object Octopus.Client.OctopusRepository $endpoint

$env = $repository.Environments.FindByName($environmentName)
$project = $repository.Projects.FindByName($projectName)
$deployments = $repository.Deployments.FindMany({ param($x) $x.EnvironmentId -eq $env.Id -and $x.ProjectId -eq $project.Id })

if ($deployments | Any)
{
    Write-Verbose "Deployments of project [$projectName] to environment [$environmentName] were found. Selecting the most recent successful deployment."
    $latestDeployment = $deployments |
        Sort -Descending -Property Created |
        First -Predicate { $repository.Tasks.Get($_.TaskId).FinishedSuccessfully -eq $true } -Default "latest"

    $release = $repository.Releases.Get($latestDeployment.ReleaseId)
}
else
{
    Write-Verbose "No deployments of project [$projectName] to environment [$environmentName] were found."
}

$version = if ($release -eq $null) { "latest" } else { $release.Version }

Write-Verbose "The version of the recent successful deployment of project [$projectName] to environment [$environmentName] was [$version]. 'latest' indicates no successful deployments, and will mean the very latest release version is used."

return $version

We have an installation of Octopus 3 that we are slowly migrating to, but I cannot rely on that migration to fix the performance issues present in these scripts.

My question is:

  • Have I approached the problem entirely incorrectly? Will simply using the “latest” in place of the version guarantee that I get the last successful release of the target project to the target environment?
  • Is there a better way to query for the information I need? I’d love to be able to push all of the logic into an API request but I’m not sure how to do it.

Thanks.

Hi Todd,

You should be able to use the dynamic dashboard API to achieve the result you are after, you’d have to use Invoke-RestMethod though as there is currently no way to pass the includePrevious parameter using Octopus.Client. See sample below:

$env = $repository.Environments.FindByName($environmentName)
$project = $repository.Projects.FindByName($projectName)

$uri = "$baseUri/api/dashboard/dynamic?environments=$($env.Id)&projects=$($project.Id)&includePrevious=True"
$headers = @{"X-Octopus-ApiKey" = $octopusApiKey}
$dash = Invoke-RestMethod -Uri $uri -Headers $headers -Method Get

if ($dash.PreviousItems -and $dash.PreviousItems.Count -gt 0)
{
    Write-Host "Deployments of project [$projectName] to environment [$environmentName] were found. Selecting the most recent successful deployment."
    $deployment = $dash.PreviousItems | Select-Object -First 1
}
else
{
    Write-Host "No deployments of project [$projectName] to environment [$environmentName] were found."
}

$version = if ($deployment -eq $null) { "latest" } else { $deployment.ReleaseVersion }

Write-Verbose "The version of the recent successful deployment of project [$projectName] to environment [$environmentName] was [$version]. 'latest' indicates no successful deployments, and will mean the very latest release version is used."

return $version

I hope that helps!

Thank you and best regards,
Henrik

Thank you for the alternate solution. Is that available in Octopus Deploy 2?

Does this mean that the “latest” keyword will not do the same behaviour when used from Octo.exe?

Hi Todd,

I’ve just tested to make sure the API call returns what I’d expect and it does.

Using latest with Octo.exe will work the same.

Thank you and best regards,
Henrik

Hi Henrik,

Thank you again for the help. I managed to get some time this afternoon to look into using your solution and unfortunately it does not do what we need it to. The dashboard endpoint seems to be limited to just the current and previous deployment, and if both of those happened to fail (which happens frequently in our CI environment) you cannot use it to get the last successful deployment.

On the upside, because you pointed out using the API directly I managed to dramatically improve the speed at which the query executes by paging through the list of tasks for the project/environment from the API where they are related to deployment and then (somewhat hackily) parse the version out of the description.

Its not great and I definitely need to improve it, but its much faster at least.

Thank you.

Hi Todd,

Sorry to hear the solution didn’t work the way you needed it to (I was sure I tested it with a project/environment combo that had failed deployments in it but I must’ve been mistaken) but great to hear that you at least was able to improve your current solution.

Thanks and best regards,
Henrik

Hi,

I will be in intermittent contact over the coming 3 weeks (8 February 2016 to 29 February 2016), as I am on annual leave.

I will still be recieving emails, but cannot guarantee timely responses.

Have a nice day.

Todd