Home > Software engineering >  How to download file from Azure Repos git using AzureDevOps pipelines && PowerShell
How to download file from Azure Repos git using AzureDevOps pipelines && PowerShell

Time:06-02

I am newbie in powershell and can't successfully execute my powershell script.

I have some .zip in my repository and I need to download it:

enter image description here

I use Azure DevOps and powershell script for achiving it:

- task: PowerShell@2
  displayName: Get .zip 
  inputs:
  targetType: 'inline'
  script: |
    $filePath= '$(Build.ArtifactStagingDirectory)'
    $token = "tokenFromUserSettings"

    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "",$token)))

    $url = "https://dev.azure.com/devopstry2305/_git/2305first_try?path=/TempTest.zip&download=true&api-version=5.0"

    $result = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/text" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} | Out-File $filePath

But I get this error:

enter image description here

I must work with private repository and use personal access token. I folllowed this guide https://gmusumeci.medium.com/how-to-download-files-from-azure-devops-repos-from-a-powershell-script-51b11d2aa7d5 but probably didn't take into account all the details. Also I viewed some questions on Stack about it but didn't understand that examples.

I dont know what '$filePath' is.. Is it place for storing file? $url - is it correct in my example?

It would be great if you fix my mistakes or give me another way of solving my task. Thank you.

CodePudding user response:

As long as the zip is contained in the repository on which you have your pipeline, this should be downloaded automatically when the source code is checked out. If you have not defined different checkout paths this zip should be inside $(Build.SourcesDirectory). If you need to download a different repository zip, then you will need to follow another approach.

As a result you can use a powershell to list the contents of this folder and verify its existence.

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      # Write your PowerShell commands here.
      ls $(Build.SourcesDirectory)
  • Related