Calculate the deployments duration in Octopus

Hey team! I want to monitor octopus deployments (for a certain project and environment) and make sure they are always under 30 minutes.
Do you know if there is any way in Octopus to see how long a deployment to all tenants take ? The only way I see and it doesn’t cover some edge case scenarios would be:
1.Get number of tenants in that environment
2.Check last release and deployments tied to it
3. If all clients were deployed for that release, calculate start time of earliest deployment and end time of most recent deployment for that release,
If more than 30 minutes send an alert.
This scenario is not that simple and doesn’t cover some edge case scenarios for sure, I was wondering if getting the deployments elapsed time can be done in Octopus in another way ?

Hi Atefah,

Great question. We don’t have anything native, built in to Octopus to do something like that, however I think the best solution would be the following:

If you had a runbook that would:

  • identify any currently executing Deployments

  • check to see if it’s “Long-Running”, matches a specific Tenant, Environment and/or project name

  • Cancels/notifies about anything that matches all criteria

Then you could get the script to trigger every 5 minutes or so to get “around the 30 minute mark”. Scheduled runbook triggers - Octopus Deploy
It’s not going to be spot on 30 minutes, but it may be exactly what you are after.

I’ve actually modified one of our existing scripts with a few extra sections to achieve what I’ve explained above. It will require a little bit more work, but the pattern is there, which you should be able to use to get it to do exactly what you need it to do.

If you wanted to use the “send an Email” step to notify, you could create a project/runbook that only sends an email, then trigger the project, if the script matches all your criteria. Details about calling other deployments from a script can been seen here: Deployments - Octopus Deploy

Let me know how you go or if you need any further help.



$OctopusURL = $OctopusParameters["Global.Base.Url"]
$APIKey = $OctopusParameters["Global.Api.Key"]
$CurrentSpaceId = $OctopusParameters["Octopus.Space.Id"]
$EnvironmentsToCheck =
$TenantsToCheck = "Tenants-61"
$MaxRunTime = 15

$header = @{ "X-Octopus-ApiKey" = $APIKey }

Write-Host "Getting list of all spaces"
$spaceList = (Invoke-RestMethod "$OctopusUrl/api/Spaces?skip=0&take=100000" -Headers $header)
$cancelledTask = $false
$cancelledTaskList = ""

foreach ($space in $spaceList.Items)
    $spaceId = $space.Id
    if ($spaceId -eq $CurrentSpaceId)
        Write-Host "Checking $spaceId for running tasks (looking for executing tasks only)"
        $taskList = (Invoke-RestMethod "$OctopusUrl/api/tasks?skip=0&states=Executing&spaces=$spaceId&take=100000" -Headers $header)
        $taskCount = $taskList.TotalResults
        Write-Host "Found $taskCount currently running tasks"
        foreach ($task in $taskList.Items)
            write-host "Task Infomation"            
            write-host "."

			$taskId = $task.Id
            $taskDescription = $task.Description
            $taskDetails = (Invoke-RestMethod "$OctopusUrl/api/tasks/$taskID" -Headers $header)
            $deploymentID = $taskdetails.Arguments.DeploymentID
            $deploymentDetails = ( Invoke-RestMethod "$OctopusUrl/api/$($task.spaceid)/deployments/$($taskdetails.Arguments.DeploymentID)" -Headers $header)

            write-host "Task ID: "$taskId
            write-host "Task Description: "$taskDescription
            write-host "DeploymentID: " $DeploymentID
            write-host "TenantID: "$DeploymentDetails.TenantID
			write-host "Task Details: "
            write-host	$taskDetails
            write-host "."
			write-host "END Task Information"

            if ($task.Name -eq "Deploy"){
                # With auto deployment triggers enabled, the start time of the task cannot be trusted, need to find the events for the most recent deployment started
                $eventList = (Invoke-RestMethod "$OctopusUrl/api/events?regardingAny=$taskId&spaces=$spaceId&includeSystem=true" -Headers $header)
                foreach ($event in $eventList.Items){
                    if ($event.Category -eq "DeploymentStarted"){
                        $startTime = (Get-Date $event.Occurred)

                        # We found the most recent deployment event started we are curious about, stop looping through
                $startTime = (Get-Date $task.StartTime)

            $currentTime = Get-Date                        
            $dateDiff = $currentTime - $startTime

            Write-Host "The task $taskDescription has been running for $dateDiff"

# Filter by Tenant-ID
			if ($DeploymentDetails.TenantID -eq $TenantsToCheck){ 
   				if ($dateDiff.TotalMinutes -gt $MaxRunTime){
                	Write-Highlight "The task $taskDescription has been running for over $MaxRunTime minutes, this indicates a problem, let's cancel it"

					#Un-comment this line to "Cancel Tasks"
                	#Invoke-RestMethod "$OctopusUrl/api/tasks/$taskId/cancel" -Headers $header -Method Post

                	$cancelledTask = $true
                	$cancelledTaskList += $taskDescription + " "

Set-OctopusVariable -name "CancelledTask" -value $cancelledTask
Set-OctopusVariable -name "CancelledTaskList" -value $cancelledTaskList
1 Like

Thank you very much for the fast reply and this script Dane! I will use it as you suggested and let you know if I need your further help!


This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.