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

VSTS Release : Create Release using c# NuGet package

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

但是我想知道是否有任何 NuGet 包可用于 creating/triggering/updating 使用 c# 在 VSTS 中发布?

其实你也可以使用REST API来create/update使用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();
    }
} 

此外,如果你的意思是creating/triggering/updating使用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;