Azure Pipeline YAML 中的 Azure DevOps CLI

Azure DevOps Services

如果要将 Azure DevOps CLI 与 YAML 管道配合使用,可以使用以下示例来了解如何使用 YAML 来安装 Azure CLI、添加 Azure DevOps 扩展和运行 Azure DevOps CLI 命令。

注意

本文中的步骤演示如何使用 Azure DevOps CLI 扩展对 Azure DevOps 进行身份验证并运行 az devops 命令。 若要使用 Azure CLI 与 Azure 资源交互,请使用 AzureCLI 任务

使用 Azure DevOps 进行身份验证

某些不调用 Azure DevOps 的 Azure DevOps CLI 命令,例如az devops configureaz devops -h,不需要任何身份验证,但大多数命令与 Azure DevOps 交互,并且确实需要身份验证。 可以使用 正在运行的管道使用的 System.AccessToken 安全令牌进行身份验证,方法是将其分配给名为 AZURE_DEVOPS_EXT_PAT环境变量的环境变量,如以下示例所示。

- bash: |
    az pipelines build list
  displayName: 'Show build list'
  env:
    AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)

如果有多个需要身份验证的步骤,请将 AZURE_DEVOPS_EXT_PAT 环境变量添加到每个步骤。

有关运行管道使用的安全令牌的范围的详细信息,请参阅 Access 存储库、项目和其他资源

有关使用个人访问令牌进行身份验证的详细信息,请参阅使用个人访问令牌登录(PAT)。

使用 Windows 和 Linux 托管代理的 Azure DevOps CLI

Microsoft 托管的 Windows 和 Linux 代理预配置了 Azure CLI 和 Azure DevOps CLI 扩展。

以下示例演示如何登录到 Azure DevOps 并运行几个命令。 此示例使用 ubuntu-latest Microsoft 托管的代理映像,但你可以将其替换为任何其他 Windows 或 Linux 托管映像

此示例使用 正在运行的管道使用的 System.AccessToken 安全令牌通过 Azure DevOps CLI 进行身份验证。

trigger:
- main

pool:
  vmImage: `ubuntu-latest`

steps:
- bash: az --version
  displayName: 'Show Azure CLI version'

- bash: az devops configure --defaults organization=$(System.TeamFoundationCollectionUri) project=$(System.TeamProject) --use-git-aliases true
  displayName: 'Set default Azure DevOps organization and project'

- bash: |
    az pipelines build list
    git pr list
  displayName: 'Show build list and PRs'
  env:
    AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)

将 Azure DevOps CLI 与 macOS 托管代理配合使用

macOS Microsoft 托管代理已安装 Azure CLI,但未安装 Azure DevOps CLI 扩展。 若要安装 Azure DevOps CLI 扩展,请在管道中运行以下命令,然后再调用任何 Azure DevOps CLI。

# Install Azure DevOps extension
- bash: az extension add -n azure-devops
  displayName: 'Install Azure DevOps extension'

托管代理 Azure CLI 版本

Microsoft 托管的 代理通常会将每周更新 部署到虚拟环境中的软件。 对于某些工具,使用部署时最新版本;对于其他人,该工具固定到特定版本(s)。

如果发布了较新版本的 Azure CLI,并且托管映像尚没有最新版本,可以通过在管道中运行以下命令将 Azure CLI 版本升级到最新版本。

# Specify python version
- task: UsePythonVersion@0
  inputs:
    versionSpec: '3.x'
    architecture: 'x64'

# Update to latest Azure CLI version
- bash: pip install --pre azure-cli
  displayName: 'Upgrade Azure CLI'

有条件地安装 Azure DevOps CLI 扩展

如果管道在多个 Microsoft 托管的 VM 映像上运行,其中一些映像未安装 Azure DevOps CLI 扩展,则可以有条件地执行此步骤,如以下示例所示。

trigger:
- main

# Run on multiple Microsoft-hosted agent images
strategy:
  matrix:
    linux22:
      imageName: "ubuntu-22.04"
    linux20:
      imageName: "ubuntu-20.04"
    mac13:
      imageName: "macos-13"
    mac12:
      imageName: "macos-12"
    mac11:
      imageName: "macos-11"
    windows2019:
      imageName: "windows-2019"
    windows2022:
      imageName: "windows-2022"
  maxParallel: 3

pool:
  vmImage: $(imageName)

