Run Azure DevOps pipeline via REST API with queue time variables

独自空忆成欢 提交于 2021-02-11 13:32:47

问题


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?


回答1:


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:

Hope this helps.




回答2:


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




回答3:


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();
    }
}


来源:https://stackoverflow.com/questions/60150292/run-azure-devops-pipeline-via-rest-api-with-queue-time-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!