运行 Azure DevOps 管道通过 REST API 和队列时间变量
Run Azure DevOps pipeline via REST API with queue time variables
我有 Azure DevOps 管道,其中包含一组应该在排队期间修改的输入变量。根据 documentation and 如果管道变量的值将在管道队列期间更改,则不能在管道 YAML 定义中定义它们,但使用 UI Variables 面板和 触发器 页面中的变量 选项卡。
如果我要使用 Azure DevOps REST API 触发我的管道,定义它们的正确方法是什么,或者我是否需要这样做?我是否还应该使用 Variables 选项卡来提前定义它们并稍后在 REST 请求负载中覆盖这些值?
您绝对不需要定义触发器,对于变量是的 - 您需要定义它们才能设置它们。
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?
您可以通过 parameters
直接调用带有队列时间变量的 REST API。
中的状态:
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.
这意味着当你在YMAL文件中定义变量时,你不能在排队管道时修改它,但是如果你在UI中定义它,你可以用队列时间变量修改它
但是,我们仍然可以 运行 带有 YAML 类型的 Azure DevOps 管道通过 REST API 和队列时间变量。
作为测试,我创建了一个 YAML 类型的管道,如下所示,YAML 中没有任何预定义变量或 UI:
pool:
vmImage: 'ubuntu-latest'
trigger:
branches:
include:
- mster
steps:
- script: echo $(Test)
displayName: 'Do something'
然后我将 REST API 与以下请求正文一起使用:
{
"parameters": "{\"Test\":\"123\"}",
"definition": {
"id": 66
}
}
作为休息,管道被触发,输出为:
希望对您有所帮助。
截至撰写本文时,我相信 Microsoft 文档中的 this article 是关于该主题的最新信息。我确实需要挠头才能让它工作,但最终还是得到了这段代码。我的管道不像 OP 中的问题那样使用变量,但它们的工作方式与参数相同。
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();
}
}
我有 Azure DevOps 管道,其中包含一组应该在排队期间修改的输入变量。根据 documentation and
如果我要使用 Azure DevOps REST API 触发我的管道,定义它们的正确方法是什么,或者我是否需要这样做?我是否还应该使用 Variables 选项卡来提前定义它们并稍后在 REST 请求负载中覆盖这些值?
您绝对不需要定义触发器,对于变量是的 - 您需要定义它们才能设置它们。
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?
您可以通过 parameters
直接调用带有队列时间变量的 REST API。
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.
这意味着当你在YMAL文件中定义变量时,你不能在排队管道时修改它,但是如果你在UI中定义它,你可以用队列时间变量修改它
但是,我们仍然可以 运行 带有 YAML 类型的 Azure DevOps 管道通过 REST API 和队列时间变量。
作为测试,我创建了一个 YAML 类型的管道,如下所示,YAML 中没有任何预定义变量或 UI:
pool:
vmImage: 'ubuntu-latest'
trigger:
branches:
include:
- mster
steps:
- script: echo $(Test)
displayName: 'Do something'
然后我将 REST API 与以下请求正文一起使用:
{
"parameters": "{\"Test\":\"123\"}",
"definition": {
"id": 66
}
}
作为休息,管道被触发,输出为:
希望对您有所帮助。
截至撰写本文时,我相信 Microsoft 文档中的 this article 是关于该主题的最新信息。我确实需要挠头才能让它工作,但最终还是得到了这段代码。我的管道不像 OP 中的问题那样使用变量,但它们的工作方式与参数相同。
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();
}
}