Yann.T
Yann.T

Reputation: 53

Azure DevOps API - Create a pipeline from a YAML file located in a specific branch source

(this is my first question, I gonna try to be understandable)

Objective : From a Powershell script create a new Pipeline from a YAML file (located on a specific branch)

According to the documentation and my own knowledge I got this JSON to create my own pipeline :

$pipelineJSON = @{
    configuration = @{
        variables = @{
            example = @{
                value =  "to be defined"
            }
        }
        path = "azure-pipelines.yml"
        repository = @{
            id = "myRepoId"
            name = "myRepoName"
            type = "azureReposGit"
        }
        type = "yaml"
    }
    name = "pipeline-test"
    folder= "\\"
} | ConvertTo-Json
$request = 'https://dev.azure.com/' + $organization + '/' + $projectName + '/_apis/pipelines?api-version=6.0-preview.1'
$responseCreatePipeline = Invoke-RestMethod $request -Method 'POST' -Headers $headers -Body $pipelineJSON -ContentType "application/json"

With this code above I can create a pipeline but only from a YAML which is located on the master branch but in my case I want to create this pipeline from a YAML file located in a different branch.

I guess we should be able to add a field in the JSON to specify it but I did not find anything.

Does anyone know how to do it ?

Upvotes: 3

Views: 1594

Answers (1)

Bright Ran-MSFT
Bright Ran-MSFT

Reputation: 13444

When using the endpoint "Pipelines - Create" to create a YAML pipeline for a repository, if the specified path of the YAML file is existing on the default branch, by default the endpoint will use existing YAML file from the default branch. And currently we have not any available option on this endpoint to allow specifying the existing YAML files from other branches. You need to manually switch the branch and YAML file after creating and saving the new YAML pipeline.

However, as a workaround, maybe you can try the Azure CLI "az pipelines create". With this command, you can specify the branch via the parameter '--branch'.

az pipelines create --name
                    [--branch]
                    [--description]
                    [--detect {false, true}]
                    [--folder-path]
                    [--org]
                    [--project]
                    [--queue-id]
                    [--repository]
                    [--repository-type {github, tfsgit}]
                    [--service-connection]
                    [--skip-first-run {false, true}]
                    [--subscription]
                    [--yaml-path]

Upvotes: 1

Related Questions