Freigeben über


Verwalten von Variablen in Variablengruppen mit der Azure DevOps CLI

Azure DevOps Services | Azure DevOps Server

Das Verwalten von Variablen in Azure Pipelines ist ausschlaggebend für die Aufrechterhaltung von Flexibilität und Sicherheit in Ihren CI/CD-Workflows. In diesem Artikel wird gezeigt, wie Sie die Azure DevOps CLI verwenden, um geheime und nichtsecret-Variablen innerhalb einer Variablengruppe von Azure Pipelines zu erstellen und zu verwalten. Mithilfe von Variablengruppen können Sie die Verwaltung von Variablen zentralisieren und sicherstellen, dass vertrauliche Informationen sicher behandelt werden.

Mithilfe des Beispiels in diesem Artikel erfahren Sie, wie Sie:

  • Definieren Sie eine Azure-Pipeline mithilfe einer YAML-Datei, die in GitHub gespeichert ist.
  • Erstellen Sie eine Variablengruppe, die sowohl geheime als auch nicht geheime Variablen enthält.
  • Führen Sie die Pipeline mithilfe der Azure DevOps CLI aus und überwachen Sie den Ausführungsprozess und die Ausgabe.

Hinweis

In diesem Beispiel wird die Funktionalität der Azure DevOps CLI mit variablen Gruppen veranschaulicht. Um die Sicherheit zu erhöhen, definieren Sie Variablen in Variablengruppen in der Pipelines-Benutzeroberfläche, oder verknüpfen Sie eine variable Gruppe mit geheimen Schlüsseln in Azure Key Vault.

Voraussetzungen

Speichern der YaML-Pipelinedatei

Speichern Sie die folgende YAML-Pipelinedefinition als Datei namens azure-pipelines.yml im Stammverzeichnis und main Zweig Ihres GitHub-Repositorys.

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)

Das Beispiel-Skript

Dieses Beispielskript führt die folgenden Aufgaben aus:

  • Erstellt die DevOps-Ressourcen
  • Führt die Pipeline aus.
  • Ändert die Variablenwerte dreimal.
  • Führt bei jeder Änderung der Variablenwerte die Pipeline erneut aus.

Das Skript erstellt die folgenden Ressourcen in Azure DevOps:

  • Ein Projekt in Ihrer DevOps-Organisation
  • Eine GitHub-Dienstverbindung
  • Eine Pipeline
  • Eine Variablengruppe mit zwei nicht geheimen Variablen und einer geheimen Variablen

Ersetzen Sie vor dem Ausführen des Skripts die folgenden Platzhalter:

  • <devops-organization> Name Ihrer Azure DevOps-Organisation. Wenn Ihre Azure DevOps-URL beispielsweise lautet https://dev.azure.com/Contoso, verwenden Sie Contoso.
  • <github-organization> Ihre GitHub-Organisation oder Ihr Benutzername. Zum Beispiel: myusername oder myorganization.
  • <github-repository> Ihr GitHub-Repositoryname. Wenn ihre Repository-URL beispielsweise lautet https://github.com/myusername/my-repo, verwenden Sie my-repo.
  • <pipelinename> Ein Name für die Pipeline, der 3 bis 19 Zeichen lang ist und nur Ziffern und Kleinbuchstaben enthält. Das Skript fügt einen fünfstelligen eindeutigen Bezeichner hinzu. Beispiel: mypipeline.

Speichern Sie Ihren GitHub-PAT in der lokalen Umgebung.

AZURE_DEVOPS_EXT_GITHUB_PAT=<your-github-pat>

Führen Sie nach dem Speichern der YAML-Datei in GitHub das folgende Azure DevOps CLI-Skript in einer Bash-Shell in Azure Cloud Shell oder lokal aus.

Von Bedeutung

Stellen Sie sicher, dass Die neueste Version der Azure CLI und die DevOps-Erweiterung installiert ist. Führen Sie az upgrade und az extension add --name azure-devops aus, bevor Sie dieses Skript ausführen.

#!/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:"

Bereinigen von Ressourcen

Um Kosten für das Azure-Projekt zu vermeiden, löschen Sie das Beispielprojekt. Diese Aktion löscht auch seine Ressource.

Kopieren Sie id des Beispielprojekt aus der Ausgabe des folgenden Befehls:

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

Löschen Sie das Projekt, indem Sie den folgenden Befehl ausführen:

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

Bereinigen Sie Ihre lokale Umgebung, indem Sie die folgenden Befehle ausführen:

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

Azure CLI-Referenzen

Im Beispiel in diesem Artikel werden die folgenden Azure CLI-Befehle verwendet: