我想使用c#代码在VSTS中创建/触发发行版。我可以使用VSTS REST API @ https://docs.microsoft.com/en-us/rest/api/vsts/release/releases?view=vsts-rest-4.1
创建/触发它但是我想知道是否有任何NuGet软件包可用于使用c#在VSTS中创建/触发/更新发行版?
答案 0 :(得分:2)
实际上,您还可以使用REST API通过C#创建/更新发行版:
下面是一个有关如何通过REST API开始构建的示例,您可以更改参数和REST API以相应地创建发行版:(更多信息,请参见:Accessing TFS/VSTS 2017 programmatically)
var personalaccesstoken = "bXlfc3VwZXJfcGVyc29uYWxfYWNjZXNzX3Rva2Vu";
var base64Token = Convert.ToBase64String(Encoding.ASCII.GetBytes($":{personalaccesstoken}"));
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64Token);
var requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://{your_tfs_server}/DefaultCollection/{project}/_apis/build/builds?api-version=2.0");
requestMessage.Content = new StringContent("{\"definition\": {\"id\":" + definitionId + "},\"sourceBranch\":\"$/BRANCH_NAME\"}", Encoding.UTF8, "application/json");
using (HttpResponseMessage response = client.SendAsync(requestMessage).Result)
{
response.EnsureSuccessStatusCode();
}
}
此外,如果您要使用C#中的Nuget包创建/触发/更新发行版,则可以引用此博客中提到的示例:Create a draft release and start it using the TFS REST API
在此处参考代码:
var serverUrl = new Uri("https://account.visualstudio.com");
var credentials = new VssBasicCredential("", PAT);
var teamProjectName = "[replace with team project name]";
using (var connection = new VssConnection(serverUrl, credentials))
using (var rmClient = connection.GetClient<ReleaseHttpClient2>())
using (var buildClient = connection.GetClient<BuildHttpClient>())
{
// Get the first build definition
var releaseDefinition = rmClient.GetReleaseDefinitionsAsync(teamProjectName, null, ReleaseDefinitionExpands.Artifacts).Result[0];
var primaryArtifact = releaseDefinition.Artifacts.Where(a => a.IsPrimary).SingleOrDefault();
var projectName = primaryArtifact.DefinitionReference["project"].Id;
var buildDefinitionId = Convert.ToInt32(primaryArtifact.DefinitionReference["definition"].Id);
// Get the latest successful build from the project and build definition id
var lastBuild = buildClient.GetBuildsAsync(projectName, new[] { buildDefinitionId }, statusFilter: BuildStatus.Completed).Result
.OrderByDescending(b => b.Id)
.FirstOrDefault();
// Create the draft release, and associate it to the latest corresponding build
var metadata = new ReleaseStartMetadata
{
DefinitionId = releaseDefinition.Id,
IsDraft = true,
Description = "Draft",
Artifacts = new[]
{
new ArtifactMetadata
{
Alias = primaryArtifact.Alias,
InstanceReference = new BuildVersion
{
Id = lastBuild.Id.ToString(),
Name = lastBuild.BuildNumber
}
}
}
};
var release = rmClient.CreateReleaseAsync(metadata, teamProjectName).Result;
// Update the draft release variable
release.Variables["Variable-A"].Value = "Test A";
release.Variables["Variable-B"].Value = "Test B";
release.Environments[0].Variables["Var-Env-A"].Value = "Test C";
release = rmClient.UpdateReleaseAsync(release, teamProjectName, release.Id).Result;
// Activate the release
var updateMetadata = new ReleaseUpdateMetadata { Status = ReleaseStatus.Active, Comment = "Yeah baby, yeah" };
release = rmClient.UpdateReleaseResourceAsync(updateMetadata, teamProjectName, release.Id).Result;
// Trigger the deployment on a specific environment
release.Environments[0].Status = EnvironmentStatus.InProgress;
release = rmClient.UpdateReleaseAsync(release, teamProjectName, release.Id).Result;