Azure Pipeline YAML의 Azure DevOps CLI

Azure DevOps Services

YAML 파이프라인에서 Azure DevOps CLI를 사용하려는 경우 다음 예제를 사용하여 YAML을 사용하여 Azure CLI를 설치하고, Azure DevOps 확장을 추가하고, Azure DevOps CLI 명령을 실행하는 방법을 이해할 수 있습니다.

참고 항목

이 문서의 단계에서는 Azure DevOps로 인증하고 Azure DevOps CLI 확장을 사용하여 명령을 실행하는 az devops 방법을 보여 줍니다. Azure CLI를 사용하여 Azure 리소스와 상호 작용하려면 AzureCLI 작업을 사용합니다.

Azure DevOps를 사용하여 인증

Azure DevOps와 같이 az devops configureaz devops -h호출하지 않는 일부 Azure DevOps CLI 명령은 인증이 필요하지 않지만 대부분의 명령은 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에 로그인하고 몇 가지 명령을 실행하는 방법을 보여 줍니다. 이 예제에서는 Microsoft 호스팅 에이전트 이미지를 사용 ubuntu-latest 하지만 다른 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)

macOS 호스팅 에이전트를 사용하는 Azure DevOps CLI

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 호스팅 에이전트는 일반적으로 가상 환경에서 소프트웨어에 매주 업데이트를 배포합니다. 일부 도구의 경우 배포 당시의 최신 버전이 사용됩니다. 다른 사용자의 경우 도구는 특정 버전에 고정됩니다.

최신 버전의 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 CLI 설치를 참조하세요. Windows, LinuxmacOS에 대한 별도의 지침이 있습니다.
  • 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 확장 설치

    # 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 명령 출력을 쿼리하는 방법을 참조하세요.