75

I have a .NET C# application and I would like to download the latest artifacts generated by the latest build of this public Azure DevOps project:

https://dev.azure.com/LumiaWoA/Boot%20Shim/_build?definitionId=3

How does one interface with Azure DevOps to download the artifacts for such a task?

I have in mind to use an HttpClient to download the zipped artifacts using some special URL.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
SuperJMN
  • 13,110
  • 16
  • 86
  • 185
  • related https://stackoverflow.com/questions/61913606/how-do-you-download-a-build-artifact-using-azure-devops-cli – BozoJoe Jan 20 '22 at 17:37

7 Answers7

101

In the Pipelines when one selects a successful run and drills down to Agent job X and selects that agent, it can be found in the log information of that agent.

In the agent Job Information, one can get a link to the download location where items can be downloaded thanks to the X artifact produced link:

enter image description here

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
31

You would need to use Azure DevOps REST Api for that. This rest call retrieves artifacts from the build:

GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/artifacts?artifactName={artifactName}&api-version=5.0

this REST call would give you latest build:

GET https://dev.azure.com/{organization}/{project}/_apis/build/latest/{definition}?branchName={branchName}&api-version=5.0-preview.1

Here's an article (with some samples) talking about how you would authenticate to the API. As from the code perspective, its a simple HTTP request, so should be fairly easy.

Johannes Schweer
  • 229
  • 1
  • 2
  • 19
