다음을 통해 공유


Azure DevOps CLI를 사용하여 변수 그룹의 변수 관리

Azure DevOps Services

Azure Pipelines에서 변수를 관리하는 것은 CI/CD 워크플로에서 유연성과 보안을 유지하는 데 매우 중요합니다. 이 가이드에서는 Azure DevOps CLI를 사용하여 Azure Pipelines 변수 그룹 내에서 비밀 및 비보안 변수를 만들고 관리하는 방법을 보여 줍니다. 변수 그룹을 사용하여 변수 관리를 중앙 집중화하고 중요한 정보가 안전하게 처리되도록 할 수 있습니다.

이 가이드의 샘플에서는 다음 방법을 알아봅니다.

  • GitHub에 저장된 YAML 파일을 사용하여 Azure Pipelines 파이프라인을 정의합니다.
  • 비밀 변수와 비보안 변수를 모두 포함하는 변수 그룹을 만듭니다.
  • Azure DevOps CLI를 사용하여 파이프라인을 실행하고 실행 처리 및 출력을 모니터링합니다.

참고 항목

이 샘플에서는 변수 그룹을 사용하여 Azure DevOps CLI의 기능을 시연합니다. 보안을 강화하려면 Pipelines UI 의 변수 그룹에 변수를 정의하거나 Azure Key Vault의 비밀에 변수 그룹을 연결합니다.

필수 조건

파이프라인 YAML 파일 저장

다음 YAML 파이프라인 정의를 GitHub 리포지토리의 루트 디렉터리 및 main 분기에 azure-pipelines.yml이라는 파일로 저장합니다.

parameters:
- name: image
  displayName: 'Pool image'
  default: ubuntu-latest
  values:
  - windows-latest
  - windows-latest
  - ubuntu-latest
  - ubuntu-latest
  - macOS-latest
  - macOS-latest
- name: test
  displayName: Run Tests?
  type: boolean
  default: false

variables:
- group: "Contoso Variable Group"
- name: va
  value: $[variables.a]
- name: vb
  value: $[variables.b]
- name: vcontososecret
  value: $[variables.contososecret]

trigger:
- main

pool:
  vmImage: ubuntu-latest

steps:
- script: |
    echo "Hello, world!"
    echo "Pool image: ${{ parameters.image }}"
    echo "Run tests? ${{ parameters.test }}"
  displayName: 'Show runtime parameter values'

- script: |
    echo "a=$(va)"
    echo "b=$(vb)"
    echo "contososecret=$(vcontososecret)"
    echo
    echo "Count up to the value of the variable group's nonsecret variable *a*:"
    for number in {1..$(va)}
    do
        echo "$number"
    done
    echo "Count up to the value of the variable group's nonsecret variable *b*:"
    for number in {1..$(vb)}
    do
        echo "$number"
    done
    echo "Count up to the value of the variable group's secret variable *contososecret*:"
    for number in {1..$(vcontososecret)}
    do
        echo "$number"
    done
  displayName: 'Test variable group variables (secret and nonsecret)'
  env:
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)

샘플 스크립트

이 샘플은 다음 작업을 수행합니다.

  • DevOps 리소스 만들기
  • 파이프라인 실행
  • 변수 값을 세 번 수정합니다.
  • 변수 값이 변경될 때마다 파이프라인을 다시 실행합니다.

이 스크립트는 Azure DevOps에서 다음 리소스를 만듭니다.

  • DevOps 조직의 프로젝트
  • GitHub 서비스 연결
  • 파이프라인
  • 비보안 변수 2개와 비밀 변수 1개가 있는 변수 그룹

스크립트를 실행하기 전에 다음과 같이 다음 자리 표시자를 바꿉니다.

  • <devops-organization> Azure DevOps 조직 이름
  • <github-organization> GitHub 조직 또는 사용자 이름
  • <github-repository> GitHub 리포지토리 이름
  • <pipelinename> 3-19자 사이이며 숫자와 소문자만 포함하는 파이프라인의 이름입니다. 스크립트는 5자리 고유 식별자를 추가합니다.

로컬 환경에 GitHub PAT를 저장합니다.

AZURE_DEVOPS_EXT_GITHUB_PAT=<your-github-pat>

GitHub에 YAML 파일을 저장한 후 Cloud Shell의 Bash 셸 또는 로컬에서 다음 Azure DevOps CLI 스크립트를 실행합니다.

