We are trying to create build definition by copy another build definition information using Azure Devops Rest API however getting the below error:
HttpError BadRequest - Value cannot be null. Parameter name: definition.Repository.Mappings.Mapping.ServerPath.
Here are the steps we are following
- Get the build information using API - This step is working fine
- Modify the name of the build definition
- 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.
Are we missing anything?
In these scenarios, there are two type error,
“definition.Repository.Mappings.Mapping.ServerPath” and “definition.Repository.Mappings.Mapping.LocalPath”.
Following situation in your path will cause above error.
definition.Repository.Mappings.Mapping.LocalPath:
- unc paths are not allowed
- local mappings are not allowed to be absolute paths or to navigate out of the s dir
- two mappings should not have the same local path
- Local Path number is 0 or mapping number is 0
definition.Repository.Mappings.Mapping.ServerPath:
- no invalid characters allowed
- no empty fields allowed for server path or type
- 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.
$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.
来源:https://stackoverflow.com/questions/54903510/create-build-definition-using-azure-devops-api