Implement azure artifact downloader

This commit is contained in:
Siklos 2022-08-03 17:18:17 +02:00 committed by Eric NGUYEN
commit a9e7737024
5 changed files with 101 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
output

25
README.md Normal file
View file

@ -0,0 +1,25 @@
# Azure DevOps artifacts downloader
# Getting Started
## Requierements
- Powershell > 5
## Configure
Edit `main.ps1` and replace all 4 first variables to contains the information
of your repository
```ps1
$token = 'ZW5ndXllbjpnN25h...' # replace this
$instance = 'dev.azure.com/myorganization' # replace this
$project = 'myproject' # replace this
$branches = 'V1.0.0.0', 'V1.0.0.1' # replace this
$outDir = "$PSScriptRoot\output" # optionnally replace the output path
```
# Execute
Open a powershell terminal and run the `main.ps1`

16
listArtifacts.ps1 Normal file
View file

@ -0,0 +1,16 @@
function ListArtifacts {
Param (
[string]$token,
[string]$instance,
[string]$project,
[string]$buildId
)
Process {
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Basic $token")
$headers.Add("Cookie", "VstsSession=%7B%22PersistentSessionId%22%3A%22358fb34b-2e05-46fc-9452-287c2986ac02%22%2C%22PendingAuthenticationSessionId%22%3A%2200000000-0000-0000-0000-000000000000%22%2C%22CurrentAuthenticationSessionId%22%3A%2200000000-0000-0000-0000-000000000000%22%2C%22SignInState%22%3A%7B%7D%7D")
$url = "https://$instance/$project/_apis/build/builds/$buildId/artifacts?api-version=7.1-preview.5"
$response = Invoke-RestMethod $url -Method 'GET' -Headers $headers
return $response
}
}

15
listBuilds.ps1 Normal file
View file

@ -0,0 +1,15 @@
function ListBuilds {
Param (
[string]$token,
[string]$instance,
[string]$project
)
Process {
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Basic $token")
$headers.Add("Cookie", "VstsSession=%7B%22PersistentSessionId%22%3A%22358fb34b-2e05-46fc-9452-287c2986ac02%22%2C%22PendingAuthenticationSessionId%22%3A%2200000000-0000-0000-0000-000000000000%22%2C%22CurrentAuthenticationSessionId%22%3A%2200000000-0000-0000-0000-000000000000%22%2C%22SignInState%22%3A%7B%7D%7D")
$url = "https://$instance/$project/_apis/build/builds?api-version=7.1-preview.7"
$response = Invoke-RestMethod $url -Method 'GET' -Headers $headers
return $response
}
}

44
main.ps1 Normal file
View file

@ -0,0 +1,44 @@
. './listBuilds.ps1'
. './listArtifacts.ps1'
$token = 'token'
$instance = 'dev.azure.com/myorganization'
$project = 'myproject'
$branches = 'V1.0.0.0', 'V1.0.0.1'
$outDir = "$PSScriptRoot\output"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Basic ${token}")
# Remove outDir
if (Test-Path $outDir) {
Remove-Item -Recurse $outDir
}
# Get the list of builds in a repo
$builds = ListBuilds -token $token -instance $instance -project $project
ForEach ($branch in $branches) {
# Select only the last build for the branches you want
$lastBuild = $builds.value | Where-Object { $_.sourceBranch -eq "refs/heads/$branch"} | Select-Object -First 1
# Get the list of artifacts for each build
$listArtifacts = ListArtifacts -token $token -instance $instance -project $project -buildId $lastBuild.id
$artifacts = $listArtifacts.value
# Create output directory
$outputDir = "$outDir\$branch"
New-Item -Path $outDir -Name $branch -ItemType Directory
# Download the artifacts
ForEach ($artifact in $artifacts) {
$artifactName = $artifact.name
$outputFile = "$outputDir\$artifactName.zip"
Invoke-RestMethod -Uri $artifact.resource.downloadUrl -Method 'GET' -Headers $headers -OutFile $outputFile
# Unzip
Expand-Archive $outputFile -DestinationPath $outputDir
}
}