# compress build products into tarball
# delete build products after compression
# publish can be toggled off for jobs that produce multiple tarballs
# for those cases, only publish the last call which puts all the tarballs in one container folder
parameters:
- name: componentName
  type: string
  default: $(Build.DefinitionName)
- name: gpuTarget
  type: string
  default: ''
- name: artifactName
  type: string
  default: drop
- name: publish
  type: boolean
  default: true
- name: os
  type: string
  default: 'ubuntu2204'

steps:
- task: ArchiveFiles@2
  displayName: '${{ parameters.artifactName }} Compress'
  inputs:
    includeRootFolder: false
    archiveType: 'tar'
    tarCompression: 'gz'
    archiveFile: '$(Build.ArtifactStagingDirectory)/${{ parameters.componentName }}_$(Build.BuildId)_$(Build.BuildNumber)_${{ parameters.os }}_${{ parameters.gpuTarget }}_${{ parameters.artifactName }}_$(System.JobAttempt).tar.gz'
- task: DeleteFiles@1
  displayName: 'Cleanup Staging Area'
  inputs:
    SourceFolder: '$(Build.BinariesDirectory)'
    Contents: '/**/*'
    RemoveDotFiles: true
- task: Bash@3
  displayName: Save pipeline artifact file name
  inputs:
    workingDirectory: $(Pipeline.Workspace)
    targetType: inline
    script: echo "${{ parameters.componentName }}_$(Build.BuildId)_$(Build.BuildNumber)_${{ parameters.os }}_${{ parameters.gpuTarget }}_${{ parameters.artifactName }}_$(System.JobAttempt).tar.gz" >> pipelineArtifacts.txt
# then publish it
- ${{ if parameters.publish }}:
  - task: PublishPipelineArtifact@1
    condition: always()
    displayName: '${{ parameters.artifactName }} Publish'
    retryCountOnTaskFailure: 3
    inputs:
      # if this artifact name is changed, please also update $ARTIFACT_URL inside miopen-get-ck-build.yml
      artifactName: $(Agent.JobName)_$(System.JobAttempt)
      targetPath: '$(Build.ArtifactStagingDirectory)'
