Get Releases Octopus.Client

I am trying to query Octopus via the Octopus.Client to see if a release has been created. I am using C#:

When I run this, and release 921 exists (2.25.6.921), the query returns instantly:

var release = _octopusRepository.Releases.FindOne(r => r.Version.Contains(“921”));

When I run this, and release 921111 doesn’t exist, the query takes 30+ seconds.

var release = _octopusRepository.Releases.FindOne(r => r.Version.Contains(“921111”));

Is there a way to speed this up? The application I am using can’t really have a 30 second call for this.

I’ve tried to limit by project and other ways (FindAll, FindMany then filtering) without much luck.

Thanks.

Greetings @cmullins, thanks for reaching out! Would you be able to elaborate on the problem you’re trying to solve?

Regards,

Shawn

When a release isn’t found, I would expect Octopus to reply instantly the same way it does for when it finds a release. Currently it is taking 30+ seconds.

There are a few factors that could affect this. Do you have a large number of releases? What are the specifications of your database server? Do you run routine database maintenance to ensure you don’t have too much index fragmentation? The tests I ran on my end came back in usually a second or less, however, I don’t think we’re dealing in apples-to-apples as for as release count goes.

I’d like to better understand what the purpose of the application is to see if there might be another way to accomplish the same goal :slight_smile:

Regards,

Shawn

Hi Shawn,

We have about 2000 releases. We do not have the DB maintenance to fix index fragmentation, but I will reach out to my DBA about that. The application is a web tool that allows people to request a deployment to Staging and Production. I want to prevent users from requesting a deployment to Staging if there isn’t a release build/deployment to a lower dev environment first.

I hate doing this, but I ended up querying the Octopus database directly with this:

var sql = “SELECT Project.Id FROM Project INNER JOIN Release ON Project.Id = Release.ProjectId " +
" WHERE(Project.Name = N’” + applicationName + “') AND (Release.Version LIKE '%” + releaseNumber + “')”;

This query comes back instantly which suggests to me that we are not dealing with a fragmented index.

While this solution has solved my problem, I would prefer to do this through the Octopus SDK, but I wouldn’t waste too much time on it.

Chris

The best I could come up with is to filter first on project and get those releases first, then do check for the release number. I thought I saw that you’d tried this, but if I recall, it was within a single Linq/lambda expression versus broken up.

// See https://aka.ms/new-console-template for more information
using Octopus.Client;
using Octopus.Client.Model;

// Declare working varibles
var octopusURL = "https://YourServer";
var octopusAPIKey = "API-XXXXXXXXXXXXXX";
var spaceName = "Default";

// Create repository object
var endpoint = new OctopusServerEndpoint(octopusURL, octopusAPIKey);
var repository = new OctopusRepository(endpoint);
var client = new OctopusClient(endpoint);

try
{
    // Get space
    var space = repository.Spaces.FindByName(spaceName);
    var repositoryForSpace = client.ForSpace(space);

    // Get project
    var project = repositoryForSpace.Projects.FindByName("BestBags");

    // Get releases
    var releases = repositoryForSpace.Releases.FindMany(p => p.ProjectId == project.Id);

    //var release = repositoryForSpace.Releases.FindOne(r => r.Version.Contains("202546578512"));

    //Console.WriteLine(string.Format("You have {0} releases to search from.", repositoryForSpace.Releases.FindAll().Count));

    var x = releases.Where(r => r.Version.Contains("2022564654"));

    Console.WriteLine(String.Format("Number is {0}", x.Count()));
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
    return;
}

Hi Shawn,

Thanks for checking. I did try the way you outlined below, but it still took a long time to load. I have the direct call to the database working and released, so as far as I’m concerned, you can close the ticket, unless you really want to get to the root cause.

Thanks.

Chris

Understood, thank you for getting back to me :slight_smile:

Regards,

Shawn

Hi @cmullins

I hope you don’t mind but I’ve noticed this type of query before. The Octopus.Client (and the rest API) has a search endpoint for release matching a specific version. You use it like so:

var octopusURL = "https://YourServer";
var octopusAPIKey = "API-XXXXXXXXXXXXXX";
var spaceName = "Default";
var projectName = "A project";

var releaseVersionSearch = "0.11";

// Create repository object
var endpoint = new OctopusServerEndpoint(octopusURL, octopusAPIKey);
var repository = new OctopusRepository(endpoint);
var client = new OctopusClient(endpoint);

try
{
	// Get space
	var space = repository.Spaces.FindByName(spaceName);
	var repositoryForSpace = client.ForSpace(space);

	// Get project
	var project = repositoryForSpace.Projects.FindByName(projectName);

	// Get releases based on version search
	var releaseCollection = repositoryForSpace.Projects.GetReleases(project, searchByVersion: releaseVersionSearch);
	var releases = releaseCollection.Items;
	Console.WriteLine(String.Format("Number of releases matching {0} is {1}", releaseVersionSearch, releases.Count));
}
catch (Exception ex)
{
	Console.WriteLine(ex.Message);
	return;
}

This is using one of the methods on the Projects repository in the Client. The method will allow the searching to be performed on the server, and not the calling application, which should result in a more efficient query and reduced time. It also has the benefit of not needing to query SQL directly.

Obviously performance will depend on many factors as Shawn has said, but I’d be interested to know what times you get using this method.

Best,

Hi Mark,

Thanks for checking further. This code does work, and it returns pretty much instantly.

Can you recommend the best place to find documentation/code examples on the SDK?

Thanks!

Chris

Hi @cmullins

Awesome, glad that worked in this case!

Currently, we have two main sources for code examples for the Client/SDK:

  1. Our API examples in our documentation - these have a bit more explanation around some of them, including the necessary parameters etc
  2. Our API GitHub repository - this will more likely always have more examples than our docs, as it can store examples that wouldn’t necessarily be useful to a wider audience.

If in doubt, always feel free to reach out to our team either here or at advice@octopus.com and we can always help out!

Hope that helps.

Best,

Great, thanks.

You’re very welcome :slight_smile:

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