次の方法で共有


Azure DevOps CLI を使用して変数グループ内の変数を管理する

Azure DevOps Services |Azure DevOps Server

CI/CD ワークフローの柔軟性とセキュリティを維持するには、Azure Pipelines での変数の管理が不可欠です。 この記事では、Azure DevOps CLI を使用して、Azure Pipelines 変数グループ内でシークレット変数と非セキュリティ変数の両方を作成および管理する方法について説明します。 変数グループを使用すると、変数の管理を一元化して、機密情報を安全に処理できます。

この記事のサンプルを使用して、次の方法を学習します。

  • GitHub に格納されている YAML ファイルを使用して、Azure Pipelines パイプラインを定義する。
  • シークレット変数と非セキュリティ変数の両方を含む変数グループを作成する。
  • Azure DevOps CLI を使用してパイプラインを実行し、実行の処理と出力を監視します。

Note

このサンプルでは、変数グループを使用した Azure DevOps CLI の機能を示します。 セキュリティを強化するには、Pipelines UI の変数グループで変数を定義するか、変数グループを Azure Key Vault のシークレットにリンクしてください。

前提条件

  • 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 を実行します。

パイプライン YAML ファイルを保存する

次の YAML パイプライン定義を、GitHub リポジトリ ルート ディレクトリと main ブランチに azure-pipelines.yml という名前のファイルとして保存します。

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 リソースを作成します
  • パイプラインを実行します
  • 変数値を 3 回変更します
  • 変数値が変更されるたびにパイプラインを再度実行します

このスクリプトは、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 スクリプトを実行します。

Important

最新バージョンの 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 コマンドを使用しています。