azure-devopscontinuous-integrationazure-pipelinesazure-devops-task-groups

Access Azure DevOps Pipeline Artifacts from different organization


I am using the Azure DevOps pipeline task DownloadPipelineArtifact@2 https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/download-pipeline-artifact-v2?view=azure-pipelines

This task will download the artifacts of another pipeline. There's a parameter that allows for downloading artifacts from a pipeline in another project, but no such parameter exists for a pipeline in another organization. Is there a way to download artifacts from a pipeline in another org, assuming we can auth to that other org?


Solution

  • There is no available pipeline task can be used to download build artifacts from another Azure DevOps organization.

    As a workaround, you can try to use the REST API to download the build artifacts from another Azure DevOps organization:

    1. Call the API "Artifacts - Get Artifact" to get the downloadUrl of the specified build artifact.

    2. Then call the downloadUrl to download the artifact as ZIP file.

    3. Extract the ZIP file.


    Below is the steps to configure the pipeline to call the API to download the build artifact:

    1. Generate a PAT (Personal Access Token) in the Azure DevOps organization where you want to download the build artifact from. The PAT should have the "Build (Read)" scope at least.

    2. In the pipeline where you want to download the artifact to, add the PAT generated above as a secret variable.

      enter image description here

    3. In the pipeline, you can use a PowerShell task to call the REST API to download the build artifact.

    steps:
    . . .
    
    - task: PowerShell@2
      displayName: 'Download Build Artifact'
      env:
        MY_PAT: $(myPAT)
      inputs:
        pwsh: true
        targetType: 'inline'
        script: |
          $organization = "xxx"
          $project = "xxx"
          $buildId = 3642
          $artifactName = "drop"
          $uri = "https://dev.azure.com/${organization}/${project}/_apis/build/builds/${buildId}/artifacts?artifactName=${artifactName}&api-version=7.0"
    
          $pat = $env:MY_PAT
          $base64Token = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $pat)))
          $headers = @{Authorization = "Basic $base64Token"}
    
          $downloadUrl = (Invoke-RestMethod -Method GET -Uri $uri -Headers $headers).resource.downloadUrl
          $zipFilePath="$PWD\drop.zip"
          Invoke-RestMethod -Method GET -Uri $downloadUrl -Headers $headers -ContentType "application/zip" -OutFile $zipFilePath
    
          [System.IO.Compression.ZipFile]::ExtractToDirectory( $zipFilePath, "$PWD")
          Remove-Item $zipFilePath