4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • Would you please tell me how to get the latest build? – SuperJMN Jan 27 '19 at 18:05
  • Also, is there a client library to do this? https://learn.microsoft.com/en-us/azure/devops/integrate/concepts/dotnet-client-libraries?view=azdevops – SuperJMN Jan 27 '19 at 18:05
  • 2
    second rest call retrieves latest build. yeah, I assume one of those client libraries will help you with that, sorry, I'm not really a c# developer. I've been using plain HTTP in powershell to talk Azure Devops. works ok. – 4c74356b41 Jan 27 '19 at 18:06
  • Sorry to bother! I almost have it. However, I'm having problems with the second call. I'm using this https://dev.azure.com/LumiaWOA/Boot%20Shim/_apis/build/latest/PreLoader%20Build?branchName=msm8994&api-version=5.0-preview.1 I hope you can tell me what's wrong. – SuperJMN Jan 27 '19 at 18:56
  • Excuse, the URL I'm trying is this. https://dev.azure.com/LumiaWOA/Boot%20Shim/_apis/build/latest/UEFI%20PreLoader Build?branchName=msm8994-pcie&api-version=5.0-preview.1. It says: Cannot serialize type Microsoft.TeamFoundation.Build.WebApi.Build, because the security requirements are not met – SuperJMN Jan 27 '19 at 19:03
  • I think you need to start another question for this one, but error suggests your auth is wrong – 4c74356b41 Jan 27 '19 at 19:11
  • Posted: https://stackoverflow.com/questions/54391933/impossible-to-get-the-latest-build-using-azure-devops-api – SuperJMN Jan 27 '19 at 19:15
  • this will not work if one has to download a file in nested/ folder structure of the build artifacts – leocrimson Dec 16 '19 at 19:28
  • How to get the definition param for the same? – Ashutosh gupta Jun 05 '20 at 12:05
  • @4c74356b41 Could you please help for this [question](https://stackoverflow.com/questions/63407273/download-a-file-from-azure-devops-server-writes-wrong-data-to-the-file) Great help is appreciated in advance –  Aug 14 '20 at 09:00
17

Here are the steps to download latest artifacts of DevOps build using Azure DevOps UI.

  1. Click on build logs.
  2. Next, click on summary option of the build.
  3. After step 2, on top right side of UI, Artifacts option would be visible, so click on that.
  4. Next, select drop to download the ZIP of the latest artifacts of the build.

Using the beta UI as of 2019-11-09, the steps are as follows.

  1. Click on the completed build (not the whole pipeline) and you will be taken to the Summary tab on the Build page.
  2. In the top box, to the extreme right, click the link under the Artifacts column. It’s an icon of a box with text like 1 published. You will then be taken to the Artifacts page.
  3. The page displays all the available artifacts. Click the download icon to download any of the artifacts.
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
anil
  • 189
  • 1
  • 2
  • We all know Azure DevOps is a fast-moving beast. Looks like the new UI-changes remove the option for downloading artifacts. Or they simply mis-placed the option somewhere I cannot find it. – Jari Turkia Aug 22 '19 at 10:07
  • Yes, I finally found the option to download a given artifact from sprint 156 update. – Jari Turkia Aug 22 '19 at 12:40
  • Thanks! Yes I found it under the build pipeline summary tab at " Build artifacts published " . – Hitesh Patel Oct 09 '19 at 02:31
  • 2
    For those of you who are going slowly insane because you can't find the "Artifacts" dropdown at top right, it may be because the build artifacts have been deleted. I was trying to download a two day-old build but couldn't see the "Artifacts" dropdown anywhere. It turned out the _**Project Settings > Pipelines > Settings > Retention policy > Days to keep artifacts and attachments**_ was set to 1 so the artifacts had been deleted already. – Simon Elms Dec 16 '19 at 03:12
15

Pick your pipeline

AzureDevops pipeline selection

Select your run.

AzureDevops pipeline run

Now look for Published. Click it. If something is there, you should be able to download it.

enter image description here

VivekDev
  • 20,868
  • 27
  • 132
  • 202
  • The screenshots do not display anymore. However, the remaining skeleton of information was enough for me to locate and download the artifact. – quat May 04 '23 at 19:53
4

Releases > Select a Release

enter image description here

Click the Artifact link (in blue eg: 20191125.1)

The new screen is the log. In the top right hand side, click the blue [Artifacts] button.

Choose Drop, expand the tree, and right click to download the file you want (probably the zip of the build)

Neil Thompson
  • 6,356
  • 2
  • 30
  • 53
3

This is how I got it to work using PowerShell.

$MyPat = '********************************'
$B64Pat = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$MyPat"))
$h = @{'Authorization' = 'Basic ' + $B64Pat}
$response = Invoke-WebRequest -Uri "https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/artifacts?artifactName={artifactName}&api-version=4.1" -Method 'GET' -Headers $h
$response_json = ($response.Content | ConvertFrom-Json)
$response_artifact = Invoke-WebRequest -Uri $response_json.resource.downloadUrl -Method 'GET' -Headers $h
Louis Cribbins
  • 121
  • 1
  • 2
  • Hey Louis, in your example you have to insert the buildId. Is this also possible with the latest successful build? E.g. https://dev.azure.com/{organization}/{project}/_apis/build/latest/{definition}.... – etalon11 Aug 04 '21 at 15:35
  • Your best bet in that case is to get the latest buildId from here: GET https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=4.1 – Louis Cribbins Aug 05 '21 at 20:15
2

The following works for me in PowerShell.

# Script to download the latest artifact from a DevOps build pipeline

# Configuration
$Company = '*********'
$Project = '*********'

$MyPat = '*********'

$DefinitionName = '*********'
$Branch = 'main'
$ArtifactName = 'drop'
$DownloadTo = 'C:\Downloads\Drop.zip'

# Build Auth header
$B64Pat = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$MyPat"))
$h = @{'Authorization' = 'Basic ' + $B64Pat}

# Get build definition id
$response = Invoke-WebRequest -Uri "https://dev.azure.com/$($Company)/$($Project)/_apis/build/definitions?name=$($DefinitionName)&api-version=6.0" -Method 'GET' -Headers $h -UseBasicParsing
$response_json = ($response.Content | ConvertFrom-Json)
$DefinitionId = $response_json.value.id

# Get latest build id for named branch
$response = Invoke-WebRequest -Uri "https://dev.azure.com/$($Company)/$($Project)/_apis/build/latest/$($DefinitionId)?branchName=$($Branch)&api-version=6.0-preview" -Method 'GET' -Headers $h -UseBasicParsing
$response_json = ($response.Content | ConvertFrom-Json)
$BuildId = $response_json.id

# Download latest named artifact
$response = Invoke-WebRequest -Uri "https://dev.azure.com/$($Company)/$($Project)/_apis/build/builds/$($BuildId)/artifacts?artifactName=$($ArtifactName)&api-version=6.0" -Method 'GET' -Headers $h -UseBasicParsing
$response_json = ($response.Content | ConvertFrom-Json)
$response_json.resource.downloadUrl
$response_artifact = Invoke-WebRequest -Uri $response_json.resource.downloadUrl -Method 'GET' -Headers $h -UseBasicParsing -OutFile "$($DownloadTo)"

Where: $Company is your company name in the DevOps URL. $Project is your project in the DevOps URL. $MyPat is your PrivateAccessToken for authentication. $DefinitionName is the name of your build definition.

I hope this helps.

Gary Smith
  • 43
  • 6