steps:
- bash: az --version
  displayName: 'Show Azure CLI version'

# Install Azure DevOps CLI extension only on macOS images
- bash: az extension add -n azure-devops
  condition: contains(variables.imageName, 'mac')
  displayName: 'Install Azure DevOps extension'

# Azure DevOps CLI extension call that does not require login or credentials
# since it configures the local environment
- bash: az devops configure --defaults organization=$(System.TeamFoundationCollectionUri) project=$(System.TeamProject) --use-git-aliases true
  displayName: 'Set default Azure DevOps organization and project'

# Call that does require credentials, use the System.AccessToken PAT
# and assign to AZURE_DEVOPS_EXT_PAT which is known to Azure DevOps CLI extension
- bash: |
    az pipelines build list
    git pr list
  env:
    AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
  displayName: 'Show build list and PRs'

使用自承载代理的 Azure DevOps CLI

如果未将自承载代理配置为使用 Azure DevOps CLI 所需的软件,或者想要确保拥有最新版本,则可以使用以下步骤安装所需的软件。

手动安装 Azure CLI 和 Azure DevOps CLI 扩展

为代理预配虚拟机映像时,在自承载代理上安装 Azure CLI 和 Azure DevOps CLI 扩展比每次运行管道时安装它们要快得多。

在管道中安装 Azure CLI 和 Azure DevOps CLI 扩展

以下示例使用管道在自承载代理上配置 Azure CLI 和 Azure DevOps CLI 扩展具有以下先决条件。

  • 使用 Python 安装 Azure CLI

    # Specify python version if you have side-by-side versions
    - task: UsePythonVersion@0
      inputs:
        versionSpec: '3.x'
        architecture: 'x64'
    
    # Update pip to latest
    - bash: python -m pip install --upgrade pip
      displayName: 'Upgrade pip'
    
    # Update to latest Azure CLI version, min version required for Azure DevOps is 2.10.1
    - bash: pip install --pre azure-cli
      displayName: 'Upgrade Azure CLI'
    
  • 安装 Azure CLI DevOps 扩展

    • 已安装 Azure CLI 2.10.1 或更高版本。
    • 代理和路径中安装了一个版本 bash 。 使用 bash 任务需要 bash 安装。
    # Install Azure DevOps extension
    - bash: az extension add -n azure-devops
      displayName: 'Install Azure DevOps extension'
    
    # Now you can make calls into Azure DevOps CLI
    # ...
    

以下示例安装 Azure CLI,后接 Azure DevOps CLI 扩展。

steps:
# Specify python version if you have side-by-side versions
- task: UsePythonVersion@0
  inputs:
    versionSpec: '3.x'
    architecture: 'x64'

# Update pip to latest
- bash: python -m pip install --upgrade pip
  displayName: 'Upgrade pip'

# Update to latest Azure CLI version, min version required for Azure DevOps is 2.10.1
- bash: pip install --pre azure-cli
  displayName: 'Upgrade Azure CLI'

# Install Azure DevOps extension
- bash: az extension add -n azure-devops
  displayName: 'Install Azure DevOps extension'

# Now you can make calls into Azure DevOps CLI
# ...

将 Azure DevOps CLI 调用的结果分配给变量

若要存储对管道变量的 Azure DevOps CLI 调用的结果,请使用task.setvariable脚本中“设置变量”中所述的语法。 以下示例检索名为 Fabrikam-2023 的变量组的 ID,然后在后续步骤中使用此值。

variables:
- name: variableGroupId

trigger: none

pool:
  vmImage: "ubuntu-latest"

steps:
- bash: az devops configure --defaults organization=$(System.TeamFoundationCollectionUri) project=$(System.TeamProject) --use-git-aliases true
  displayName: 'Set default Azure DevOps organization and project'

- bash: echo "##vso[task.setvariable variable=variableGroupId]$(az pipelines variable-group list --group-name Fabrikam-2023 --query [].id -o tsv)"
  env:
    AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
  displayName: 'Get Fabrikam-2023 variable group id'

- bash: az pipelines variable-group variable list --group-id $(variableGroupId)
  env:
    AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
  displayName: 'List variables in Fabrikam-2023 variable group'

有关使用变量(包括跨作业和阶段处理变量)的更多示例,请参阅 “定义变量”。 有关上一示例中使用的查询语法示例,请参阅 如何使用 JMESPath 查询查询 Azure CLI 命令输出。