You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you want to use Azure DevOps CLI with a YAML pipeline, use the following examples to install Azure CLI, add the Azure DevOps extension, and run Azure DevOps CLI commands.
23
+
If you want to use Azure DevOps CLI with a YAML pipeline, you can use the Azure DevOps extension or use the [AzureCLI task](/azure/devops/pipelines/tasks/reference/azure-cli-v2). The Microsoft-hosted Windows and Linux agents are preconfigured with Azure CLI and the Azure DevOps CLI extension. The Azure DevOps CLI extension runs `az devops`commands.
21
24
22
-
> [!NOTE]
23
-
> The steps in this article show how to authenticate with Azure DevOps and run `az devops` commands using the Azure DevOps CLI extension. If you want to use Azure CLI to interact with Azure resources, use the [AzureCLI task](/azure/devops/pipelines/tasks/reference/azure-cli-v2).
25
+
You need to use a PAT with the Azure CLI extension in a pipeline. For added security, use the use the [AzureCLI task](/azure/devops/pipelines/tasks/reference/azure-cli-v2) with a service connection.
26
+
27
+
::: zone pivot="pat"
24
28
25
29
## Authenticate with Azure DevOps
26
30
27
31
Some Azure DevOps CLI commands, like `az devops configure` and `az devops --help`, don't require any authentication. They don't connect into Azure DevOps. Most commands interact with Azure DevOps and do require authentication.
28
32
29
33
You can authenticate using the [System.AccessToken](../pipelines/build/variables.md#systemaccesstoken) security token used by the running pipeline, by assigning it to an environment variable named `AZURE_DEVOPS_EXT_PAT`, as shown in the following example.
30
34
35
+
Using `System.AccessToken` relies on having a PAT. As a more secure alternative, you can use the AzureCLI@2 task to populate a service connection.
36
+
31
37
# [Bash](#tab/bash)
32
38
33
39
```yml
@@ -160,7 +166,6 @@ You can upgrade the Azure CLI on your hosted images by running the following com
160
166
- pwsh: pip install --pre azure-cli
161
167
displayName: 'Upgrade Azure CLI'
162
168
```
163
-
164
169
---
165
170
166
171
## Conditionally install the Azure DevOps CLI extension
@@ -472,9 +477,110 @@ steps:
472
477
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
473
478
displayName: 'List variables in Fabrikam-2023 variable group'
474
479
```
475
-
476
480
---
477
481
482
+
::: zone-end
483
+
484
+
::: zone pivot="service-connection"
485
+
486
+
487
+
## Authenticate with a service connection
488
+
489
+
When you use a service connection, the service connection provides the necessary credentials for Azure CLI and Azure DevOps CLI commands in the AzureCLI@2 task without requiring manual credential management in the pipeline.
490
+
491
+
> [!NOTE]
492
+
> When you use a service connection for authentication with `AzureCLI@2`, you need to [manually add the service principal to your Azure DevOps organization](../integrate/get-started/authentication/service-principal-managed-identity.md#2-add-a-service-principal-to-an-azure-devops-organization).
493
+
494
+
This code sample defines a new parameter, `serviceConnection`, with the name of an existing service connection. That parameter is referenced in the `AzureCLI@2` task. The task lists all projects (`az devops project list`) and pools (`az pipelines pool list`).
az devops project list --query "value[].{Name:name, Id:id}" `
523
+
-o table
524
+
525
+
Write-Host "`nUse Azure DevOps CLI (az pipelines) to list pools in the organization '$(System.CollectionUri)'..."
526
+
Write-Host "$($PSStyle.Formatting.FormatAccent)az pipelines pool list$($PSStyle.Reset)"
527
+
az pipelines pool list --query "[].{Id:id, Name:name}" `
528
+
-o table
529
+
failOnStandardError: true
530
+
```
531
+
532
+
## Assign the results of an Azure DevOps CLI call to a variable
533
+
534
+
To store the results of an Azure DevOps CLI call to a pipeline variable, use the `task.setvariable` syntax described in [Set variables in scripts](../pipelines/process/variables.md#set-variables-in-scripts). The following example gets the ID of a variable group named **Fabrikam-2023** and uses this value in a subsequent step.
az pipelines variable-group variable list --group-id '$(variableGroupId)'
579
+
```
580
+
581
+
582
+
::: zone-end
583
+
478
584
For more examples of working with variables, including working with variables across jobs and stages, see [Define variables](../pipelines/process/variables.md). For examples of the query syntax used in the previous example, see [How to query Azure CLI command output using a JMESPath query](/cli/azure/query-azure-cli).
0 commit comments