You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用C# Azure DevOps SDK替代REST API触发YAML发布流水线?

使用Azure DevOps .NET SDK触发YAML流水线的C#实现

第一步:安装依赖包

首先安装Azure DevOps官方的.NET SDK包:

dotnet add package Microsoft.TeamFoundationServer.Client

第二步:C#实现代码

以下代码完全对应你提供的Postman请求参数,实现触发指定流水线的功能:

using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
using Microsoft.TeamFoundation.Pipeline.WebApi;
using System.Collections.Generic;

class Program
{
    static async System.Threading.Tasks.Task Main(string[] args)
    {
        // 配置Azure DevOps信息
        string organizationUrl = "https://dev.azure.com/Dev";
        string projectName = "modernization";
        int pipelineId = 30;
        string personalAccessToken = "你的PAT令牌"; // 替换为你的个人访问令牌,需具备流水线执行权限

        // 初始化连接
        var credentials = new VssBasicCredential(string.Empty, personalAccessToken);
        using var connection = new VssConnection(new System.Uri(organizationUrl), credentials);
        
        // 获取流水线客户端
        var pipelinesClient = connection.GetClient<PipelinesHttpClient>();

        // 构建流水线运行参数,完全匹配Postman请求体
        var runParams = new RunPipelineParameters
        {
            PreviewRun = false,
            Resources = new RunResources
            {
                Repositories = new Dictionary<string, RepositoryResource>
                {
                    {
                        "self", new RepositoryResource
                        {
                            Repository = new PipelineRepository
                            {
                                Id = "PartsUnlimited",
                                Type = "azureReposGit"
                            },
                            RefName = "refs/heads/feature/branch4",
                            Version = "aaaaabbbbbbe6a1b2d7904b110zzzzzaaa400000"
                        }
                    }
                },
                Pipelines = new Dictionary<string, PipelineResource>
                {
                    {
                        "source_build", new PipelineResource
                        {
                            Pipeline = new PipelineIdentifier
                            {
                                Name = "tfvc-build"
                            },
                            Version = "4972"
                        }
                    }
                }
            },
            StagesToSkip = new List<string> { "Stage_Test1" },
            TemplateParameters = new Dictionary<string, object>
            {
                { "number_A", "2" },
                { "number_B", "4" }
            }
        };

        try
        {
            // 触发流水线运行
            var runResult = await pipelinesClient.RunPipelineAsync(runParams, projectName, pipelineId);
            System.Console.WriteLine($"流水线已触发,运行ID:{runResult.Id},状态:{runResult.State}");
        }
        catch (System.Exception ex)
        {
            System.Console.WriteLine($"触发失败:{ex.Message}");
        }
    }
}

关键说明

  • 个人访问令牌(PAT):需授予Build (Execute)和Pipeline (Read & execute)权限,确保具备流水线触发权限
  • 参数映射:代码中所有属性均严格对应你提供的Postman请求体字段,无需额外调整
  • API版本适配:SDK会自动匹配7.2-preview.1版本的接口能力,无需手动指定API版本

内容的提问来源于stack exchange,提问作者osim_ans

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 10:12:41