gpt4 book ai didi

c# - VSTS 版本 : Create Release using c# NuGet package

转载 作者:行者123 更新时间:2023-12-02 20:20:52 33 4
gpt4 key购买 nike

我想使用 C# 代码在 VSTS 中创建/触发发布。我可以使用 VSTS REST API @ https://learn.microsoft.com/en-us/rest/api/vsts/release/releases?view=vsts-rest-4.1 创建/触发它

但是我想知道是否有任何 NuGet 包可用于使用 c# 在 VSTS 中创建/触发/更新版本?

最佳答案

其实你也可以使用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;

关于c# - VSTS 版本 : Create Release using c# NuGet package,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51430475/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com