Search code examples
azureazure-pipelines-release-pipelineazcopyazure-file-copy

azcopy not working on Azure Hosted Build Agents


We were using AzureFileCopy to transfer files to a VM. The latest release has introduced a bug which means you receive an error when trying to do this. Apparently the url and the SAS token should have a ? between them but this has been missed out. I have reported the issue.

A solution was to break the task into two, one to copy the file to a storage account and a second to use azcopy to copy the storage blob data to the VM.

This was working for a spell but last week azcopy has started to fail reporting that:

azcopy : The term 'azcopy' is not recognized as the name of a cmdlet, function, script file, or...

The task in the release is PowerShell on Target Machines and the inline script is literally calling azcopy with parameters.

At present we can't release any of our software as it is dependent on this step to configure the VM.

Things we have tried to resolve this:

  1. To search for azcopy.exe on the build agent; this doesn't seem to find it.
  2. To try both windows-2019 and windows-2022 agents.
  3. Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Confirm:$False -Force; this fails with a confirm prompt which we can't workaround as PowerShell is understandably non interactive.
  4. To download azcopy and extract it; this fails with the azcopy error in the OP.
  5. To download azcopy as an artifact; this fails with the azcopy error in the OP.

Solution

  • From your description, you are using the PowerShell on Target Machines task to run the Azcopy command.

    The task will use the VM environment to run the Azcopy command. You need to install Azcopy Tool to the VM to complete the operation.

    You can add the download/extract azcopy command to the PowerShell on Target Machines task.

    For example:

    PowerShell script:

    Invoke-WebRequest -Uri 'https://azcopyvnext.azureedge.net/release20220315/azcopy_windows_amd64_10.14.1.zip' -OutFile 'azcopyv10.zip'
    Expand-archive -Path '.\azcopyv10.zip' -Destinationpath '.\'
    $AzCopy = (Get-ChildItem -path '.\' -Recurse -File -Filter 'azcopy.exe').FullName
    # Invoke AzCopy 
    & $AzCopy xxxxxx
    

    PowerShell on Target Machines task

    - task: PowerShellOnTargetMachines@3
      displayName: 'Run PowerShell on Target Machines'
      inputs:
        Machines: XX
        UserName: XX
        UserPassword: 'XX'
        InlineScript: |
         Invoke-WebRequest -Uri 'https://azcopyvnext.azureedge.net/release20220315/azcopy_windows_amd64_10.14.1.zip' -OutFile 'azcopyv10.zip'
         Expand-archive -Path '.\azcopyv10.zip' -Destinationpath '.\'
         $AzCopy = (Get-ChildItem -path '.\' -Recurse -File -Filter 'azcopy.exe').FullName
         # Invoke AzCopy 
         & $AzCopy XXXX
    

    enter image description here

    The download and extract command only need to execute once. In the next runs, you can directly use the following powershell command to use the azcopy

    $AzCopy = (Get-ChildItem -path '.\' -Recurse -File -Filter 'azcopy.exe').FullName
    # Invoke AzCopy 
    & $AzCopy xxxxxx
    

    For more detailed info, you can refer to the doc: PowerShellOnTargetMachines@3 - PowerShell on target machines v3 task and Use script to download Azcopy