Search code examples
azure-pipelinesazure-sdk-.netazure-pipelines-yaml

Microsoft.TeamFoundation.Build.WebApi associate an artifact with a build before queuing it


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.


Solution

  • 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.