VSTS

Access to VSTS Releases by C# in preview version

Posted on Updated on


vstsde

This post describes somes lines of code which permit you to access VSTS platform (Visual Studio Team Service), we describe fundamntal steps permit to access new objects and services, we begin by decsribing packages that you have to download from NUGET in order to develop access layers.

The benefits of this post is to identify easily list of packages and class that you have known, in order access datas, without forget that we are in preview version.

Firslty begin by adding theses packages, we remarks that we add Microsoft.VisualStudio.Services.Release.Client package with preview version.

Below full list of packages that you have download :

-Microsoft.AspNet.WebApi.Client with version =5.2.2
-Microsoft.AspNet.WebApi.Core with version =5.2.2
-Microsoft.IdentityModel.Clients.ActiveDirectory with version =3.13.5
-Microsoft.TeamFoundation.DistributedTask.Common.Contracts with version =15.120.0-preview
-Microsoft.TeamFoundationServer.Client with version =15.120.0-preview
-Microsoft.TeamFoundationServer.ExtendedClient with version =15.120.0-preview
-Microsoft.Tpl.Dataflow with version =4.5.24
-Microsoft.VisualStudio.Services.Client with version =15.120.0-preview
-Microsoft.VisualStudio.Services.InteractiveClient with version =15.120.0-preview
-Microsoft.VisualStudio.Services.Release.Client with version =15.120.0-preview
-Newtonsoft.Json with version =8.0.3
-System.IdentityModel.Tokens.Jwt with version =4.0.2.206221351
-WindowsAzure.ServiceBus with version =3.3.2

Below source code permit you to create release definition, we define VssClientCredentials object permit to ensure connection to your VSTS, so you need to register your login and your token.

If you wish to get token, go to security panel of your VSTS, and create basic token with retency of 90 days.

token

We create VssClientCredentials with credentials, for etablish connection with VSTS we define VssConnection object based on credentials.

ReleaseHttpClient is the service permit you to access releases. based on ReleaseHttpClientBase abstract class.

ReleaseHttpClientBase permit you to create, delete, update, modify your definitions.

In this sample we create definition of release.

 VssClientCredentials credentialsDestination = new VssClientCredentials
                            (new VssBasicCredential(ConfigurationManager.AppSettings["loginForMigration"],
                                                            ConfigurationManager.AppSettings["tokenForMigration"]));

            using (VssConnection connectionDestination = new VssConnection(serverUrlDestination, credentialsDestination))
            {
                using (var releaseHttpClient = connectionDestination.GetClient())
                {
                    try
                    {
                        
                        Task releaseDefinitionTask = releaseHttpClient.CreateReleaseDefinitionAsync(releaseDefinition, projectName);
                        releaseDefinitionTask.Wait();
                        
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }

After describing call of CreateReleaseDefinitionAsync, we speak about business rules that definition have to respect.

Constraints on VSTS :

  • Definition must have Id.
  • Definition must have Name unique.
  • On each environment of your release you have to define RetentionPolicy with three properties DaysToKeep, RetainBuild and ReleasesToKeep.
  • We three types of agents, in order to stay coherent with TFS platform, we use AgentDeploymentInput type as agent.

Remarks : In the future evolutions we preconise to use Deployment group, so replace AgentBasedDeployPhase type with MachineGroupBasedDeployPhase type.

 foreach (var environment in definition.Environments)
 {
                environment.RetentionPolicy = new EnvironmentRetentionPolicy
                {
                    DaysToKeep = daysToKeep,
                    RetainBuild = true,
                    ReleasesToKeep = releasesToKeep
                };

                var agentDeploymentInput = new AgentDeploymentInput
                {
                };

                if (agentName == AgentNameDefaultConstant)
                {
                    agentDeploymentInput.QueueId = 1;
                }

                DeployPhase deployPhase = new AgentBasedDeployPhase
                {
                    Name = deployPhaseName,
                    Rank = deployPhaseRank,
                    DeploymentInput = agentDeploymentInput
                };

                environment.DeployPhases.Add(deployPhase);

                // Adjust tasks of step
                if (environment.DeployStep != null &&
                    environment.DeployStep.Tasks.Any())
                {
                    deployPhase.WorkflowTasks = .....;
                }
}

All tasks are contained on DeployPhase object.

GC.Collect