다음을 통해 공유


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

Azure DevOps Services | Azure DevOps Server

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 파일로 저장합니다.

parameters:
- name: image
  displayName: 'Pool image'
  default: ubuntu-latest
  values:
  - windows-latest
  - ubuntu-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 조직 이름입니다. 예를 들어 Azure DevOps URL이 https://dev.azure.com/Contoso면 다음을 사용합니다 Contoso.
  • <github-organization> GitHub 조직 또는 사용자 이름입니다. 예를 들어 myusername 또는 myorganization.
  • <github-repository> GitHub 리포지토리 이름입니다. 예를 들어 리포지토리 URL이 https://github.com/myusername/my-repo면 다음을 사용합니다 my-repo.
  • <pipelinename> 3-19자 사이이며 숫자와 소문자만 포함하는 파이프라인의 이름입니다. 스크립트는 5자리 고유 식별자를 추가합니다. 예: mypipeline.

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

AZURE_DEVOPS_EXT_GITHUB_PAT=<your-github-pat>

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

중요합니다

최신 버전의 Azure CLI 및 DevOps 확장이 설치되어 있는지 확인합니다. az upgradeaz extension add --name azure-devops를 실행한 후 이 스크립트를 실행합니다.

#!/bin/bash

# ===== CONFIGURATION =====
# Replace the placeholder values with your own.
devopsOrg="https://dev.azure.com/<devops-organization>"
githubOrg="<github-organization>"
githubRepo="<github-repository>"
pipelineName="<pipeline-name>"
repoName="$githubOrg/$githubRepo"
repoType="github"
branch="main"

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

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

# Sign in to Azure DevOps with your Azure DevOps PAT, if necessary.
# Uncomment the following line if your Azure AD account doesn't have Azure DevOps access.
# echo "Signing in to Azure DevOps..."
# az devops login

# ===== PROJECT CREATION =====
# Create the Azure DevOps project and set defaults.
echo "Creating Azure DevOps project..."
projectId=$(az devops project create \
    --name "$devopsProject" \
    --organization "$devopsOrg" \
    --visibility private \
    --query id \
    --output tsv)
echo "Project created with ID: $projectId"

# Set default organization and project for subsequent commands.
az devops configure --defaults organization="$devopsOrg" project="$devopsProject"
pipelineRunUrlPrefix="$devopsOrg/$projectId/_build/results?buildId="

# ===== SERVICE CONNECTION =====
# Create GitHub service connection.
echo "Creating GitHub service connection..."
githubServiceEndpointId=$(az devops service-endpoint github create \
    --name "$serviceConnectionName" \
    --github-url "https://www.github.com/$repoName" \
    --query id \
    --output tsv)
echo "Service connection created with ID: $githubServiceEndpointId"

# ===== PIPELINE CREATION =====
# Create the pipeline from the YAML file.
echo "Creating 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 \
    --output tsv)
echo "Pipeline created with ID: $pipelineId"

# ===== VARIABLE GROUP =====
# Create a variable group with 2 non-secret variables and 1 secret variable.
echo "Creating variable group..."
variableGroupId=$(az pipelines variable-group create \
    --name "$variableGroupName" \
    --authorize true \
    --variables a=12 b=29 \
    --query id \
    --output tsv)
echo "Variable group created with ID: $variableGroupId"

# Add a secret variable to the group.
echo "Adding secret variable to the group..."
az pipelines variable-group variable create \
    --group-id $variableGroupId \
    --name contososecret \
    --secret true \
    --value 17

# ===== PIPELINE RUNS =====
# Run the pipeline for the first time.
echo "Running pipeline (1st run)..."
pipelineRunId1=$(az pipelines run \
    --id $pipelineId \
    --query id \
    --output tsv)
echo "Pipeline run 1 started with ID: $pipelineRunId1"
echo "Go to the pipeline run's web page to view the output results of the 'Test variable group variables' job."
echo "URL: ${pipelineRunUrlPrefix}${pipelineRunId1}"
read -p "Press Enter to change the value of variable 'a', then run again:"

# Change the value of one of the variable group's nonsecret variables.
echo "Updating variable 'a'..."
az pipelines variable-group variable update \
    --group-id $variableGroupId \
    --name a \
    --value 22

# Run the pipeline for the second time.
echo "Running pipeline (2nd run)..."
pipelineRunId2=$(az pipelines run \
    --id $pipelineId \
    --query id \
    --output tsv)
echo "Pipeline run 2 started with ID: $pipelineRunId2"
echo "URL: ${pipelineRunUrlPrefix}${pipelineRunId2}"
read -p "Press Enter to change the value of the secret variable, then run once more:"

# Change the value of the variable group's secret variable.
echo "Updating secret variable 'contososecret'..."
az pipelines variable-group variable update \
    --group-id $variableGroupId \
    --name contososecret \
    --value 35

# Run the pipeline for the third time.
echo "Running pipeline (3rd run)..."
pipelineRunId3=$(az pipelines run \
    --id $pipelineId \
    --query id \
    --output tsv)
echo "Pipeline run 3 started with ID: $pipelineRunId3"
echo "URL: ${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 명령을 사용합니다.