Zarządzanie zmiennymi w grupie zmiennych za pomocą interfejsu wiersza polecenia usługi Azure Devops
Azure DevOps Services | Azure DevOps Server 2022 | Azure DevOps Server 2020
W tym przykładzie użyto az devops
rozszerzenia Usługi Azure DevOps do interfejsu wiersza polecenia platformy Azure w celu utworzenia i uruchomienia potoku platformy Azure, który uzyskuje dostęp do grupy zmiennych zmiennych tajnych i niesekretowych.
Skrypt demonstruje następujące trzy operacje:
- Definiuje potok usługi Azure Pipelines przy użyciu pliku YAML przechowywanego w usłudze GitHub.
- Tworzy grupę zmiennych z niezabezpieczonymi i tajnymi zmiennymi do użycia w potoku.
- Uruchamia potok przy użyciu interfejsu wiersza polecenia usługi Azure DevOps i pokazuje przetwarzanie i dane wyjściowe przebiegu potoku.
Wymagania wstępne
Użyj środowiska powłoki Bash w usłudze Azure Cloud Shell. Aby uzyskać więcej informacji, zobacz Szybki start dotyczący powłoki Bash w usłudze Azure Cloud Shell.
Jeśli wolisz uruchamiać polecenia referencyjne interfejsu wiersza polecenia lokalnie, zainstaluj interfejs wiersza polecenia platformy Azure. Jeśli korzystasz z systemu Windows lub macOS, rozważ uruchomienie interfejsu wiersza polecenia platformy Azure w kontenerze Docker. Aby uzyskać więcej informacji, zobacz Jak uruchomić interfejs wiersza polecenia platformy Azure w kontenerze platformy Docker.
Jeśli korzystasz z instalacji lokalnej, zaloguj się do interfejsu wiersza polecenia platformy Azure za pomocą polecenia az login. Aby ukończyć proces uwierzytelniania, wykonaj kroki wyświetlane w terminalu. Aby uzyskać inne opcje logowania, zobacz Logowanie się przy użyciu interfejsu wiersza polecenia platformy Azure.
Po wyświetleniu monitu zainstaluj rozszerzenie interfejsu wiersza polecenia platformy Azure podczas pierwszego użycia. Aby uzyskać więcej informacji na temat rozszerzeń, zobacz Korzystanie z rozszerzeń w interfejsie wiersza polecenia platformy Azure.
Uruchom polecenie az version, aby znaleźć zainstalowane wersje i biblioteki zależne. Aby uaktualnić do najnowszej wersji, uruchom polecenie az upgrade.
Aby uruchomić przykład, potrzebne są następujące elementy:
- Repozytorium GitHub z zainstalowaną usługą Azure Pipelines
- Osobisty token dostępu w usłudze GitHub na potrzeby dostępu
- Organizacja usługi Azure DevOps z osobistym tokenem dostępu (PAT) na potrzeby uwierzytelniania
- Uprawnienia administratora kolekcji projektów w organizacji usługi Azure DevOps
Zapisywanie przykładowego pliku
Zapisz następującą definicję potoku YAML jako plik o nazwie azure-pipelines.yml w katalogu głównym i main
gałęzi repozytorium GitHub.
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)
Uruchamianie aplikacji przykładowej
Po przechowywaniu pliku YAML w usłudze GitHub uruchom następujący skrypt interfejsu wiersza polecenia usługi Azure DevOps w powłoce powłoki Bash w usłudze Cloud Shell lub lokalnie. Skrypt tworzy potok, grupę zmiennych oraz zmienne tajne i niesekret, a następnie modyfikuje wartości zmiennych.
Przed uruchomieniem skryptu zastąp następujące symbole zastępcze:
<devops-organization>
Nazwa organizacji usługi Azure DevOps<github-organization>
Nazwa organizacji usługi GitHub<github-repository>
Nazwa repozytorium GitHub<github-pat>
Twój osobisty token dostępu do usługi GitHub
Zastąp następujące symbole zastępcze wybranymi wartościami:
<pipelinename>
Nazwa potoku, który zawiera od 3 do 19 znaków i zawiera tylko cyfry i małe litery. Skrypt dodaje 5-cyfrowy unikatowy identyfikator.<azure-resource-group-location>
Region platformy Azure dla grupy zasobów, która zawiera konto usługi Azure Storage, na przykładeastus
.<azure-storage-account-location>
Tak samo jak lokalizacja grupy zasobów.
#!/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:"
Czyszczenie zasobów
Aby uniknąć naliczania opłat za usługę Azure Storage po uruchomieniu przykładowego skryptu, możesz usunąć grupę zasobów platformy Azure i wszystkie jej zasoby, uruchamiając następujący skrypt:
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=""
Dokumentacja interfejsu wiersza polecenia platformy Azure
W przykładzie w tym artykule są używane następujące polecenia interfejsu wiersza polecenia platformy Azure:
- 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-group create
- az pipelines variable-group variable-group update
- az storage account create
- az storage account delete