I’m trying to use the client API to determine the machine names that were deployed to of the last successful release for a project on an environment.
I can get the latest release through this:
DashboardResource item = repository.Dashboards.GetDynamicDashboard(new[] { projectId }, new[] { environmentId });
Although that would return me a failed release when I want the last successful one.
From the item I could get to the machine names list this:
string deploymentId = item.Items.First().DeploymentId;
var deployment = repository.Deployments.Get(deploymentId);
var task = repository.Tasks.Get(deployment.TaskId);
var details = repository.Tasks.GetDetails(task);
And then use something like: details.ActivityLog.Children[1].Children[0].Name to get the machine name.
I also thought about using Roles to determine the machine names however then I’d need to look at the Steps to find the step that uploaded the NuGet package (because Projects aren’t assigned Roles, Steps are). There is also a gap so that if a new server is added to a role for an environment but hasn’t yet been deployed to it will also be returned (unless, I suppose, it is disabled).
Please could you give me a steer on the best approach to getting the machine names?
As we don’t store the machines that were part of a deployment, you were on the right track with your solution.
I have modified it slightly to include previous deployments as well so that you can get the latest successful one if the latest wasn’t successful.
var dynamicDashboardUri = client.RootDocument.Link("DashboardDynamic");
var project = repo.Projects.FindByName("ProjectName");
var environment = repo.Environments.FindByName("EnvironmentName");
var dashboard = client.Get<Octopus.Client.Model.DashboardResource>(dynamicDashboardUri, new { projects = project.Id, environments = environment.Id, includePrevious = true}); // the includePrevious will as it's name suggests return all previous deployments for the project to the environment, this way you can get the latest successful deployment if the latest deployment failed.
Octopus.Client.Model.DashboardItemResource lastSuccessful;
if(dashboard.Items[0].State == Octopus.Client.Model.TaskState.Success)
{
lastSuccessful = dashboard.Items[0];
}
else
{
lastSuccessful = dashboard.PreviousItems.FirstOrDefault (pi => pi.State == Octopus.Client.Model.TaskState.Success);
}
var task = repo.Tasks.Get(lastSuccessful.TaskId);
var taskDetail = repo.Tasks.GetDetails(task);
var machines = taskDetail.ActivityLog.Children.First (c => c.Name.Equals("Acquire packages", StringComparison.OrdinalIgnoreCase)).Children.Select(c => c.Name);