共用方式為


使用 Azure DevOps CLI 管理變數群組中的變數

Azure DevOps Services

在 Azure Pipelines 中管理變數對於維護 CI/CD 工作流程中的彈性和安全性至關重要。 本指南示範如何使用 Azure DevOps CLI 在 Azure Pipelines 變數群組內建立和管理秘密和非秘密變數。 藉由使用變數群組,您可以集中管理變數,並確保能安全地處理敏感性資訊。

使用本指南中的範例,您將瞭解如何:

  • 使用儲存在 GitHub 中的 YAML 檔案定義 Azure Pipelines 管線。
  • 建立包含秘密和非Secret 變數的變數群組。
  • 使用 Azure DevOps CLI 執行管線,並監視執行處理和輸出。

注意

此範例會示範 Azure DevOps CLI 與變數群組的功能。 為了提高安全性,請在 Pipelines UI 中的變數群組中定義變數,將變數群組連結至 Azure 金鑰保存庫 中的秘密。

必要條件

儲存管線 YAML 檔案

將下列 YAML 管線定義儲存為名為 azure-pipelines.yml 的檔案,並儲存在 GitHub 存放庫的根目錄和 main 分支中。

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 服務連線
  • 管道
  • 具有兩個非secret 變數和一個秘密變數的變數群組

執行腳本之前,請取代下列佔位元,如下所示:

  • <devops-organization> 您的 Azure DevOps 組織名稱
  • <github-organization> 您的 GitHub 組織或用戶名稱
  • <github-repository> 您的 GitHub 存放庫名稱
  • <pipelinename> 管線的名稱,介於 3 到 19 個字元之間,且只包含數位和小寫字母。 腳本會新增五位數的唯一標識符。

將 GitHub PAT 儲存在本機環境中。

AZURE_DEVOPS_EXT_GITHUB_PAT=<your-github-pat>

將 YAML 檔案儲存在 GitHub 之後,請在 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 命令: