【发布时间】:2022-03-24 09:10:11
【问题描述】:
我使用 REST API 在 Azure DevOps 中为我的每个存储库创建 2 个 YAML 管道。
管道可以正常工作,只是它不会在 repo 更改时触发。不知何故,管道未被完全识别为 Azure DevOps。 这意味着即使 REST 创建的管道使用默认的 azure-pipelines.yml 文件,“设置构建”按钮仍会显示在 repo 页面上。
另外,当我去 Pipelines 手动设置额外的管道时,我不能这样做。它想为 azure-pipelines.yml 文件创建一个新管道(即使它已被 REST 创建的管道使用)并且不允许我选择不同的 .yml 文件。
以下是我使用 REST API 创建管道的代码:
string pipelinesURL = $"https://dev.azure.com/{ViewModel.Organization}/{ViewModel.ProjectName}/_apis/pipelines?api-version=6.0-preview.1";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", "", pat))));
string folderToUse = "null";
if (!string.IsNullOrEmpty(folder))
{
folderToUse = "\"" + folder + "\""; //folder + "/"; // @"/" + folder;
}
string pipelineDef =
"{" +
$" \"folder\": {folderToUse}," +
$" \"name\": \"{name}\"," +
" \"configuration\": {" +
" \"type\": \"yaml\"," +
$" \"path\": \"{yamlPath}\"," +
" \"repository\": {" +
$" \"id\": \"{repoId}\"," +
$" \"name\": \"{repoName}\"," +
" \"type\": \"azureReposGit\"" +
" }" +
" }" +
"}";
StringContent stringContent = new StringContent(pipelineDef, Encoding.UTF8, "application/json");
string url = pipelinesURL;
using (HttpResponseMessage response = await client.PostAsync(
url, stringContent))
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
return true;
}
}
我该如何解决这个问题?
【问题讨论】:
标签: azure-devops azure-devops-rest-api azure-pipelines-yaml