How to Use Service Connection in Azure DevOps: Simple Guide
In Azure DevOps, use a
service connection to securely link your project pipelines to external services like Azure subscriptions or GitHub. Create the service connection in project settings, then reference it in your pipeline YAML or classic pipeline tasks to authenticate and access those services.Syntax
A service connection in Azure DevOps is created in the project settings under Service connections. It requires specifying the connection type (e.g., Azure Resource Manager), authentication method (like service principal), and scope (subscription or resource group).
In pipeline YAML, reference the service connection using the azureSubscription property inside tasks that need it.
yaml
resources: connections: - connection: 'MyAzureConnection' type: 'azurerm' steps: - task: AzureCLI@2 inputs: azureSubscription: 'MyAzureConnection' scriptType: 'bash' scriptLocation: 'inlineScript' inlineScript: | az group list
Example
This example shows how to create a service connection for Azure Resource Manager and use it in a pipeline to list resource groups.
yaml
trigger: - main pool: vmImage: 'ubuntu-latest' steps: - task: AzureCLI@2 inputs: azureSubscription: 'MyAzureServiceConnection' scriptType: 'bash' scriptLocation: 'inlineScript' inlineScript: | az group list --output table
Output
Name Location Status
---------------- ---------- --------
myResourceGroup eastus Succeeded
Common Pitfalls
- Not granting the service principal enough permissions in Azure causes authentication failures.
- Using the wrong service connection name in the pipeline YAML leads to errors.
- For classic pipelines, forgetting to select the service connection in task settings blocks access.
- Not updating expired credentials in the service connection causes pipeline failures.
yaml
steps:
- task: AzureCLI@2
inputs:
azureSubscription: 'WrongConnectionName' # Incorrect name causes failure
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
az group list
# Correct usage:
steps:
- task: AzureCLI@2
inputs:
azureSubscription: 'MyAzureServiceConnection' # Correct name
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
az group listQuick Reference
| Step | Description |
|---|---|
| Create Service Connection | Go to Project Settings > Service connections > New service connection and select type. |
| Configure Authentication | Provide credentials like service principal or PAT with correct permissions. |
| Use in Pipeline | Reference the service connection name in pipeline tasks using azureSubscription or serviceConnection property. |
| Maintain Credentials | Regularly update credentials to avoid pipeline failures. |
Key Takeaways
Create a service connection in Azure DevOps project settings to securely connect external services.
Reference the service connection by name in your pipeline tasks to authenticate actions.
Ensure the service principal or credentials have proper permissions in the target service.
Use exact service connection names in YAML or classic pipelines to avoid errors.
Keep credentials updated to prevent pipeline authentication failures.