Is it possible to search by Role Tags

Hi Team,

Is it possible to search by the role tags all the projects they are used in?
Please let me know if you need more clarification on this.

Hi @ananth.tatachar.private,

Thanks for getting in touch! This is certainly possible, though not through the UI and would require going through the API to get this information. Another user has recently posted a C# script to get this information which I’ll paste below (referencing this thread):

// find the projects that reference the roles used by particular machines

// initialize endpoint and repo
var endpoint = new OctopusServerEndpoint(Environment.GetEnvironmentVariable("OCTOPUS_SERVER"), Environment.GetEnvironmentVariable("OCTOPUS_CLI_API_KEY"));
var repository = new OctopusRepository(endpoint);

// get the machines we are interested in
string[] machineNames = {"MTLWPVSCWEB11","MTLWPVSCWEB12","MTLWPVSCWEB13"};
var machines = repository.Machines.FindByNames(machineNames);

// get all the roles used by those machines
var roles = machines.SelectMany(m => m.Roles).Distinct();
Debug.WriteLine($"Looking for projects using these roles: [{String.Join(",",roles)}]");
Debug.WriteLine("----")

// get all the project groups to a dictionary for later lookup
var projGroups = repository.ProjectGroups.FindAll().ToDictionary(pg => pg.Id);

// get all the projects and iterate over them
var projs = repository.Projects.FindAll();
foreach (var p in projs) {
    // fetch the project's current deployment process
	var d = repository.DeploymentProcesses.Get(p.DeploymentProcessId);
	// see if the deployment process has any steps scoped to the roles of interest
	if (d.Steps.Any(st => st.Properties.ContainsKey("Octopus.Action.TargetRoles") && roles.Contains(st.Properties["Octopus.Action.TargetRoles"].Value)))
	{
		// output the project name and the group it's in
		Debug.WriteLine($"{p.Name} : {projGroups[p.ProjectGroupId].Name}");
	}
}

I hope this helps! Don’t hesitate to reach out if you have any questions or concerns moving forward. :slight_smile:

Best regards,

Kenny

1 Like

Thanks a lot Kenny. I will try it out and get back to you.
Thanks again.

1 Like

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