Skip to content

Commit ac7ae47

Browse files
Merge pull request #8102 from MicrosoftDocs/users/jukullam/cli-workloadidentity
Add service connection option for CLI connection
2 parents 8171d47 + 14f7a44 commit ac7ae47

File tree

2 files changed

+120
-6
lines changed

2 files changed

+120
-6
lines changed

docs/cli/azure-devops-cli-in-yaml.md

Lines changed: 112 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,31 @@ ms.manager: mijacobs
99
ms.author: chcomley
1010
author: chcomley
1111
monikerRange: 'azure-devops'
12-
ms.date: 05/30/2025
12+
ms.date: 07/07/2025
13+
zone_pivot_groups: configure-cli
14+
1315
#customer intent: As a team member, I want to use YAML configuration files to manage my pipeline tasks by using Azure DevOps CLI.
1416
---
1517

1618
# Azure DevOps CLI in Azure Pipeline YAML
1719

20+
1821
[!INCLUDE [version-eq-azure-devops](../includes/version-eq-azure-devops.md)]
1922

20-
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.
2124

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"
2428

2529
## Authenticate with Azure DevOps
2630

2731
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.
2832

2933
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.
3034

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+
3137
# [Bash](#tab/bash)
3238

3339
```yml
@@ -160,7 +166,6 @@ You can upgrade the Azure CLI on your hosted images by running the following com
160166
- pwsh: pip install --pre azure-cli
161167
displayName: 'Upgrade Azure CLI'
162168
```
163-
164169
---
165170

166171
## Conditionally install the Azure DevOps CLI extension
@@ -472,9 +477,110 @@ steps:
472477
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
473478
displayName: 'List variables in Fabrikam-2023 variable group'
474479
```
475-
476480
---
477481

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`).
495+
496+
```yml
497+
trigger:
498+
- main
499+
500+
parameters:
501+
- name: serviceConnection
502+
displayName: Azure Service Connection Name
503+
type: string
504+
default: my-service-connection
505+
506+
steps:
507+
- task: AzureCLI@2
508+
condition: succeededOrFailed()
509+
displayName: 'Azure CLI -> DevOps CLI'
510+
inputs:
511+
azureSubscription: '${{ parameters.serviceConnection }}'
512+
scriptType: pscore
513+
scriptLocation: inlineScript
514+
inlineScript: |
515+
Write-Host "Using logged-in Azure CLI session..."
516+
Write-Host "$($PSStyle.Formatting.FormatAccent)az devops configure$($PSStyle.Reset)"
517+
az devops configure --defaults organization=$(System.CollectionUri) project=$(System.TeamProject)
518+
az devops configure -l
519+
520+
Write-Host "`nUse Azure DevOps CLI (az devops) to list projects in the organization '$(System.CollectionUri)'..."
521+
Write-Host "$($PSStyle.Formatting.FormatAccent)az devops project list$($PSStyle.Reset)"
522+
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.
535+
536+
537+
538+
539+
```yml
540+
# Install Azure DevOps extension
541+
trigger:
542+
- main
543+
544+
variables:
545+
- name: variableGroupId
546+
547+
parameters:
548+
- name: serviceConnection
549+
displayName: Azure Service Connection Name
550+
type: string
551+
default: my-service-connection
552+
553+
steps:
554+
- task: AzureCLI@2
555+
condition: succeededOrFailed()
556+
displayName: 'Azure CLI -> DevOps CLI'
557+
inputs:
558+
azureSubscription: '${{ parameters.serviceConnection }}'
559+
scriptType: pscore
560+
scriptLocation: inlineScript
561+
inlineScript: |
562+
Write-Host "Using logged-in Azure CLI session..."
563+
Write-Host "$($PSStyle.Formatting.FormatAccent)az devops configure$($PSStyle.Reset)"
564+
az devops configure --defaults organization=$(System.CollectionUri) project=$(System.TeamProject)
565+
az devops configure -l
566+
567+
##vso[task.setvariable variable=variableGroupId]$(az pipelines variable-group list --group-name kubernetes --query [].id -o tsv)"
568+
569+
- task: AzureCLI@2
570+
condition: succeededOrFailed()
571+
displayName: 'Azure CLI -> DevOps CLI'
572+
inputs:
573+
azureSubscription: '${{ parameters.serviceConnection }}'
574+
scriptType: pscore
575+
scriptLocation: inlineScript
576+
inlineScript: |
577+
Write-Host "Using logged-in Azure CLI session..."
578+
az pipelines variable-group variable list --group-id '$(variableGroupId)'
579+
```
580+
581+
582+
::: zone-end
583+
478584
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).
479585

480586
## Related content

docs/zone-pivot-groups.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,11 @@ groups:
5656
title: Standalone
5757
- id: bundled-ghazdo
5858
title: Bundled
59+
- id: configure-cli
60+
title: Configure Azure CLI
61+
prompt: Choose your Azure CLI authentication method
62+
pivots:
63+
- id: pat
64+
title: PAT
65+
- id: service-connection
66+
title: Service Connection

0 commit comments

Comments
 (0)