I am trying to script getting number of Octopus Deployments for each space for a specific date (say yesterday AddDays(-1)). Am I going about this the right way?
Instead of going into the deployments repository, I instead went to the Audit log and filtered by date.
Add-Type -Path 'C:\temp\Octopus.Client.dll'
$apikey = '' # Get this from your profile
$octopusURI = '' # Your Octopus Server address
$spaceName = "Default"
$endpoint = new-object Octopus.Client.OctopusServerEndpoint $OctopusURI, $APIKey
$client = new-object Octopus.Client.OctopusClient $endpoint
$space = $client.ForSystem().Spaces.FindByName($spaceName)
$spaceRepository = $client.ForSpace($space)
[System.DateTimeOffset]$after = "9/8/2020"
[System.DateTimeOffset]$before = "9/9/2020"
$deploymentcount = 0
#return the all of the events that match the parameters
$events = $spacerepository.Events.FindMany(
{param($e) if(($e.Category -match "DeploymentStarted") -and ($e.Occurred -gt $after) -and ($e.Occurred -lt $before))
{
$True
}
})
#if you want to do more logic, you could implement it in the for loop
foreach ($event in $events){
$deploymentcount++
}
#or alternatively just do a count
#$events.Count
write-host $deploymentcount
You could also add some extra logic for DeploymentSucceeded or DeploymentFailed or other categories to get more advanced stats/numbers.
Please let me know if this works for you or if we need to find a different solution.
Hi Jeremy,
That looks good. The only thing is that I will be running this once daily and the date is hardcoded, so if I wanted to get yesterdays deployments.
So would this work:
[System.DateTimeOffset]$after = (Get-Date).AddDays(-1) (ie: yesterday)
[System.DateTimeOffset]$before = Get-Date (ie: today)
Hi Jeremy,
I have implemented the code and it works fine. For one of my spaces which do alot of deployments the script runs for along time and still dont get a number back for the number of deployments. Is there a way to improve the search to make it faster?
The only property I can use is the Occured which I have narrowed down using the dates $after and $before.
I get this error:
Exception calling “FindMany” with “1” argument(s): “Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.”
Yeah, that’s a pretty expensive API call because it’s hitting every row in your event table in your SQL database.
Let’s go back to working with deployments. A colleague of mine figured out that we should be using FindAll instead of GetAll, and then filter on the date. GetAll not working is why I moved to the event table for the solution.
This code works for me in testing.
Please test it and let me know if it works for you.
Add-Type -Path 'C:\temp\Octopus.Client.dll'
$apikey = '' # Get this from your profile
$octopusURI = '' # Your Octopus Server address
$spaceName = "Default"
$endpoint = new-object Octopus.Client.OctopusServerEndpoint $OctopusURI, $APIKey
$client = new-object Octopus.Client.OctopusClient $endpoint
$space = $client.ForSystem().Spaces.FindByName($spaceName)
$spaceRepository = $client.ForSpace($space)
[System.DateTimeOffset]$after = (Get-Date).AddDays(-1)
$deployments = $Spacerepository.Deployments.FindAll() | Where-Object {$_.Created -ge $after}
$deployments.count
There is also a reporting xml endpoint you can hit but you can only use REST API for that.
I’m kind of surprised that the deployments section is also timing out as that gets cleaned up by retention policies etc. What do your sql server metrics look like when you’re running the query? How long does it run before it times out?
I’m glad to hear its working. If it stops working again I would check your SQL metrics and see if the server is maxed out. You could also move the code to REST API and do a take command and only take the last X number of deployments. That would likely be much quicker, but it also has the potential for missing some if you set the number too low. For example, if you had 105 deployments and you only take the last 100, you would just get 100 as your number. You could definitely code it in to say, if deployment count -eq takevalue, add 50 to take value and rerun script or something like that.
If you try to implement the above and hit a roadblock please let me know.