Release pipeline powershell task to use a file in azure repository for temp download of file (how to story)

102 Views Asked by At

This should be a stupid simple thing to do... Simply need to use a powershell task to download a .js file from an azure repository using the powershell task tell the azure agent server to run a command to import the .js into a mongodb load file.

This is new to me but I did some looking into how. What I came up with.

#Download to local path from Azure Repository
$organization = "HappyBalloons"
        $project = "99RedBalloons"
        $repository = "BagofBalloons"
        $path = "/api/red2/front-end-happy/_Mike99Balloon.js"
        $version = "99main"

        
        $pat = "$(System.AccessToken)" 

        $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($pat)"))

        $url = "https://dev.azure.com/repositoryURL"

        $response = Invoke-RestMethod -Uri $url -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Get

        # Download File
        $response.content | Set-Content -Path "$(System.DefaultWorkingDirectory)/downloaded-files/_Mike99Balloon.js" -Force
      
     

     #FilePath
      $FilePath = "$(System.DefaultWorkingDirectory)/downloaded-files/_Mike99Balloon.js"

do I really need a project PAT? or is there a better way I can't see (forest for the trees kinda thing)

$pat = "$(System.AccessToken)" 

        $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($pat)"))

so I updated the

$pat = "$(System.AccessToken)" to be "$env:System_AccessToken"

Seems to work now. or at least doesn't choke on it.

2

There are 2 best solutions below

3
Scott Richards On

If you're using Release pipelines, you can include a repository as an artifact, and then those files will be automatically downloaded onto the agent for the stages you configure it to be included on.

enter image description here

0
Shamrai Aleksander On

Check this api Items - Get:

Example:

$user = ""
$token = "<pat>" 

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

$org = "<org>"
$project = "<team_project>"
$repo = "<repo_name>"

$localfilepath = "c:/temp/<filename>"
$devopsfilepath = "/<path_to_repo_file>"
$fileurl = "https://dev.azure.com/$org/$project/_apis/git/repositories/$repo/items?scopePath=$devopsfilepath&download=true&api-version=7.2-preview.1"

function InvokeDownloadRequest ($GetUrl, $filepath)
{   
    return Invoke-RestMethod -Uri $GetUrl -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -OutFile $filepath
}


InvokeDownloadRequest $fileurl $localfilepath