# The default behaviour of this template is to download artifacts from previous jobs in the current pipeline
# It can be overridden to download any artifact from any pipeline, given the appropriate build/pipeline IDs

parameters:
  - name: os
    type: string
    default: 'ubuntu2204'
  - name: gpuTarget
    type: string
    default: ''
  - name: preTargetFilter
    type: string
    default: ''
  - name: postTargetFilter
    type: string
    default: ''
  # Set buildType to specific to download artifacts from previous builds, useful for saving time when debugging
  - name: buildType
    type: string
    default: current
    values:
      - current
      - specific
  # One of the below params must be set if buildType == specific
  # Set definitionId to the pipeline ID or buildId to the specific build ID
  - name: definitionId
    type: string
    default: 0
  - name: buildId
    type: string
    default: 0

steps:
  - task: DownloadPipelineArtifact@2
    displayName: Download ${{ parameters.preTargetFilter}}*${{ parameters.os }}_${{ parameters.gpuTarget}}*${{ parameters.postTargetFilter}}
    retryCountOnTaskFailure: 3
    inputs:
      ${{ if eq(parameters.buildType, 'specific') }}:
        buildType: specific
        buildVersionToDownload: specific
        project: ROCm-CI
        ${{ if ne(parameters.definitionId, 0) }}:
          definition: ${{ parameters.definitionId }}
        ${{ if ne(parameters.buildId, 0) }}:
          buildId: ${{ parameters.buildId }}
      itemPattern: '**/*${{ parameters.preTargetFilter }}*${{ parameters.os }}_${{ parameters.gpuTarget }}*${{ parameters.postTargetFilter }}*'
      targetPath: $(Pipeline.Workspace)/d
  - task: ExtractFiles@1
    displayName: Extract ${{ parameters.preTargetFilter}}*${{ parameters.os }}_${{ parameters.gpuTarget}}*${{ parameters.postTargetFilter}}
    inputs:
      archiveFilePatterns: '$(Pipeline.Workspace)/d/**/*.tar.gz'
      destinationFolder: '$(Agent.BuildDirectory)/rocm'
      cleanDestinationFolder: false
      overwriteExistingFiles: true
  - task: DeleteFiles@1
    displayName: Clean up ${{ parameters.preTargetFilter}}*${{ parameters.os }}_${{ parameters.gpuTarget}}*${{ parameters.postTargetFilter}}
    inputs:
      SourceFolder: '$(Pipeline.Workspace)/d'
      Contents: '/**/*.tar.xz'
      RemoveDotFiles: true
