通过 REST API 在 Azure DevOps 中创建管道失败 - 值不能为空。参数名称:repositoryName

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

我正在尝试通过 rets API 创建 Azure devops (tfs) 管道,如下所述:https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/pipelines/create?view= azure-devops-rest-6.0

请求网址:https://tfs.xxx/tfs/Projects/xxx/_apis/pipelines?api-version=6.0-preview 请求正文:

{   
    "folder": null,   
    "name": "pipeline-made-by-api",   
    "configuration": {     
        "type": "yaml",     
        "path": "build.yaml",     
        "repository": {       
            "id": "xxx",       
            "name": "xxx-repo",       
            "type": "azureReposGit"     
        }   
    } 
}

我收到错误 400 并回复:

{"$id":"1","innerException":null,"message":"Value cannot be null.\r\nParameter name: repositoryName","typeName":"System.ArgumentNullException, mscorlib","typeKey":"ArgumentNullException","errorCode":0,"eventId":0}

我在文档中没有看到任何名为repositoryName 的参数。

我错过了什么?

谢谢!

azure azure-devops tfs azure-devops-rest-api
1个回答
0
投票

Pipeline yaml 位于特定存储库中,因此您必须提供存储库 ID。获取存储库 ID 的简单方法:

azureDevopsRepositoryGet.ps1

[CmdletBinding()]
param (
    [Parameter(Mandatory)]
    [string]$OrganizationName,
    [Parameter(Mandatory)]
    [string]$ProjectName,
    [Parameter(Mandatory)]
    [string]$RepositoryName,
    [Parameter(Mandatory)]
    [hashtable]$Headers
)

$uri = "https://dev.azure.com/{0}/{1}/_apis/git/repositories/{2}?api-version=7.0" -f $OrganizationName, $ProjectName, $RepositoryName

$response = Invoke-RestMethod -Uri $uri -Method 'Get' -Headers $headers

return $response

然后你可以将存储库ID传递到

azureDevopsPipelineCreate.ps1

[CmdletBinding()]
param (
    [Parameter(Mandatory)]
    [string]$OrganizationName,
    [Parameter(Mandatory)]
    [string]$ProjectName,
    [Parameter(Mandatory)]
    [string]$PipelineName,
    [Parameter(Mandatory)]
    [string]$PipelineYamlFile,
    [Parameter(Mandatory)]
    [string]$RepositoryId,
    [Parameter(Mandatory)]
    [string]$RepositoryName,
    [Parameter(Mandatory)]
    [string]$BranchName,
    [Parameter(Mandatory)]
    [hashtable]$Headers
)

$uri = "https://dev.azure.com/{0}/{1}/_apis/pipelines?api-version=7.0" -f $OrganizationName, $ProjectName

$body = @{
    folder        = "\"
    name          = $PipelineName
    configuration = @{
        type       = "yaml"
        path       = $PipelineYamlFile
        repository = @{
            id   = $RepositoryId
            name = $RepositoryName
            type = "azureReposGit"
        }
    }
}| ConvertTo-Json -Depth 5

$response = Invoke-RestMethod -Uri $uri -Method 'Post' -ContentType 'application/json' -Headers $headers -Body $body

return $response

如何获取标题

azureDevopsHearderGet.ps1

[CmdletBinding()]
param (
    [Parameter()]
    [string]$UserName,
    [string]$PersonalAccessToken
)

$basicAuth = ("{0}:{1}" -f $UserName, $PersonalAccessToken)
$basicAuth = [System.Text.Encoding]::UTF8.GetBytes($basicAuth)
$basicAuth = [System.Convert]::ToBase64String($basicAuth)
$headers = @{Authorization = ("Basic {0}" -f $basicAuth) }
return $headers
© www.soinside.com 2019 - 2024. All rights reserved.