#!/bin/bash

# Provide placeholder variables.
devopsOrg="https://dev.azure.com/<devops-organization>"
githubOrg="<github-organization>"
githubRepo="<github-repository>"
pipelineName="<pipelinename>"
repoName="$githubOrg/$githubRepo"
repoType="github"
branch="main"

# Declare other variables.
uniqueId=$RANDOM
devopsProject="Contoso DevOps Project $uniqueId"
serviceConnectionName="Contoso Service Connection $uniqueId"

# Sign in to Azure CLI and follow the sign-in instructions, if necessary.
echo "Sign in."
az login

# Sign in to Azure DevOps with your Azure DevOps PAT, if necessary.
echo "Sign in to Azure DevOps."
az devops login

# Create the Azure DevOps project and set defaults.
projectId=$(az devops project create \
    --name "$devopsProject" --organization "$devopsOrg" --visibility private --query id)
projectId=${projectId:1:-1}  # Just set to GUID; drop enclosing quotes.
az devops configure --defaults organization="$devopsOrg" project="$devopsProject"
pipelineRunUrlPrefix="$devopsOrg/$projectId/_build/results?buildId="

# Create GitHub service connection.
githubServiceEndpointId=$(az devops service-endpoint github create \
    --name "$serviceConnectionName" --github-url "https://www.github.com/$repoName" --query id)
githubServiceEndpointId=${githubServiceEndpointId:1:-1}  # Just set to GUID; drop enclosing quotes.

# Create the pipeline.
pipelineId=$(az pipelines create \
    --name "$pipelineName" \
    --skip-first-run \
    --repository $repoName \
    --repository-type $repoType \
    --branch $branch \
    --service-connection $githubServiceEndpointId \
    --yml-path azure-pipelines.yml \
    --query id)

# Create a variable group with 2 non-secret variables and 1 secret variable.
# (contososecret < a < b). Then run the pipeline.
variableGroupId=$(az pipelines variable-group create \
    --name "$variableGroupName" --authorize true --variables a=12 b=29 --query id)
az pipelines variable-group variable create \
    --group-id $variableGroupId --name contososecret --secret true --value 17
pipelineRunId1=$(az pipelines run --id $pipelineId --open --query id)
echo "Go to the pipeline run's web page to view the output results of the 'Test variable group variables' job for the 1st run."
echo "If the web page doesn't automatically appear, go to:"
echo "    ${pipelineRunUrlPrefix}${pipelineRunId1}"
read -p "Press Enter to change the value of one of the variable group's nonsecret variables, then run again:"

# Change the value of one of the variable group's nonsecret variables.
az pipelines variable-group variable update \
    --group-id $variableGroupId --name a --value 22
pipelineRunId2=$(az pipelines run --id $pipelineId --open --query id)
echo "Go to the pipeline run's web page to view the output results of the 'Test variable group variables' job for the 2nd run."
echo "If the web page doesn't automatically appear, go to:"
echo "    ${pipelineRunUrlPrefix}${pipelineRunId2}"
read -p "Press Enter to change the value of the variable group's secret variable, then run once more:"

# Change the value of the variable group's secret variable.
az pipelines variable-group variable update \
    --group-id $variableGroupId --name contososecret --value 35
pipelineRunId3=$(az pipelines run --id $pipelineId --open --query id)
echo "Go to the pipeline run's web page to view the output results of the 'Test variable group variables' job for the 3rd run."
echo "If the web page doesn't automatically appear, go to:"
echo "    ${pipelineRunUrlPrefix}${pipelineRunId3}"
read -p "Press Enter to continue:"

리소스 정리

Azure 프로젝트에 대한 요금이 발생하지 않도록 하려면 해당 리소스를 삭제하는 샘플 프로젝트를 삭제할 수 있습니다.

id 다음 명령의 출력에서 샘플 프로젝트의 복사합니다.

az devops project list --org <your-organization>

다음 명령을 실행하여 프로젝트를 삭제합니다.

az devops project delete --id <project-id> --org <your-organization> --yes

다음 명령을 실행하여 로컬 환경을 정리합니다.

export AZURE_DEVOPS_EXT_GITHUB_PAT=""
az devops configure --defaults organization="" project=""

Azure CLI 참조

이 문서의 샘플에서는 다음 Azure CLI 명령을 사용합니다.