0

I have two pipelines in Azure Devops. One generates an artifact and the other one runs tests on it. The pipeline that generates the artifact gets triggered whenever a PR is created. The pipeline that runs the tests is only triggered if certain checks are true.

I am trying to programmatically associate the artifact ID from the first pipeline with the second one but don't seem to find a way to do this. the SDK offers BuildHttpClientBase.CreateArtifactAsync() method but you must already have a build ID to do so. I am not sure how to get the ID, since queuing a build with BuildHttpClient.QueueBuildAsync() will fail without this artifact set first.

Sunny-Dee
  • 87
  • 9

1 Answers1

1

First of all, I think you have a misunderstanding about the BuildHttpClientBase.CreateArtifactAsync() method. This method should be used to create an artifact for a build, rather than assigning artifacts to another build. The method provided by the SDK can only trigger the pipeline, and cannot associate the artifact generated by one build pipeline with another build pipeline.

So to achieve this, we need to set the second build pipeline definition and add download pipeline artifact task to the second pipeline to download the artifact generated in the first pipeline.

For example:

1.Publishing artifacts in the first pipeline:

steps:
- task: PublishPipelineArtifact@1
  inputs:
    targetPath: $(System.DefaultWorkingDirectory)/bin/WebApp
    artifactName: WebApp

2.Downloading artifacts in the second pipeline:

steps:
- task: DownloadPipelineArtifact@2
  inputs:
    artifact: WebApp

For details, please refer to this official document.

Hugh Lin
  • 17,829
  • 2
  • 21
  • 25
  • Thanks for taking the time to answer! This is really helpful. The last thing would be to set the version of the artifact when I kick off the second pipeline. I see in the [download task docs](https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/download-pipeline-artifact?view=azure-devops#yaml-snippet) that you can specify `runVersion: specific` and `runId`. I need to be able to set `runId` at the time my build is being kicked off using the SDK. Do you know how to set variables for a build? – Sunny-Dee Oct 29 '20 at 17:22
  • 1
    Sorry for the late response! Do you refer to achieve something like [this](https://i.stack.imgur.com/ClA8j.png)? About defining variables in yaml, please refer to this [document](https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch). – Hugh Lin Nov 02 '20 at 10:02
  • Exactly that, but the variable is set at queue time. I see [here](https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#allow-at-queue-time) how to declare a variable to be settable at queue time, but what I'm wondering is how to programmatically set the final value. I'm using [the client](https://learn.microsoft.com/en-us/dotnet/api/microsoft.teamfoundation.build.webapi.buildhttpclient?view=azure-devops-dotnet) provided by the .NET SDK. – Sunny-Dee Nov 02 '20 at 23:48
  • 1
    You can try the method mentioned in this [ticket](https://developercommunity.visualstudio.com/comments/1238392/view.html):`var json = new { "aaa" = "123", }; build.Parameters = JsonConvert.SerializeObject(json);` – Hugh Lin Nov 03 '20 at 05:58
  • That did it! Thank you! – Sunny-Dee Nov 03 '20 at 06:28