Search code examples
azure-pipelinesazure-pipelines-release-pipelineazure-pipelines-yaml

Azure Pipelines. Artifact trigger filter by name


I have a pipeline which is publishing two artifacts with different names.

- task: PublishPipelineArtifact@1
  inputs:
    targetPath: '$(Build.ArtifactStagingDirectory)\WPF'
    publishLocation: 'pipeline'
    artifact: 'WPF'

Then I have another pipeline which is triggered by the first pipeline. I want to know if it's possible to do not trigger stage1 or stage2 when no artifact with matching name is published. The idea is not trigger pipeline in all situations, only with mathing artifact name is published.

resources:
  pipelines:
  - pipeline: 'resName' 
    source: '\Folder\PipelineName' 
    trigger: true 

stages:
  - stage: stage1

  steps:
  - checkout: none
    clean: true

  - download: resName
    artifact: WPF

- stage: stage1

  steps:
  - checkout: none
    clean: true

  - download: resName
    artifact: Xamarin

Solution

  • I want to know if it's possible to do not trigger stage1 or stage2 when no artifact with matching name is published.

    There is no Predefined variable can show the artifacts name of the Pipeline resource.

    To meet your requirement, you need to use the Rest API: Artifacts - List to Get the Artifacts list of the Pipeline Run and then set the artifacts name as variable to pass the next stages to check if it contains the artifacts name.

    Here is an example:

    resources:
      pipelines:
      - pipeline: 'resName' 
        source: '\Folder\PipelineName' 
        trigger: true 
    
    stages:
    - stage: stagename
      jobs:
      - job: jobname
        steps:
          - task: PowerShell@2
            name:  taskname
            inputs:
              targetType: 'inline'
              
              script: |
                $token = "$(PAT)"
                
                $url="https://dev.azure.com/{orgname}/{projectname}/_apis/build/builds/$(resources.pipeline.resName.runID)/artifacts?api-version=4.1"
                
                $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
                
                $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get  -ContentType application/json
                
                $Artifactslist= ""
                
                
                foreach($Artifacts in $response.value)
                {
                   $Artifactslist = "$($Artifactslist) $($Artifacts.name)"
                
                }
                
                echo $Artifactslist
                
                echo "##vso[task.setvariable variable=Artifactslist;isOutput=true]$Artifactslist"
          
    
    - stage: stage1
      condition: contains(stageDependencies.stagename.outputs['jobname.taskname.Artifactslist'], 'WPF')
      jobs:
        - job: 
          steps:
          xxxx
    
    - stage: stage2
      condition: contains(stageDependencies.stagename.outputs['jobname.taskname.Artifactslist'], 'Xamarin')
      jobs:
        - job: 
          steps:
          xxxx
    

    Result:

    When the Artifacts contains two artifacts, it will run all stages:

    enter image description here

    When the artifacts contains one of the artifacts, it will run the stage based on the artifacts name.

    enter image description here

    For more detailed info, you can refer to the Cross-stage variables