Last active
September 12, 2019 11:38
-
-
Save MarcBruins/99c1c7c6d77de0aced1db19056f53cb3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Microsoft.TeamFoundation.Build.WebApi; | |
using Microsoft.TeamFoundation.Core.WebApi; | |
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models; | |
using Microsoft.VisualStudio.Services.Common; | |
using Microsoft.VisualStudio.Services.WebApi; | |
namespace NuKeeperService | |
{ | |
class Program | |
{ | |
private const string ProjectUrl = "https://dev.azure.com/marcbruins1"; | |
private const string ProjectName = "NuKeeperService"; | |
private const string PAT = ""; | |
private const string RepoName = "NuKeeper"; | |
private static BuildHttpClient _buildHttpClient; | |
private static TeamProjectReference _nuKeeperServiceProject; | |
static async Task Main(string[] args) | |
{ | |
var url = new Uri(ProjectUrl); | |
var connection = new VssConnection(url, new VssBasicCredential(string.Empty, PAT)); | |
var projects = await connection.GetClient<ProjectHttpClient>().GetProjects().ConfigureAwait(false); | |
_nuKeeperServiceProject = projects.SingleOrDefault(p => p.Name == ProjectName); | |
_buildHttpClient = connection.GetClient<BuildHttpClient>(); | |
var serviceClient = connection.GetClient<ConnectedServiceHttpClient>(); | |
await CreateConnectedService(serviceClient, _nuKeeperServiceProject).ConfigureAwait(false); | |
//await ReadExistingDefinition().ConfigureAwait(false); | |
//await CreateNewDefinition().ConfigureAwait(false); | |
//await QueueBuild().ConfigureAwait(false); | |
} | |
private static async Task<WebApiConnectedService> CreateConnectedService(ConnectedServiceHttpClient client, TeamProjectReference project) | |
{ | |
var connectedServices = await client.GetConnectedServices(project.Id.ToString()).ConfigureAwait(false); | |
var cred = connectedServices.FirstOrDefault(); | |
await client.CreateConnectedService(_nuKeeperServiceProject.Id.ToString(), | |
new WebApiConnectedServiceDetails() | |
{ | |
ConnectedServiceMetaData = new WebApiConnectedService() | |
{ | |
AuthenticatedBy = new IdentityRef() | |
{ | |
}, | |
Description = "Build cred", | |
FriendlyName = "Build credentials" | |
} | |
}).ConfigureAwait(false); | |
return cred; | |
} | |
private static async Task QueueBuild() | |
{ | |
var availableBuilds = await _buildHttpClient.GetBuildsAsync2(_nuKeeperServiceProject.Id).ConfigureAwait(false); | |
var lastBuild = availableBuilds.Last(); | |
// | |
await _buildHttpClient.QueueBuildAsync(new Build() | |
{ | |
BuildNumber = "43", | |
AgentSpecification = new AgentSpecification(new AgentPoolQueueTarget()), | |
Definition = new BuildDefinition | |
{ | |
AuthoredBy = new IdentityReference() | |
}, | |
Project = _nuKeeperServiceProject | |
}).ConfigureAwait(false); | |
Console.WriteLine($"Trigger build"); | |
} | |
private static async Task ReadExistingDefinition() | |
{ | |
var definitions = | |
await _buildHttpClient.GetFullDefinitionsAsync(project: ProjectName).ConfigureAwait(false); | |
foreach (var definition in definitions) | |
{ | |
Console.WriteLine($" {definition.Id} - {definition.Name}:"); | |
Debugger.Break(); | |
} | |
Console.ReadLine(); | |
} | |
private static async Task CreateNewDefinition() | |
{ | |
var rnd = new Random().Next(0, 5000); | |
var buildDefinition = new BuildDefinition() | |
{ | |
Repository = new BuildRepository() | |
{ | |
DefaultBranch = "master", | |
Id = "MarcBruins/NuKeeper", | |
Url = new Uri("https://github.com/MarcBruins/NuKeeper.git"), | |
Type = "Github", | |
Clean = "false", | |
CheckoutSubmodules = false, | |
Properties = | |
{ | |
new KeyValuePair<string, string>("apiUrl", "https://api.github.com/repos/MarcBruins/NuKeeper"), | |
new KeyValuePair<string, string>("branchesUrl", "https://api.github.com/repos/MarcBruins/NuKeeper/branches"), | |
new KeyValuePair<string, string>("cloneUrl", "https://github.com/MarcBruins/NuKeeper.git"), | |
new KeyValuePair<string, string>("defaultBranch", "master"), | |
new KeyValuePair<string, string>("fullName", "MarcBruins/NuKeeper"), | |
new KeyValuePair<string, string>("hasAdminPermissions", "True"), | |
new KeyValuePair<string, string>("managedUrl", "https://github.com/MarcBruins/NuKeeper"), | |
new KeyValuePair<string, string>("orgName", "MarcBruins"), | |
} | |
}, | |
Queue = new AgentPoolQueue() | |
{ | |
Name = "Hosted VS2017" | |
}, | |
Process = new DesignerProcess | |
{ | |
Phases = | |
{ | |
new Phase | |
{ | |
Name = "Phase 1", | |
RefName = "Phase_1", | |
Condition = "succeeded()", | |
JobAuthorizationScope = BuildAuthorizationScope.Project, | |
Steps = new List<BuildDefinitionStep>() | |
{ | |
new BuildDefinitionStep() | |
{ | |
DisplayName = "NuKeeper", | |
Enabled = true, | |
Inputs = new Dictionary<string, string>() | |
{ | |
{"arguments", "-m 3"} | |
}, | |
TaskDefinition = new TaskDefinitionReference() | |
{ | |
DefinitionType = "task", | |
Id = Guid.Parse("e5a17a66-903f-46a9-a9b9-94241bcedc39"), //NUKEEPER TASK ID | |
VersionSpec = "0.*" | |
} | |
} | |
}, | |
Target = new AgentPoolQueueTarget() | |
} | |
} | |
}, | |
Name = $"{rnd}" | |
}; | |
await _buildHttpClient.CreateDefinitionAsync(buildDefinition, ProjectName); | |
Console.WriteLine($"Created pipeline: {rnd}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment