通过 Azure Devops REST API 运行构建管道时,分支始终默认为 Master

问题描述 投票:0回答:1

我已经设置了一个 powershell 脚本,该脚本通过 REST API 在 Azure Devops 中运行构建管道,这可以工作,但始终将分支指定为主分支,即使我指定了不同的分支也是如此。

我按照以下方式设置了代码(省略了敏感细节):

  $body = @{
        stagesToSkip = @{}
        resources = 
        @{
            repositories = 
            @{
                "self" = @{
                "refName" = '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

    $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

您可以从中看到,在请求正文中,“refname”值设置为分支 test123,这是我创建的测试分支(尽管无论我尝试哪个分支,都会出现相同的问题)。

根据运行此脚本的输出,它将分支设置为 Master 并忽略我指定的内容:

    "_links":  {
                   "self":  {
                                "href":  "https://dev.azure.com/[OrgName]/68babb1f-0ed8-4b26-8f68-a3c492c31279/_apis/pipelines/22/runs/1371"
                            },
                   "web":  {
                               "href":  "https://dev.azure.com/[OrgName]/68babb1f-0ed8-4b26-8f68-a3c492c31279/_build/results?buildId=1371"
                           },
                   "pipeline.web":  {
                                        "href":  "https://dev.azure.com/[OrgName]/68babb1f-0ed8-4b26-8f68-a3c492c31279/_build/definition?definitionId=22"
                                    },
                   "pipeline":  {
                                    "href":  "https://dev.azure.com/[OrgName]/68babb1f-0ed8-4b26-8f68-a3c492c31279/_apis/pipelines/22?revision=152"
                                }
               },
    "variables":  {
                      "variable1":  {
                                                "value":  ""
                                            },
                      "variable2":  {
                                   "value":  ""
                               },
                      "variable3":  {
                                      "value":  "Yes"
                                  },
                      "variable4":  {
                                                "value":  ""
                                            },
                      "variable5":  {
                                            "value":  ""
                                        },
                      "variable6":  {
                                                  "value":  ""
                                              },
                      "variable7":  {
                                            "value":  ""
                                        },
                      "variable8":  {
                                                     "value":  ""
                                                 },
                      "variable9":  {
                                             "value":  ""
                                         },
                      "variable10":  {
                                          "value":  ""
                                      },
                      "variable11":  {
                                      "value":  ""
                                  },
                      "variable12":  {
                                        "value":  ""
                                    },
                      "variable13":  {
                                           "value":  ""
                                       }
                  },
    "templateParameters":  {

                           },
    "pipeline":  {
                     "url":  "https://dev.azure.com/[OrgName]/68babb1f-0ed8-4b26-8f68-a3c492c31279/_apis/pipelines/22?revision=152",
                     "id":  22,
                     "revision":  152,
                     "name":  "Container Build",
                     "folder":  "\\"
                 },
    "state":  "inProgress",
    "createdDate":  "2024-01-08T11:25:06.1776032Z",
    "url":  "https://dev.azure.com/[OrgName]/68babb1f-0ed8-4b26-8f68-a3c492c31279/_apis/pipelines/22/runs/1371",
    "resources":  {
                      "repositories":  {
                                           "self":  "@{repository=; refName=refs/heads/master}"
                                       }
                  },
    "id":  1371,
    "name":  "1371"

我需要能够交换此脚本中的分支,因为这是我所参与的工作的一个主要方面。我如何正确设置它?仅供参考,我使用的是经典的 Azure DevOps 管道,而不是基于 YAML。

我已经尝试了请求正文的各种格式和语法,这没有什么区别。

我希望当我在请求正文中指定特定分支时,它会在构建管道运行时检查该分支,但它始终使用 Master。

powershell azure-devops azure-pipelines branch azure-devops-rest-api
1个回答
0
投票

根据您的描述,您正在使用经典 Azure DevOps 管道。

您当前使用的 Rest API 适用于 YAML Pipeline 更改分支。 运行 - 运行管道

要设置Pipeline Run Source Branch,需要更改为使用Rest API:Builds - Queue

例如:

休息API:

POST https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=7.1-preview.7

请求正文:

{
    "definition":{"id":buildefinitionid},
    "sourceBranch":"refs/heads/branchname",
    "reason":1,
    "demands":[],
    "parameters":"{\"system.debug\":\"false\",\"variablename1\":\"value\",\"variablename2\":\"value\"}"
}

Powershell 示例:

  $body= @'
    {
      
    "definition":{"id":22},
    "sourceBranch":"refs/heads/main",
    "reason":1,
    "demands":[],
    "parameters":"{\"system.debug\":\"false\",\"variablename1\":\"value\",\"variablename2\":\"value\"}"
      
    }  
'@
    $token = "PAT"
    $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Content-Type", "application/json")
    $headers.Add("Authorization", "Basic $token")
    $response = Invoke-RestMethod 'https://dev.azure.com/xx/xx/_apis/build/builds?api-version=7.1-preview.7' -Method 'POST' -Headers $headers -Body $body
    $response | ConvertTo-Json

您可以在请求正文的parameters字段中设置变量。您可以通过 sourceBranch 字段设置 Pipeline Run 源分支。

结果:

© www.soinside.com 2019 - 2024. All rights reserved.