Cancelling all InProgress/Queued environment between two days


Recently one of our internal customer 'OneDrive' reported as they have many InProgress/Queued environments. And they wanted to cancel all such environments and asked us to help. So I have written a program which would take account url, project name, definition id, NumberOfDaysBefore and NumberOfDaysAfter and Cancel the environment.

How to do:-

  1. GetReleases2Async() takes parameter as minCreatedTime, maxCreatedTime, top and continuationToken, so get all releases between specified time period and then iterate over each release and then to each environment and find inprogress or queued environment.
  2. Cancel the inprogress/queued environment. You may choose to delete the release as well so that retention can clear them off.

Code snippet:-

public static void CancelAllInProgressReleases(ReleaseHttpClient2 rmClient)
        {
            var maxModifiedTime = DateTime.Now.AddDays(-numberOfDaysBefore);
            var minModifiedTime = DateTime.Now.AddDays(-numberOfDaysAfter);

            try
            {
                int ctn = 0;
                IPagedCollection<Release> releases = null;
                do
                {
                    releases = rmClient.GetReleasesAsync2(
                    definitionId: definitionId,
                    statusFilter: ReleaseStatus.Active,
                    minCreatedTime: minModifiedTime,
                    maxCreatedTime: maxModifiedTime,
                    top: 100,
                    continuationToken: ctn,
                    expand: ReleaseExpands.Environments).Result;

                    ReleaseEnvironmentUpdateMetadata envMetaData = new ReleaseEnvironmentUpdateMetadata();
                    envMetaData.Status = EnvironmentStatus.Canceled;
                    envMetaData.Comment = "Good to cancel as it's pretty old";

                    foreach (var release in releases)
                    {
                        foreach (var environment in release.Environments)
                        {
                            if (environment.Status == EnvironmentStatus.Queued || environment.Status == EnvironmentStatus.InProgress)
                            {
                                try
                                {
                                    totalDeploymentToCancel++;
                                    Console.WriteLine("Cancelling the environment with releaseId: {0} ReleaseEnvironmentId: {1} ", release.Id, environment.Id);
                                    var result = rmClient.UpdateReleaseEnvironmentAsync(envMetaData, projectName, release.Id, environment.Id).Result;
                                    // rmClient.DeleteReleaseAsync(projectName, release.Id).SyncResult();
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine("=======================");
                                    Console.WriteLine("Not Cancelling the deployment with releaseId: {0} ReleaseEnvironmentId: {1} ", release.Id, environment.Id);
                                    Console.WriteLine(ex.Message);
                                    Console.WriteLine("=======================");
                                }
                            }
                        }
                    }

                    int.TryParse(releases.ContinuationToken, out ctn);
                } while (releases.ContinuationToken != null);

                Console.WriteLine("Total cancelled environments: {0}", totalDeploymentToCancel);
            }
            catch(Exception ex)
            {
                Console.WriteLine("Could not get releases with given information");
            }
        }
    }

Complete code :- https://github.com/chandan-anjani/VSTSRMAPISample/blob/master/OneDriveCancelReleases/OnDriveCancelReleases/Program.cs

Usage:- OneDriveCancelReleases.exe <ServerUrl> <ProjectName> <DefinitionId> <NumberOfDaysBefore> <NumberOfDaysAfter>


Comments (0)

Skip to main content