Tabrez Shams
Tabrez Shams

Reputation: 133

How to download pipeline artifacts from azure pipeline to local using powershell?

I need to download artifacts from azure pipeline on my local machine. Can anyone help in doing this using powershell script?

Upvotes: 9

Views: 9713

Answers (3)

Marc
Marc

Reputation: 14294

The solutions provided above do not or no longer work - the API seems to have changed. If you use the solutions you get HTML content asking you to sign in.

The new API returns JSON with a downloadUrl:

{
  "id": 105284,
  "name": "Artifact",
  "resource": {
    ...
    "downloadUrl": "https://artprodsu6weu.artifacts.visualstudio.com/....."        
  }
}

So here is some code which works fine:

$BuildId = "154782"
$ArtifactName = "Artifact"
$OutFile = "Artifact.zip"
$OrgName="myorg"
$ProjectName="myproject"

$PAT = "**************"
    
$url = "https://dev.azure.com/$($OrgName)/$($ProjectName)/_apis/build/builds/$($BuildID)/artifacts?artifactName=$($ArtifactName)&api-version=&format=zip"
    
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
    
# Get the download URL
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"}
$downloadUrl = $response.resource.downloadUrl

# Download the artifact
$response = Invoke-WebRequest -Uri $downloadUrl -Headers @{Authorization = "Basic $token"} -OutFile $OutFile

Upvotes: 7

Hugh Lin
Hugh Lin

Reputation: 19451

$token = "xxx"
    
$url="https://dev.azure.com/{OrgName}/{ProjectName}/_apis/build/builds/{BuildID}/artifacts?artifactName={ArtifactName}&api-version=6.1-preview.5&%24format=zip"
    
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
    
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/zip -OutFile "{SomePath}\Response.zip"

Note: Add &%24format=zip after the url and set -ContentType application/zip -OutFile "{SomePath}\Response.zip"

You need to replace token(PAT),OrgName,ProjectName,BuildID,ArtifactName with your own values. And choose one existing path to save the response, such as C:\pub\Response.zip. I have existing path C:\pub, after running the PS script I can get one created Response.zip which contains the artifact I need.

In addition , you can also try to download the build artifact through c# code. For details,please refer to this ticket.

static readonly string TFUrl = "https://dev.azure.com/OrgName/";
static readonly string UserPAT = "PAT";

static void Main(string[] args)
{
    try
    {
        int buildId = xx; // update to an existing build definition id
        string artifactName = "drop"; //default artifact name
    //  string project = "projectName";
        ConnectWithPAT(TFUrl, UserPAT);

        Stream zipStream = BuildClient.GetArtifactContentZipAsync(buildId, artifactName).Result; //get content
        using (FileStream zipFile = new FileStream(@"C:\MySite\test.zip", FileMode.Create))
            zipStream.CopyTo(zipFile);
        Console.WriteLine("Done");
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception: " + ex.Message);
        if (ex.InnerException != null) Console.WriteLine("Detailed Info: " + ex.InnerException.Message);
        Console.WriteLine("Stack:\n" + ex.StackTrace);
    }
}

Upvotes: 4

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41665

You can use the Artifacts Rest API:

$token = "Your PAT"

# Create Authorization header
$headers = @{"Authorization" = "Bearer $token"}

# Create Web client - used to downlaod files
$wc = New-Object System.Net.WebClient
$wc.Headers.Add("Authorization", $headers["Authorization"])

            # Get Build artifact details
            $buildId = "the artifats build id"
            $artifactsUrl = "https://dev.azure.com/{organization}/{project}/_apis/build/builds/$buildId/artifacts?api-version=4.1"
            $buildArtifacts = Invoke-RestMethod -Method Get -Headers $headers -Uri $artifactsUrl

            foreach($buildArtifact in $buildArtifacts.value){
                # Download build artifacts - ZIP files
                $url = $buildArtifact.resource.downloadUrl
                $output = Join-Path $artifactsDir "$($buildArtifact.name).zip"
                $wc.DownloadFile($url, $output)
            }

$wc.Dispose()

Upvotes: 1

Related Questions