3

I've been trying to run a pipeline for a particular branch of the repository I'm using.

In the UI, there is a convenient option, but I don't understand what to try in the request.

enter image description here

No matter what I do I always run from master.

How do I change that? I tried filling out the repository parameters but to no avail: https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/runs/run%20pipeline?view=azure-devops-rest-6.0#repositoryresourceparameters

Here is an example request:

curl --location --request POST 'https://dev.azure.com/<redacted>/<redacted>/_apis/pipelines/<redacted>/runs?api-version=6.0-preview.1' \
--header 'Authorization: Basic <redacted>' \
--header 'Content-Type: application/json' \
--header 'Cookie: VstsSession=<redacted>' \
--data-raw '{   
    "previewRun": true,
    "resources": {
        "repositories": {
            "refName": "refs/heads/<redacted>"
        }
    },
    "runParameters":
    {
        "namespace" : "<redacted>",
        "image" : "<redacted>",
        "tag" : "<redacted>",
        "package" : "<redacted>",
        "version" : "8.4.4"
    }
}'
Dimitar
  • 127
  • 1
  • 3
  • 8

2 Answers2

4

From your screenshot, it seems that you are using the YAML pipeline.

I have tested your example , and the root cause of this issue is that the request body(data-raw) has some issues.

You could try my sample

curl --location --request POST 'https://dev.azure.com/<redacted>/<redacted>/_apis/pipelines/<redacted>/runs?api-version=6.0-preview.1' \
--header 'Authorization: Basic <redacted>' \
--header 'Content-Type: application/json' \
--header 'Cookie: VstsSession=<redacted>' \
--data-raw '{   
"stagesToSkip":[],
    "resources":
    {
        "repositories":
        {
            "self":{"refName":"refs/heads/{Branchname}"}
            }

    },
    "templateParameters":
    {
        "namespace":"{value}",
        "image":"{value}",
        "tag":"{value}",
        "package":"{value}",
        "version":"{value}"
    },
    "variables":{}
}'

Result:

enter image description here

Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • Its unclear if the curly braces in the `templateParameters` are required per syntax - if not could you use `< >` like OP did? – Lars Pellarin Feb 28 '22 at 08:23
1

For Http Request (you can test it with Postman)

1. Get pipeline api url like this https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?api-version=7.1-preview.1

Source: https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/runs/run-pipeline?view=azure-devops-rest-7.1

Fill in your organization, project and pipelineId. (all of this can be found in the link when you open your pipeline definition in Azure DevOps)

2. Add Basic Auth headers

key: Authorization, value:"Basic [your azure Person Access Token from Azure Dev Ops with Access to Pipeline goes here]"

Should look like this "Basic OndzNWdz43FKfjdi98hjKDJFH8kkg9854HJKHF9D8RFEHui4387lkNXE="

And set content type to application/json

like this

key: Content-Type, value: application/json

3. Put this JSON Into Raw Body

{
   "templateParameters":{
      "inputName":"johnsmith"
   },
   "resources":{
      "repositories":{
         "self":{
            "refName":"refs/heads/feature/#JIRATask01_Blabla"
         }
      }
   },
   "variables":{
      
   }
}

Replace refName value with branch you want to run the pipeline for.

If you have multiple repo pipeline look into this topic (but i haven't tested that solution): Azure DevOps API to Trigger Multi Repo changing branches

pawciu
  • 855
  • 9
  • 15