How to use SpecialVariables in Octopus.Client

I want to know what was the previous successful deployment in a machine. this is what I have so far, but I don’t find anyway to see if the deployments that match my criteria were successful or not.

Also, How would I use PreviousSuccessful.Id in this context?

		var repository = new OctopusRepository(endpoint);
		EnvironmentResource environment = repository.Environments.FindByName(environmentName);
		ProjectResource project = repository.Projects.FindByName(projectName);
		ResourceCollection<DeploymentResource> deployments =
			repository.Deployments.FindAll( new string[] {project.Id}, new string[] {environment.Id});

Thank you.

Hi,

Thanks for getting in touch!
The variable Octopus.Deployment.PreviousSuccessful.Id can be used with repository.Deployments.Get(id) to get the deployment that you are looking for.

Hope this helps!
Vanessa

HI Vanessa,

Thank you for your help. I tried doing that. However, ‘Octopus.Deployment.PreviousSuccessful.Id’ always returns the string value ‘“Octopus.Deployment.PreviousSuccessful.Id”’.

Also, in which context does that variable know which machine and project I am referring to. Does PreviousSuccessful.Id, returns the id of previous successful deployment among all projects and machines?

Hi,

You need to retrieve the task that is related to each deployment and check that state of the task to find the latest successful deployment to a machine.

The below sample should get you what you are after.

static void Main(string[] args)
{
    var client = new OctopusClient(new OctopusServerEndpoint(HOST_URL, API_KEY));
    var repo = new OctopusRepository(client);

    var environment = repo.Environments.FindByName(EnvironmentName);
    var project = repo.Projects.FindByName(ProjectName);

    var previousSuccessfulDeployment =
        repo.Deployments.FindAll(new[] {project.Id}, new[] {environment.Id})
            .Items.OrderByDescending(d => d.Created)
            .FirstOrDefault(d => WasSuccessful(repo, d));

    Console.ReadLine();
}

private static bool WasSuccessful(OctopusRepository repo, DeploymentResource deploymentResource)
{
    var task = repo.Tasks.Get(deploymentResource.TaskId);
    return task != null && task.State == TaskState.Success;
}

Hope that helps!

Henrik

Thank you , that is great.