Använda Azure Devops CLI för att hantera variabler i en variabelgrupp
Azure DevOps Services | Azure DevOps Server 2022 | Azure DevOps Server 2020
Det här exemplet använder az devops
Azure DevOps-tillägget till Azure CLI för att skapa och köra en Azure-pipeline som har åtkomst till en variabelgrupp som innehåller både hemliga och icke-sekretiska variabler.
Skriptet visar följande tre åtgärder:
- Definierar en Azure Pipelines-pipeline med hjälp av en YAML-fil som lagras i GitHub.
- Skapar en variabelgrupp med icke-säkerhets- och hemliga variabler för användning i pipelinen.
- Kör pipelinen med hjälp av Azure DevOps CLI och visar bearbetning och utdata för pipelinekörningar.
Förutsättningar
Använd Bash-miljön i Azure Cloud Shell. Mer information finns i Snabbstart för Bash i Azure Cloud Shell.
Om du föredrar att köra CLI-referenskommandon lokalt installerar du Azure CLI. Om du kör i Windows eller macOS kan du köra Azure CLI i en Docker-container. Mer information finns i Så här kör du Azure CLI i en Docker-container.
Om du använder en lokal installation loggar du in på Azure CLI med hjälp av kommandot az login. Slutför autentiseringsprocessen genom att följa stegen som visas i terminalen. Andra inloggningsalternativ finns i Logga in med Azure CLI.
När du uppmanas att installera Azure CLI-tillägget vid första användningen. Mer information om tillägg finns i Använda tillägg med Azure CLI.
Kör az version om du vill hitta versionen och de beroende bibliotek som är installerade. Om du vill uppgradera till den senaste versionen kör du az upgrade.
Om du vill köra exemplet behöver du:
- En GitHub-lagringsplats med Azure Pipelines installerat
- En personlig åtkomsttoken för GitHub (PAT) för åtkomst
- En Azure DevOps-organisation med en personlig åtkomsttoken (PAT) för autentisering
- Administratörsbehörigheter för projektsamling i Azure DevOps-organisationen
Spara exempelfilen
Spara följande YAML-pipelinedefinition som en fil med namnet azure-pipelines.yml i rotkatalogen och main
grenen av din GitHub-lagringsplats.
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)
Kör exemplet
När du har lagrat YAML-filen i GitHub kör du följande Azure DevOps CLI-skript i ett Bash-gränssnitt i Cloud Shell eller lokalt. Skriptet skapar variablerna pipeline, variabelgrupp och hemlighet och icke-sekret och ändrar sedan variabelvärdena.
Innan du kör skriptet ersätter du följande platshållare på följande sätt:
<devops-organization>
Ditt Azure DevOps-organisationsnamn<github-organization>
Ditt GitHub-organisationsnamn<github-repository>
Ditt GitHub-lagringsplatsnamn<github-pat>
Din GitHub PAT
Ersätt följande platshållare med värden som du väljer:
<pipelinename>
Ett namn på pipelinen som är mellan 3 och 19 tecken och som endast innehåller siffror och gemener. Skriptet lägger till en 5-siffrig unik identifierare.<azure-resource-group-location>
Azure-region för resursgruppen som innehåller Azure Storage-kontot, till exempeleastus
.<azure-storage-account-location>
Samma som resursgruppens plats.
#!/bin/bash
# Provide placeholder variables.
devopsOrg="https://dev.azure.com/<devops-organization>"
githubOrg="<github-organization>"
githubRepo="<github-repository>"
githubPat="<github-pat>"
pipelineName="<pipelinename>"
resourceGroupLocation="<azure-resource-group-location>"
storageAccountLocation="<azure-storage-account-location>"
repoName="$githubOrg/$githubRepo"
repoType="github"
branch="main"
# Declare other variables.
uniqueId=$RANDOM
resourceGroupName="$pipelineName$uniqueId"
storageAccountName="$pipelineName$uniqueId"
devopsProject="Contoso DevOps Project $uniqueId"
serviceConnectionName="Contoso Service Connection $uniqueId"
variableGroupName="Contoso Variable Group"
# 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 a resource group and a storage account.
az group create --name "$resourceGroupName" --location "$resourceGroupLocation"
az storage account create --name "$storageAccountName" \
--resource-group "$resourceGroupName" --location "$storageAccountLocation"
# Set the environment variable used for Azure DevOps token authentication.
export AZURE_DEVOPS_EXT_GITHUB_PAT=$githubPat
# 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 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 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 for the 3rd run."
echo "If the web page doesn't automatically appear, go to:"
echo " ${pipelineRunUrlPrefix}${pipelineRunId3}"
read -p "Press Enter to continue:"
Rensa resurser
För att undvika kostnader för Azure-lagring när du har kört skriptexemplet kan du ta bort Azure-resursgruppen och alla dess resurser genom att köra följande skript:
az pipelines variable-group delete --group-id $variableGroupId --yes
az pipelines delete --id $pipelineId --yes
az devops service-endpoint delete --id $githubServiceEndpointId --yes
az devops project delete --id $projectId --yes
export AZURE_DEVOPS_EXT_GITHUB_PAT=""
az storage account delete --name $storageAccountName --resource-group $resourceGroupName --yes
az group delete --name $resourceGroupName --yes
az devops configure --defaults organization="" project=""
Azure CLI-referenser
Exemplet i den här artikeln använder följande Azure CLI-kommandon:
- az devops configure
- az devops project create
- az devops project delete
- az devops service-endpoint github create
- az group create
- az group delete
- az login
- az pipelines create
- az pipelines delete
- az pipelines run
- az pipelines variable-group create
- az pipelines variable-group delete
- az pipelines variable-group variable create
- az pipelines variable-group variable update
- az storage account create
- az storage account delete
Feedback
https://aka.ms/ContentUserFeedback.
Kommer snart: Under hela 2024 kommer vi att fasa ut GitHub-problem som feedbackmekanism för innehåll och ersätta det med ett nytt feedbacksystem. Mer information finns i:Skicka och visa feedback för