Note
Access to this page requires authorization. You can try signing in or .
Access to this page requires authorization. You can try .
Use Azure Key Vault secrets in your pipeline
Azure DevOps Services | Azure DevOps Server | Azure DevOps Server 2022
Developers can use Azure key vaults to securely store and manage sensitive information such as passwords, API keys, and certificates. This article walks you through how to query and use secrets from an Azure key vault in your pipeline.
Prerequisites
| Product | Requirements |
|---|---|
| Azure DevOps | - An Azure DevOps project. - Permissions: - To grant access to all pipelines in the project: You must be a member of the Project Administrators group. - To create service connections: You must have the Administrator or Creator role for service connections. |
| GitHub | - A GitHub account and a GitHub repository. - A GitHub service connection to authorize Azure Pipelines. |
| Azure | - An Azure subscription. |
Create a key vault
Sign in to the Azure portal, and then select Create a resource.
Under Key Vault, select Create to create a new Azure key vault.
Select your subscription from the dropdown menu, and then select an existing resource group or create a new one. Enter a key vault name, select a region, choose a pricing tier, and select Next if you want to configure more properties. Otherwise, select Review + create to keep the default settings.
After the deployment is finished, select Go to resource.
Set up authentication
Create a user-assigned managed identity
Sign in to the Azure portal, and then search for the Managed Identities service in the search bar.
Select Create, and then fill out the required fields:
- Subscription: Select your subscription from the dropdown menu.
- Resource group: Select an existing resource group or create a new one.
- Region: Select a region from the dropdown menu.
- Name: Enter a name for your user-assigned managed identity.
Select Review + create after you're finished.
After the deployment is finished, select Go to resource. Copy the subscription and client ID because you need them in the next steps.
Go to Settings > Properties, and copy your managed identity Tenant ID to use later.
Set up key vault access policies
Go to the Azure portal, and use the search bar to find the key vault that you created earlier.
Select Access policies, and then select Create to add a new policy.
Under Secret permissions, select the Get and List checkboxes.
Select Next, and then paste the client ID of the managed identity that you created earlier into the search bar.
Select your managed identity, select Next, and then Next again.
Review your new policy, and then select Create after you're finished.
Create a service connection
Sign in to your Azure DevOps organization, and then go to your project.
Select Project settings > Service connections, and then select New service connection.
Select Azure Resource Manager, and then select Next.
Under Identity Type, select Managed identity from the dropdown menu.
For Step 1: Managed identity details, fill out the fields:
- Subscription for managed identity: Select the subscription that contains your managed identity.
- Resource group for managed identity: Select the resource group where your managed identity is hosted.
- Managed identity: Select your managed identity from the dropdown menu.
For Step 2: Azure Scope, fill out the fields:
- Scope level for service connection: Select Subscription.
- Subscription for service connection: Select the subscription that your managed identity accesses.
- Resource group for service connection: (Optional) Specify this item if you want to restrict access to a specific resource group.
For Step 3: Service connection details, fill out the fields:
- Service connection name: Enter a name for your service connection.
- Service management reference: (Optional) Include context information from an ITSM database.
- Description: (Optional) Add a description.
Under Security, select the Grant access permission to all pipelines checkbox to allow all pipelines to use this service connection. If you leave this checkbox blank, you need to manually grant access for each pipeline.
Select Save to validate and create the service connection.
👁 Screenshot that shows how to create a managed identity Azure Resource Manager service connection.
Query and use secrets in your pipeline
With the Azure Key Vault task, you can now query and fetch secrets from your key vault and use them in subsequent tasks in your pipeline. Secrets must be explicitly mapped to environment variables, as shown in the following example:
pool:
vmImage: 'ubuntu-latest'
steps:
- task: AzureKeyVault@2 # Download Azure Key Vault secrets.
inputs:
azureSubscription: 'SERVICE_CONNECTION_NAME' # Name of the service connection. Alias: ConnectedServiceName.
KeyVaultName: 'KEY_VAULT_NAME' # Name of the key vault.
SecretsFilter: '*' # Secrets filter. Default: *.
- bash: |
echo "Secret Found! $MY_MAPPED_ENV_VAR"
env:
MY_MAPPED_ENV_VAR: $(SECRET_NAME)
The output from the last Bash step should look like this example:
Secret Found! ***
Note
To query multiple secrets from your key vault, use the SecretsFilter input and provide a comma-separated list of secret names, like 'secret1, secret2'.
Related content
Feedback
Was this page helpful?
