简体   繁体   English

使用Azure Devops API创建生成定义

[英]Create Build Definition using Azure Devops API

We are trying to create build definition by copy another build definition information using Azure Devops Rest API however getting the below error: 我们正在尝试通过使用Azure Devops Rest API复制另一个构建定义信息来创建构建定义,但是出现以下错误:

HttpError BadRequest - Value cannot be null. HttpError BadRequest-值不能为null。 Parameter name: definition.Repository.Mappings.Mapping.ServerPath. 参数名称:definition.Repository.Mappings.Mapping.ServerPath。

Here are the steps we are following 这是我们要遵循的步骤

  1. Get the build information using API - This step is working fine 使用API​​获取构建信息-此步骤工作正常
  2. Modify the name of the build definition 修改构建定义的名称
  3. Create the new build definition by passing the above build definitions request Body 通过传递上述构建定义请求主体来创建新的构建定义

Sample code 样例代码

var buildDefinitionGet = client.GetBuildDefinitionsAsync("XXX.DevOps", "15");

var newBuildDefinition = buildDefinitionGet;
newBuildDefinition.name = "MVC2017-1";

var buildDefinition = await client               
   .CreateBuildDefinitionsAsync("XXX.DevOps", newBuildDefinition)
   .ConfigureAwait(false);

Here is the request body structure: 这是请求主体结构:

public class BuildDefinitionRequestBody
{
    public Process process { get; set; }
    public Repository repository { get; set; }
    public ProcessParameters processParameters { get; set; }
    public List<object> drafts { get; set; }
    public Queue queue { get; set; }
    public string name { get; set; }
    public string type { get; set; }
    public string queueStatus { get; set; }
}

We are using TFVC as source control. 我们正在使用TFVC作为源代码控制。

Are we missing anything? 我们错过了什么吗?

In these scenarios, there are two type error, 在这些情况下,有两种类型错误,

definition.Repository.Mappings.Mapping.ServerPath ” and “definition.Repository.Mappings.Mapping.LocalPath”. definition.Repository.Mappings.Mapping.ServerPath ”和“ definition.Repository.Mappings.Mapping.LocalPath”。

Following situation in your path will cause above error. 路径中的以下情况将导致上述错误。

definition.Repository.Mappings.Mapping.LocalPath : definition.Repository.Mappings.Mapping.LocalPath


  1. unc paths are not allowed 不允许使用unc路径
  2. local mappings are not allowed to be absolute paths or to navigate out of the s dir 本地映射不允许是绝对路径或导航到s目录之外
  3. two mappings should not have the same local path 两个映射不应具有相同的本地路径
  4. Local Path number is 0 or mapping number is 0 本地路径号为0或映射号为0

definition.Repository.Mappings.Mapping.ServerPath : definition.Repository.Mappings.Mapping.ServerPath


  1. no invalid characters allowed 不允许使用无效字符
  2. no empty fields allowed for server path or type 服务器路径或类型不允许使用空字段
  3. two mappings should not have the same server path 两个映射不应具有相同的服务器路径

Since the screenshots doesn't show the whole local path and the server path , please check the paths based on above rules on your side. 由于屏幕快照并未显示完整的本地路径和服务器路径,因此请根据您自己的上述规则检查路径。 And I suggest you copy the Server Path value from the corresponding project's Code -> Files, on the top of the page, which could make sure the server paths are correct. 我建议您从页面顶部的相应项目的“代码”->“文件”中复制“服务器路径”值,以确保服务器路径正确。 For the local paths, I suggest you remove the one by one to make sure which one caused this issue. 对于本地路径,建议您一一删除,以确保引起此问题的是哪一个。

Powershell equivalent code for cloning build. 用于克隆构建的Powershell等效代码。

 $uri = 'https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}' $result = Invoke-RestMethod -Method Get -Uri $uri -UseDefaultCredentials $result.path = '\\NewFolder\\Location' $result.name = "Testing" $body = $result | ConvertTo-Json -Depth 7 Invoke-RestMethod -Method POST -uri 'https://dev.azure.com/{organization}/{project}/_apis/build/definitions?api-version=4.0' -UseDefaultCredentials -Body $body -ContentType 'application/json' 

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM