Azure DevOps 服務 |Azure DevOps Server
在 Azure Pipelines 中管理變數對於維護 CI/CD 工作流程中的彈性和安全性至關重要。 本文說明如何使用 Azure DevOps CLI 在 Azure Pipelines 變數群組中建立並管理秘密與非秘密變數。 藉由使用變數群組,您可以集中管理變數,並確保能安全地處理敏感性資訊。
透過使用本文中的範例,你可以學會如何:
- 使用儲存在 GitHub 中的 YAML 檔案定義 Azure Pipelines 管線。
- 建立包含秘密和非Secret 變數的變數群組。
- 使用 Azure DevOps CLI 執行管線,並監控執行、處理與輸出。
注意
此範例示範 Azure DevOps CLI 與變數群組的功能。 為了提高安全性,請在 Pipelines UI 中的變數群組中定義變數,或將變數群組連結至 Azure 金鑰保存庫 中的秘密。
必要條件
在 Azure Cloud Shell 中使用 Bash 環境。 如需詳細資訊,請參閱開始使用 Azure Cloud Shell。
若要在本地執行 CLI 參考命令,請安裝 Azure CLI。 若您在 Windows 或 macOS 上執行,請考慮在 Docker 容器中執行 Azure CLI。 如需詳細資訊,請參閱〈如何在 Docker 容器中執行 Azure CLI〉。
如果您使用的是本機安裝,請使用 az login 命令,透過 Azure CLI 來登入。 請遵循您終端機上顯示的步驟,完成驗證程序。 如需其他登入選項,請參閱 使用 Azure CLI 向 Azure 進行驗證。
出現提示時,請在第一次使用時安裝 Azure CLI 延伸模組。 如需擴充功能的詳細資訊,請參閱 使用和管理 Azure CLI 的擴充功能。
執行 az version 以尋找已安裝的版本和相依程式庫。 若要升級至最新版本,請執行 az upgrade。
- 已安裝 Azure Pipelines 的 GitHub 存放庫
- 用於存取的 GitHub 個人存取令牌 (PAT)
- 具有個人存取令牌 (PAT) 用於身份驗證的 Azure DevOps 組織
- Azure DevOps 組織中的專案集合系統管理員許可權
儲存管線 YAML 檔案
請將以下 YAML 管線定義存為名為 azure-pipelines.yml 的檔案,存放在你的 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 服務連線
- 管道
- 具有兩個非機密變數和一個機密變數的變數群組
執行腳本前,請替換以下佔位符:
-
<devops-organization>你的 Azure DevOps 組織名稱。 例如,如果你的 Azure DevOps URL 是https://dev.azure.com/Contoso,請使用Contoso。 -
<github-organization>你的 GitHub 組織或使用者名稱。 例如,myusername或myorganization。 -
<github-repository>你的 GitHub 倉庫名稱。 例如,如果你的儲存庫網址是https://github.com/myusername/my-repo,請使用my-repo。 -
<pipelinename>管線的名稱,介於 3 到 19 個字元之間,且只包含數位和小寫字母。 腳本會新增五位數的唯一標識符。 例如:mypipeline。
將 GitHub PAT 儲存在本機環境中。
AZURE_DEVOPS_EXT_GITHUB_PAT=<your-github-pat>
將 YAML 檔案存入 GitHub 後,在 Azure Cloud Shell 的 Bash shell 或本地執行以下 Azure DevOps CLI 腳本。
這很重要
確保你安裝的是最新版本的 Azure CLI 和 DevOps 擴充功能。 在執行這個腳本之前,先執行 az upgrade 和 az 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 命令:
- az devops configure(用於設定 DevOps 組態的命令)
- az devops 專案創建
- az devops 專案刪除
- AZ DevOps 專案清單
- az devops service-endpoint github 建立
- AZ Login
- az 管線 創建
- az pipelines 刪除
- az pipelines 執行
- az pipelines 變數群組 create
- az pipelines 變數群組刪除
- az pipelines 變數群組變數 create
- az 管道 變數組 變數 更新