How to get repository name by Project name using Octopus Clients library?

I want to know the version control repository name by the Project name in Octopus through API. It would be great to get the branch name of the repo also for the different-2 deployment environments.

For example, there are around 40-50 projects. There are multiple environments like Beta, UAT and Production. Source control branches could be different for each environment.

I am using C# as the programming language.

Hey @vivek.nuna,

I’m just checking to see how this is possible and I’ll get back to you very soon.

Thanks

@adam.close Thank you, I’ll wait for your update.

@adam.close do you have any update on this?

Hey @vivek.nuna,

I’ve checked and using the Octopus API to get project details by the project name returns the version control repository url. The example script here shows how to get project details using the Octopus API via PowerShell.

I used this project in our samples instances to check this out.

Project request URL;

Returned version control information;

image

The repro will be the same for all environments in a release created in Octopus so are you trying to get the branch used in a release?

Thanks

@adam.close Thank you for your response. Yes, for a given project Repo is the same but the branches could be different for each environment.

For example.

For Project ABC, Beta Environment, Branch is Beta
For Project ABC, Production Environment, Branch is Prod
For Project ABC, UAT Environment, Branch is UAT

I have tried with this request, but it does not give repo-related information. Could you please check?

https://gc-deploy.intlfcstone.net/api/Spaces-1/projects/fxtrader

fxtrader is my project name. @adam.close

Hey @vivek.nuna,

That’s strange! This might be a dumb question from me but is version controlled configured in that project? Can you view the version control setting with the same user your using the API with?

Thanks,
Adam

@adam.close yes version control configured for the project. We are using this project from years. I’m hitting this get request directly from the browser? So I have one question do we need to explicitly pass the user credentials. I think it’s not giving the info due to security reasons otherwise any person can see the details so it should not be public.

If we need to pass the user credentials then how to pass and what are the parameters. Please provide a sample.

@adam.close Hi Adam, Hope you had a nice weekend. Did you get a chance to look into this?

@adam.close Do you have any update for me? Please respond.

Hi @vivek.nuna

Just jumping in for Adam.

I’d like to understand your use case of changing the branch for the deployment of a project to an environment with a similar name.

Specifically, I’d like to understand how it’s working currently for you. In a version-controlled project, the lifecycle of the project, which isn’t version-controlled, applies to all the releases you see on the overview, no matter the branch name.

Therefore, I’m keen to find out how you are working around this from a general project overview perspective. Every time you change the lifecycle, the environments you can deploy to will also change in the project.

Adam was correct in his previous messages though, a branch is associated with a release, and not directly with an environment. So you might want to query releases for a project, get the deployments associated with the release and then join the two items (branch and environment) up that way.

Best,

@mark.harrison For different environments, the codebase could be different so we have created a separate branch for each environment. Branch name has no standard, It could be anything. All projects are version controlled. We are using git repositories and we use Azure DevOps to manage the repo and pipelines.

But I am not getting the branch name with release in the above-mentioned get call.
And could you please provide me information that how can I get environment info for projects?

In brief, my ultimate goal is to get the branch names for each deployment environment of a given project from Octopus.

Hi @vivek.nuna

When you want to deploy to one of the other environments for a branch, are you also changing the lifecycle in the project, or does the lifecycle contain all environments? A screenshot of what the lifecycle looks like in one of your projects would be helpful.

As I mentioned previously, a deployment environment could have multiple branches associated with it as each environment could have many releases deployed to it. That may in practise for your use case only have one though.

As a general rule, we don’t write scripts for our customers. To answer your query though, there are probably a couple of ways to get the information you are after

Using the Octopus.Client C# library you would need to (for each project):

  1. Get the project record
  2. Get all releases you are interested in (since branches are tied to a release) for the project. This will give you the branch info you are after.
  3. Get the deployments for each release and that will give you the deployment environment you are after.
  4. From these two sets of records, you will have the release number, branch associated with the release and the deployment environments for that release.

I hope that helps, but please let me know if you have any questions.

Best,

@mark.harrison when we have the same branch for different environments for a given project, wo directly deploy from Octopus UI.

So for example Beta environment has been deployed from beta branch. Then we directly go to octopus and click the required beta environment version from octopus and deploy to UAT environment.

Then to deploy to production we click the required UAT environment deployed version from octopus and deploy to production.

@mark.harrison could you please provide the relevant octopus client library method for each points which you have mentioned in your last comment (for each point from 1,4)

Note: These examples use the latest Octopus.Client available on nuget.org (at the time of writing version 13.0.3875)

Get the project record

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

Get all releases you are interested in (since branches are tied to a release) for the project. This will give you the branch info you are after.

This example gets the first 100 releases for a project:

var releases = repositoryForSpace.Projects.GetReleases(project, skip: 0, take: 100);

Get the deployments for each release and that will give you the deployment environment you are after.

This example gets the first 100 deployments for a release:

var deployments = repositoryForSpace.Releases.GetDeployments(release, skip: 0, take: 100);

From these two sets of records, you will have the release number, branch associated with the release and the deployment environments for that release.

This example gets all environments, and then loops through each release’s deployments and prints tot he console the release, deployment environment and git reference:

var environments = repositoryForSpace.Environments.GetAll();
foreach (var deployment in deployments.Items)
{
	Console.WriteLine(string.Format("Release {0} deployed to {1} has gitref: {2} ({3})", release.Version, environments.First(e => e.Id == deployment.EnvironmentId).Name, release.GitReference.GitRef, release.GitReference.GitCommit));
}

Best

@mark.harrison thank you for providing the relevant code. As it’s too late, ’ll try today morning.

@mark.harrison Thank you for providing the above examples.

I tried, but

release.GitReference is coming as null for all the projects. I have tried the latest versions 13.0.3875 and 13.0.3882 .

Could you please check, what is the issue?

Hi @vivek.nuna

It’s hard to know what the cause of the issue is without more details of the releases that don’t have the git reference.

I’d recommend checking the projects are versioned controlled and that the releases that have a null value were also created from a branch.

Releases that were created prior to converting to version control won’t have a git reference as they would have been created when the deployment process was stored in the database.

When I say version controlled, I’m referring to the feature known as Configuration as Code.

It’s also worth verifying the Octopus version you are running supports Config as Code. That feature was added in 2022.1

Best,

@mark.harrison It’s in version control, As I told We are working on these projects for the last 2-3 years.

Please refer to these screenshots.

This is my Octopus version Download: 2019.9.10 - Octopus Deploy

So As per your comment, I cannot get the branch details with my version? Or is there any workaround for the same?