azure - 如何在自托管 Windows 代理上从 Azure DevOps 管道运行 Azure CLI 任务?

标签 azure powershell azure-devops azure-pipelines azure-cli

情况

我的自托管 Windows 代理运行来自 Azure DevOps 的管道。到 管理 Azure 中的资源 我想使用 Azure CLI 任务。即使在前面的步骤中安装了 Azure CLI,AzureCLI 任务也会失败。

我有两个从我的管道运行的脚本。

  • (1) 安装 Azure CLI --> 成功
  • (2) 运行 Azure CLI 命令 --> 在不运行任何内部代码的情况下失败,甚至“Hello, World!”没有被执行。
2021-03-05T14:50:02.5986237Z ##[error]Azure CLI 2.x is not installed on this machine.
2021-03-05T14:50:02.6391547Z ##[error]Script failed with error: Error: Unable to locate executable file: 'az'. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also verify the file has a valid extension for an executable file.

Microsoft

  • >(1) 在代理上安装新软件后,必须重新启动代理才能使新功能显示在池中,以便构建可以运行。
  • >(2) 安装完成后,您需要重新打开 PowerShell 才能使用 Azure CLI。

AzureCLI 任务无法找到已安装的 Azure CLI 可执行文件。如何修复此问题以便可以运行 AzureCLI 任务?

我已经尝试过的

  • 通过 PowerShell 设置 Azure CLI 的路径。路径已设置,但 Azure CLI 任务的 Powershell 任务失败。
  • 直接在我的安装脚本中运行 AzureCLI 命令,这可以工作,但当我想要使用 AzureCLI 任务中定义的服务主体时,我需要使用单独的凭据登录 Azure。
  • 正在虚拟机上重新启动 Microsoft Agent 服务,但我的代理上没有提到的服务 ( https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/v2-windows?view=azure-devops )
  • 设置执行 Azure CLI 任务之前的延迟。
  • 使用 Microsoft 托管代理,该代理 100% 有效,但不符合我公司的要求,因此不是一个选择。

管道详细信息

trigger:
  branches:
    exclude:
    - master

pool:
  name: SelfHosted-AgentPool
  vmImage: 'windows-latest'

variables:
  environment.name: 'Test'

stages:
- stage: build_and_deploy
  jobs:
  - deployment: VMBackup_Testing  
    displayName: "Enable Backup Protection"
    environment: '$(environment.name)'
    strategy:
      runOnce:
        deploy:
          steps:
          - checkout: self    
          
         
          - task: PowerShell@2
            inputs:
              filePath: '$(System.DefaultWorkingDirectory)/Templates/Snippets/InstallAzureCLI.ps1'

          - task: AzureCLI@2
            inputs:
              workingDirectory: 'C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin'
              azureSubscription: 'XXX'
              scriptType: 'ps'
              scriptLocation: 'scriptPath'
              scriptPath: '$(System.DefaultWorkingDirectory)/Templates/Snippets/EnableBackupProtection.ps1'

安装 Azure CLI 脚本

# Download and Install Azure CLI
Invoke-WebRequest -Uri https://azcliprod.blob.core.windows.net/msi/azure-cli-2.19.1.msi -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList "/I AzureCLI.msi /quiet"; rm .\AzureCLI.msi

# Update PATH for Powershell to use new installed software
setx /M PATH "$env:Path += ;C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin"

# Test if PATH of Azure CLI exists
Test-Path -Path "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin"

# Reload Shell with new PATH 
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")

# Check if AZ CLI is installed
az version

Azure CLI 命令脚本

# Check if script gets executed
Write-Host "Hello, World!"

# AZ CLI commands to enable Backup Protection
az backup protection enable-for-vm `
    --resource-group XXX`
    --vault-name XXXX`
    --vm $(az vm show -g XXX -n XXX --query id) `
    --policy-name DailyBackup

最佳答案

为什么每次在同一个自托管 Windows 代理上运行管道时都需要安装 Azure CLI?

Microsoft-hosted agents不同,您只需要在自托管代理机器上手动安装所需的工具,然后就可以在代理上运行的管道中使用它们。

  1. 登录到安装了自承载代理的 Windows 计算机(本地或虚拟机)。

  2. 打开 Web 浏览器,从 here 下载已发布的最新 Azure CLI 的 MSI 安装程序。 .

  3. 通过 MSI 安装程序安装 Azure CLI 时,通常安装向导会自动将此工具添加到系统环境变量 PATH 中。安装完成后,您可以在机器上打开“编辑系统环境变量”进行检查。如果没有添加到系统环境变量PATH中,可以手动添加。

enter image description here

  • 完成上述步骤后,按照文档的建议,重新启动代理服务或重新启动计算机,以便已安装的 Azure CLI 工具可以在池中代理的功能中列出。
  • 通过这种方式,当您设置管道在此自托管代理上运行时,您可以直接调用 Azure CLI,而无需在管道中安装 Azure CLI 的任何步骤。

    关于azure - 如何在自托管 Windows 代理上从 Azure DevOps 管道运行 Azure CLI 任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66509534/

    相关文章:

    powershell - DSC 文件资源文件不复制 PDB 文件

    用于列出代理池和运行的作业的 Azure CLI 命令

    azure - 获取 Azure DevOps YAML 管道中所有提交的更改

    c# - msbuild 命令行尚不支持 WebPublishMethod(FileSystem)

    c# - 来自计时器触发器的 Azure 函数输出服务总线绑定(bind)

    Azure Worker角色缩小位置

    sql - 无法将 Windows Azure 连接到 MySQL WorkBench

    Powershell:从两个或多个循环中中断

    azure - 无法在内联 Javascript 中引用逻辑应用操作输出

    将特定 PDF 页面打印成图像的 Powershell 脚本