skip to Main Content

I am tring to create Azure devops (tfs) Pipeline by rets API as described here: https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/pipelines/create?view=azure-devops-rest-6.0

Request url: https://tfs.xxx/tfs/Projects/xxx/_apis/pipelines?api-version=6.0-preview
Request body:

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

I got error 400 with response:

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

I don’t see any parameter called repositoryName in the documentation.

What am i missing?

Thanks!

2

Answers


  1. Pipeline yaml be located in specific repository, so you must provide the repository id. A simple way to get your repository 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
    

    Then you can pass the repository id into

    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
    

    How to get header

    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
    
    Login or Signup to reply.
  2. It appears the url https://tfs.xxx/tfs/Projects/xxx/_apis/pipelines?api-version=6.0-preview is not correct, at least there’s extra Projects.

    For On-Prem ADO server(tfs), please fix the url with below format:

    https://{instance}/{collection}/{project}/_apis/pipelines?api-version=7.0
    

    The api-version could be different based on different tfs version.

    On my ADO server 2022, below powershell script works. Please replace url and parameters, and try on your side.

    # Define your parameters
    $token = "PAT"
    $repositoryId = "repoid"
    $repositoryName = "reponame"
    
    # Convert PAT token to Base64
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($token)"))
    
    # Define the API URL
    $url = "https://{instance}/{collection}/{project}/_apis/pipelines?api-version=6.0-preview"
    
    # Define the request body
    $body = @{
        "folder" = "null"
        "name" = "test"
        "configuration" = @{
            "type" = "yaml"
            "path" = "build.yml"
            "repository" = @{
                "id" = $repositoryId
                "name" = $repositoryName
                "type" = "azureReposGit"
            }
        }
    } | ConvertTo-Json
    
    # Send the POST request
    $response = Invoke-RestMethod -Uri $url -Method Post -Body $body -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
    
    # Output the response
    $response
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search