commit a9e7737024a04dc88c2830618128a4ed50566901 Author: Siklos Date: Wed Aug 3 17:18:17 2022 +0200 Implement azure artifact downloader diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f0aabd1 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +output diff --git a/README.md b/README.md new file mode 100644 index 0000000..ce0d7b6 --- /dev/null +++ b/README.md @@ -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` diff --git a/listArtifacts.ps1 b/listArtifacts.ps1 new file mode 100644 index 0000000..0a2f1b2 --- /dev/null +++ b/listArtifacts.ps1 @@ -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 + } +} \ No newline at end of file diff --git a/listBuilds.ps1 b/listBuilds.ps1 new file mode 100644 index 0000000..0be32ff --- /dev/null +++ b/listBuilds.ps1 @@ -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 + } +} \ No newline at end of file diff --git a/main.ps1 b/main.ps1 new file mode 100644 index 0000000..e1a324f --- /dev/null +++ b/main.ps1 @@ -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 + } +}