2
votes

It currently takes me about a minute to mount an azure file share to the azure DevOps pipeline. I think, this is too long. My pipelines contains two steps for the mount operation: retrieve the credentials and mount.

enter image description here

This is what my pipeline looks like:

- task: AzureCLI@2
      inputs:
        azureSubscription: 'My Resource Group'
        scriptType: 'ps'
        scriptLocation: 'inlineScript'
        inlineScript: |
          (get-date).ToString('T')
          $sas_expiry = (Get-Date).AddHours($(storage.sas.expiry_hours)).ToUniversalTime().ToString("yyyy-M-d'T'H:M'Z'")
          echo "Getting keys..."
          (get-date).ToString('T')
          $key=(az storage account keys list --account-name $(storage.account) | jq '.[0].value' -r)
          echo "Keys are here."
          echo "##vso[task.setvariable variable=storage.key;issecret=true]$key"
          (get-date).ToString('T')
          $sas_key=(az storage account generate-sas --account-key $key --account-name $(storage.account) --expiry  $sas_expiry --https-only --permissions acdlpruw --resource-types sco --services f --output tsv)
          echo "##vso[task.setvariable variable=storage.sas.key;issecret=false]$sas_key"
          (get-date).ToString('T')
      displayName: Get azure file share credentials
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          $connectTestResult = Test-NetConnection -ComputerName mystorage.file.core.windows.net -Port 445
          if ($connectTestResult.TcpTestSucceeded) {
              cmd.exe /C "cmdkey /add:`"mystorage.file.core.windows.net`" /user:`"Azure\$(storage.account)`" /pass:`"$(storage.key)`""
              # Mount the drive
              New-PSDrive -Name $(storage.drive) -PSProvider FileSystem -Root "$(storage.unc)" -Persist
              dir z:
          }
          else {
              Write-Error -Message "Unable to reach the Azure storage account via port 445. Check to make sure your organization or ISP is not blocking port 445, or use Azure P2S VPN, Azure S2S VPN, or Express Route to tunnel SMB traffic over a different port."
          }
      displayName: "Mount azure file share to Z"

... I obviously have added some timestamp outputs for easier "profiling". This is the logging.

Starting: Get azure file share credentials
==============================================================================
Task         : Azure CLI
Description  : Run Azure CLI commands against an Azure subscription in a PowerShell Core/Shell script when running on Linux agent or PowerShell/Powershell Core/Batch script when running on Windows agent.
Version      : 2.0.5
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/deploy/azure-cli
==============================================================================
C:\windows\system32\cmd.exe /D /S /C ""C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" --version"
WARNING: You have 2 updates available. Consider updating your CLI installation. Instructions can be found at https://docs.microsoft.com/en-us/cli/azure/install-azure-cli
azure-cli                         2.0.80 *

command-modules-nspkg              2.0.3
core                              2.0.80 *
nspkg                              3.0.4
telemetry                          1.0.4

Extensions:
azure-devops                      0.17.0

Python location 'C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\python.exe'
Extensions directory 'C:\Program Files\Common Files\AzureCliExtensionDirectory'

Python (Windows) 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 02:47:15) [MSC v.1900 32 bit (Intel)]

Legal docs and information: aka.ms/AzureCliLegal




Please let us know how we are doing: https://aka.ms/clihats
Setting AZURE_CONFIG_DIR env variable to: d:\a\_temp\.azclitask
Setting active cloud to: AzureCloud
C:\windows\system32\cmd.exe /D /S /C ""C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" cloud set -n AzureCloud"
C:\windows\system32\cmd.exe /D /S /C ""C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" login --service-principal -u *** -p *** --tenant ***"
[
  {
    "cloudName": "AzureCloud",
    "id": "3c8ec2e2-61cd-4554-bd9d-7e18817ef269",
    "isDefault": true,
    "name": "DevOps Experiments",
    "state": "Enabled",
    "tenantId": "***",
    "user": {
      "name": "***",
      "type": "servicePrincipal"
    }
  }
]
C:\windows\system32\cmd.exe /D /S /C ""C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" account set --subscription 3c8ec2e2-61cd-4554-bd9d-7e18817ef269"
C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'd:\a\_temp\azureclitaskscript1581554138338.ps1'"
12:36:15 AM
Getting keys...
12:36:15 AM
Keys are here.
12:36:21 AM
12:36:23 AM
C:\windows\system32\cmd.exe /D /S /C ""C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" account clear"
Finishing: Get azure file share credentials

... and for step 2:

Starting: Mount azure file share to Z
==============================================================================
Task         : PowerShell
Description  : Run a PowerShell script on Linux, macOS, or Windows
Version      : 2.163.1
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
==============================================================================
Generating script.
========================== Starting Command Output ===========================
"C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'd:\a\_temp\5ebb0c88-b7df-4c27-9dd3-51c6966ba590.ps1'"

CMDKEY: Credential added successfully.

Name           Used (GB)     Free (GB) Provider      Root                                               CurrentLocation
----           ---------     --------- --------      ----                                               ---------------
Z                  41.84       5078.16 FileSystem    \\mystorage.file.core.windows.n...                               

PSPath            : Microsoft.PowerShell.Core\FileSystem::Z:\builds
PSParentPath      : Microsoft.PowerShell.Core\FileSystem::Z:\
PSChildName       : builds
PSDrive           : Z
PSProvider        : Microsoft.PowerShell.Core\FileSystem
PSIsContainer     : True
Name              : builds
FullName          : Z:\builds
Parent            : 
Exists            : True
Root              : Z:\
Extension         : 
CreationTime      : 2/9/2020 8:28:26 PM
CreationTimeUtc   : 2/9/2020 8:28:26 PM
LastAccessTime    : 2/9/2020 8:28:26 PM
LastAccessTimeUtc : 2/9/2020 8:28:26 PM
LastWriteTime     : 2/9/2020 8:28:26 PM
LastWriteTimeUtc  : 2/9/2020 8:28:26 PM
Attributes        : Directory
Mode              : d-----
BaseName          : builds
Target            : 
LinkType          : 



Finishing: Mount azure file share to Z

All this runs on the default Microsoft Azure hosted agents. What can I do to mount the file share faster?

1
You can try using a single powershell task to execute the scripts in above tasks. you can use az login --service-principal -u *** -p *** --tenant *** and az account set --subscription 3c8ec2e2-61cd-4554-bd9d-7e18817ef269 to login and set azure subscription context. It should be faster to execute the scripts in one task than in two tasks.Levi Lu-MSFT
Thanks! Merging the two steps into one saved about 7-15 seconds!hey
That's great it saved some time. I post above comment to the answer, please accept it if it helps.Levi Lu-MSFT
thanks. would you really use the az cli inside a pipeline? it seems to login automatically, and I would not want any credentials inside the pipeline...hey
Yes, you are right, az cli will login automatically, for it uses service connection. The scripts should be in az cli taskLevi Lu-MSFT

1 Answers

1
votes

Checking the task log for az cli, it actually invoked powershell.exe to execute the scripts in az cli task.

So the scripts in powershell task can be merged into the az cli task.

It should be faster to execute the scripts in one task than in two tasks.