VSTS 发布:使用 c# NuGet 包创建发布

Posted

技术标签:

【中文标题】VSTS 发布:使用 c# NuGet 包创建发布【英文标题】:VSTS Release : Create Release using c# NuGet package 【发布时间】:2018-07-19 19:30:11 【问题描述】:

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

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

【问题讨论】:

您的意思是使用安装了 Nuget 包 Microsoft.TeamFoundationServer.ExtendedClient 的 C# 创建/触发/更新版本吗? 【参考方案1】:

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

【讨论】:

嗨,安迪,非常感谢您的帮助!

以上是关于VSTS 发布:使用 c# NuGet 包创建发布的主要内容,如果未能解决你的问题,请参考以下文章

在 VSTS 任务中下载 NuGet 包

如何为NuGet创建手动VSTS版本?

在VSTS上配置Nuget包以进行预发布

Nuget 包 - 提要 (VSTS):尝试添加源时抛出异常“System.AggregateException”

需要自定义 nuget 包帮助

如何使用使用不同选项构建的 C# 和 C++ 库创建 Nuget 包?