0
votes

I have Azure DevOps pipeline with set of input variables which supposed to be modified during queue time. According to documentation and this post if values of pipeline variables will be changed during pipeline queue, they cannot be defined in the pipeline YAML definition, but using UI Variables panel and Variables tab in Trigger page.

If I am going to trigger my pipeline with Azure DevOps REST API, what is the correct approach to define them or do I need to do this? Should I also use Variables tab to define them in advance and override the values later in the REST request payload?

3
@ShaykiAbramczyk Thanks for pointing, this is useful post on how to invoke pipeline with REST API, but I do not see any clarification on how do I need to define variables in advance before pipeline queue.easkerov
You should define them with "settable at queue time" and in the above answer you can see how to put a value when you queue the build with the rest api.Shayki Abramczyk

3 Answers

1
votes

what is the correct approach to define them or do I need to do this? Should I also use Variables tab to define them in advance and override the values later in the REST request payload?

You could invoke the REST API with queue time variables by parameters directly.

The state in that post:

if values of pipeline variables will be changed during pipeline queue, they cannot be defined in the pipeline YAML definition, but using UI Variables panel and Variables tab in Trigger page.

It means that when you define variable in YMAL file, then you cannot modify it while queuing the pipeline, but if you defined it in the UI, you could modify it with queue time variables

However, we still could run Azure DevOps pipeline with YAML type via REST API with queue time variables.

As test, I create a pipeline with YAML type like following without any predefined variables in YAML or UI:

pool:
  vmImage: 'ubuntu-latest'

trigger: 
 branches:
  include:
    - mster

steps:
- script: echo $(Test)
  displayName: 'Do something'

Then I use the REST API with following request body:

{
    "parameters": "{\"Test\":\"123\"}",

    "definition":  {
                       "id":  66
                   }
}

As rest, the pipeline is triggered and the output is:

enter image description here

Hope this helps.

0
votes

you definitely dont need to define triggers, for variables yes - you need to define them to be able to set them.

0
votes

As of the time of writing, I believe this article in the Microsoft Docs is the most recent on the subject. I did have to scratch my head a bit to make it work, but wound up with this code. My pipeline doesn't use Variables as was the question from the OP, but they work the same as parameters.

public static async Task InitiatePipeline(CancellationToken cancellationToken = default)
{
    using(HttpClient client = new HttpClient())
    {
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        var token = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", AppSettings.DevOpsPAT)));
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", token);

var repoGuid = "Put GUID Here"; // You can get GUID for repo from the URL when you select the rpo of interest under  Repos is Project Settings
var bodyJson = @"{
    ""parameters"": {
        ""parameterName"": ""parameterValue""
    },
    ""variables"": {},
    ""resources"": {
        ""repositories"": {
            ""self"": {
                ""repository"": {
                    ""id"": """ + repoGuid + @""",
                    ""type"": ""azureReposGit""
                },
                ""refName"": ""refs/heads/master""
            }
        }
    }
}";

        var bodyContent = new StringContent(bodyJson, Encoding.UTF8, "application/json");
        var pipeLineId = 61; // Can get this from URL when you open the pipeline of interest in Azure DevOps
        var response = await client.PostAsync($"https://dev.azure.com/ORG_NAME/PROJECT_NAME/_apis/pipelines/{pipeLineId}/runs?api-version=6.0-preview.1", bodyContent, cancellationToken);
        response.EnsureSuccessStatusCode();
    }
}