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

通过Azure DevOps REST API触发构建流水线时分支始终默认Master

解决Azure DevOps经典流水线API触发时分支不生效的问题

你的问题出在请求体参数格式不对:你用的是YAML流水线指定分支的参数结构,但经典流水线的API触发逻辑不一样,不识别resources.repositories.self.refName这个参数。

修正后的脚本

把请求体里的resources部分替换为直接添加sourceBranch字段,修改后的$body定义如下:

$body = @{
    stagesToSkip = @{}
    sourceBranch = 'refs/heads/test123' # 这里指定目标分支
    templateParameters = @{}
    variables = @{
        'variable1' = @{ value = $textBox1.Text }
        'variable2' = @{ value = $textBox2.Text }
        'variable3' = @{ value = $textBox3.Text }
        'variable4' = @{ value = $textBox4.Text }
        'variable5' = @{ value = $textBox5.Text }
        'variable6' = @{ value = $textBox6.Text }
        'variable7' = @{ value = $textBox7.Text }
        'variable8' = @{ value = $textBox8.Text }
        'variable9' = @{ value = $textBox9.Text }
        'variable10' = @{ value = $textBox10.Text }
        'variable11' = @{ value = $textBox11.Text }
        'variable12' = @{ value = $textBox12.Text }
        'variable13' = @{ value = $textBox13.Text }
    }
} | ConvertTo-Json -Depth 10 # 避免嵌套对象序列化时被截断

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization", "Basic [Omitted PAT]")
$response = Invoke-RestMethod 'https://dev.azure.com/[OrgName]/[ProjectName]/_apis/pipelines/22/runs?api-version=7.0' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json

关键说明

  1. 经典流水线的分支参数:经典流水线触发构建时,必须用sourceBranch直接指定分支的完整引用(格式为refs/heads/[分支名]),而不是YAML流水线用的resources嵌套结构。
  2. 添加-Depth参数:ConvertTo-Json默认深度是2,你的变量结构嵌套较深,需要指定足够的深度(比如10),避免JSON序列化时丢失内容。

执行修改后的脚本后,构建会正确检出你指定的test123分支。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 16:13:25