azure-devopsazure-pipelinesazure-pipelines-yamlazure-yaml-pipelines

yaml pipeline: How to use a build artifact generated in a previous build


Using Azure Devops Server 2022, I have defined a pipeline that build and publish an artifact. I tried to use the PublishPipelineArtifact@1 task but it doesn't work. I got the following error message:

The pipeline artifact task is not supported locally. Use the build artifact task instead

So I use the PublishBuildArtifacts@1 task to publish the artifact.
I have created another pipeline to deploy an artifact previously published.

resources:
  pipelines:
  - pipeline: mypipeline-resource
    source: 'mypipeline - CI'
    trigger: none

As a pipeline is defined as a resource in my yaml file, when I run the pipeline, I can select a "pipeline run" from the "Resources" selector. That's what I've done.
How can I access and use the artifact that is part of the pipeline run that I have selected from my list? Do I have to call a download task?
I tried to use several predefined variables ($(Build.ArtifactStagingDirectory), $(System.ArtifactsDirectory),$(Build.BuildId)), but I didn't succeed...
An example would be appreciated


Solution

  • The pipeline artifact task is not supported locally. Use the build artifact task instead

    For this error, you can refer to this doc: PublishPipelineArtifact@1 - Publish Pipeline Artifacts v1 task

    Publish Pipeline Artifacts is not supported in on-premises. Please use Publish Build Artifacts if you're using Azure DevOps Server or TFS 2018.

    When we use Azure DevOps Server 2022, we need to use the Publish Build Artifacts task.

    How can I access and use the artifact that is part of the pipeline run that I have selected from my list? Do I have to call a download task?

    Based on your requirement, you need to download the artifacts selected in the Pipeline Run -> Resources selector.

    To meet your requirement, you need to add steps: - download: PipelineAlias in the Yaml Code to download the artifacts from the Pipeline resources.

    Here is an example:

    resources:
      pipelines:
      - pipeline: mypipeline-resource
        source: 'mypipeline - CI'
        trigger: none
    
    steps:
    - download: mypipeline-resource
      
    

    For more detailed info, you can refer to this doc: Download Pipeline Resource Artifacts.

    Regular job artifacts aren't automatically downloaded. Use download explicitly when needed.

    Artifacts from the pipeline resource are downloaded to the $(PIPELINE.WORKSPACE)/<pipeline-identifier>/<artifact-identifier> folder.

    For example: $(PIPELINE.WORKSPACE)/mypipeline-resource/artifatname

    Update:

    How can I retrieve the artifact name? I have to make actions according to its name

    I am afraid that there is no out-of-box variable can directly get the artifact name.

    To meet your requirement, you can use script to get the artifact folder name and set the pipeline variable.

    Here is an example:

    resources:
      pipelines:
      - pipeline: mypipeline-resource
        source: 'mypipeline - CI'
        trigger: none
    
    steps:
    - download: mypipeline-resource
    - powershell: |
       $artifactname = (Get-ChildItem "$(PIPELINE.WORKSPACE)/mypipeline-resource" -directory  | Select Name).name 
       echo $artifactname
       echo "##vso[task.setvariable variable=artifactname;]$artifactname"
    - powershell: echo $(artifactname)
      condition: eq(variables['artifactname'],'expectedname')