microsoft / azure-devops-dotnet-samples

.NET/C# samples for integrating with Azure DevOps Services and Azure DevOps Server
https://docs.microsoft.com/azure/devops/integrate
MIT License
519 stars 511 forks source link

Need sample/help for QueueBuildAsync #297

Closed MisinformedDNA closed 3 years ago

MisinformedDNA commented 3 years ago

I cannot figure out how to queue a build, specifically a YAML pipeline. I always get the error message:

Could not queue the build because there were validation errors or warnings.

There is no further information.

Here's my code:

string collectionUri = $"https://dev.azure.com/{organization}";
var creds = new VssBasicCredential(string.Empty, pat);
var connection = new VssConnection(new Uri(collectionUri), creds);

var projectClient = connection.GetClient<ProjectHttpClient>();
var project = await projectClient.GetProject(projectName);

var buildClient = await connection.GetClientAsync<BuildHttpClient>();
var definition = await buildClient.GetDefinitionAsync(projectName, 1253);

var build = new Build
{
    Definition = definition,
    Project = project,
};

var response = await buildClient.QueueBuildAsync(build, ignoreWarnings: true);

Setup:

image

What am I doing wrong?

MisinformedDNA commented 3 years ago

Sign... the pipeline was only defined on a specific branch. Once I set the branch name, it worked fine.

However, now I'm stuck at trying to set a variable for the build. How can I do that?

icnocop commented 3 years ago

@MisinformedDNA I found this code snippet https://stackoverflow.com/a/52777688/90287

MisinformedDNA commented 3 years ago

Thanks. This is all I ended up needing:

var json = new JObject(new JProperty("pullRequestId", pullRequestId));
build.Parameters = JsonConvert.SerializeObject(json);