azure-devops

How do I run an Azure DevOps pipeline using the Azure DevOps NuGet and at the same time specify resources?


I'm building upon the question asked in "How do I queue a build and only run specific stages with the azure devops nuget?", but I have a need for extending it further, to specify some resources.

In my pipeline YAML, I have defined a "pipeline" resource, that is ultimately used to download a build artifact.

I can use the same PipelinesHttpClient mentioned in the referenced question to retrieve a pipeline run that was performed manually (note: important to use the client from the "Microsoft.Azure.Pipelines.WebApi" namespace, rather than the one in the older "Microsoft.TeamFoundation.Pipelines.WebApi" namespace).

Through that instance I see that it has a Resources property that further has a Repositories property, with an item named "self", containing an instance of "Microsoft.Azure.Pipelines.WebApi.RepositoryResource".

The problem comes when trying to replicate this instance in code. I ultimately get stuck on trying to instantiate a RunResourcesParameters instance I need for assigning to a RunPipelineParameters instance's Resources property.

If I just instantiate a "vanilla" RunPipelineParameters instance, I end up with its Resources property being null: Vanilla RunPipelineParameters instance

I see that its type is RunResourcesParameters, so tried instantiating that. However, it won't let me do that, due to its protection level:

var runResourcesParameters = new RunResourcesParameters(); RunResourcesParameters ctor access error

It has an other ctor as well, but the same goes for that:

var runResourcesParameters2 = new RunResourcesParameters((ISecuredObject)(new object())); RunResourcesParameters ctor access error v2

It's defined like this: RunResourcesParameters type and further like this:

BaseSecuredObject

The same problem of course occurs if I try object initialization on the RunPipelineParameters instance:

var runParameters = new RunPipelineParameters{Resources = }; RunPipelineParameters object init

var runParameters = new RunPipelineParameters{Resources = new RunResourcesParameters()}; RunPipelineParameters object init v2

What am I doing wrong?

I've looked at the ".NET client samples", but it doesn't contain anything for PipelinesHttpClient. I note that the repo has been set in read-only mode back in 2021.

The NuGet page for the "Microsoft.TeamFoundationServer.Client" NuGet package shows that there is a brand new 19.x series package in preview these days, so possibly the samples will be revived in combination with that?


Solution

  • To solve this issue, we can use the following code:

        string json = @"{ 'Builds': {}, 'Containers': {}, 'Repositories': {}, 'Pipelines': {}, 'Packages': {} }";
        RunResourcesParameters test = JsonConvert.DeserializeObject<RunResourcesParameters>(json);
    
        test.Pipelines.Add(name, PipelineResource);
    
        parameters.Resources = test;
    

    Full code sample:

    using System;
    
    using System.IO;
    
    using System.Runtime.Serialization;
    
    using Microsoft.Azure.Pipelines.WebApi;
    
    using Microsoft.TeamFoundation.Build.WebApi;
    
    using Microsoft.TeamFoundation.Core.WebApi;
    
    using Microsoft.TeamFoundation.SourceControl.WebApi;
    
    using Microsoft.VisualStudio.Services.Common;
    
    using Microsoft.VisualStudio.Services.WebApi;
    
    using Newtonsoft.Json;
    
    using Newtonsoft.Json.Linq;
    namespace ConsoleApp5
    
    {
    
        class Program
    
        {
    
    
            static async Task Main(string[] args)
    
            {
    
    
                var vssConnection = new VssConnection(new Uri("https://dev.azure.com/orgname"), new VssBasicCredential(string.Empty, "PAT"));
    
                var name = "pipelineresourcename";
    
                var pipelineClient = new PipelinesHttpClient(vssConnection.Uri, vssConnection.Credentials);
    
                var PipelineResource = new PipelineResourceParameters();
    
          
                PipelineResource.Version = "Pipelineresourceversion";
    
                var parameters = new RunPipelineParameters();
    
    
    
     
    
                string json = @"{ 'Builds': {}, 'Containers': {}, 'Repositories': {}, 'Pipelines': {}, 'Packages': {} }";
                RunResourcesParameters test = JsonConvert.DeserializeObject<RunResourcesParameters>(json);
    
                test.Pipelines.Add(name, PipelineResource);
    
                parameters.Resources = test;
    
    
    
               
    
                var run = await pipelineClient.RunPipelineAsync(parameters, "projectid", pipelineid);
    
            }
    
        }
    
    }
    

    When you run the c# sample, the pipeline resource info will be passed.

    enter image description here