title | description | ms.topic | ms.date | monikerRange |
---|---|---|---|---|
Set secret variables |
Learn how to set secret variables. |
conceptual |
11/19/2024 |
azure-devops || >= azure-devops-2019 |
[!INCLUDE version-gt-eq-2020]
Secret variables are encrypted variables that you can use in pipelines without exposing their value. Secret variables can be used for private information like passwords, IDs, and other identifying data that you wouldn't want exposed in a pipeline. Secret variables are encrypted at rest with a 2048-bit RSA key and are available on the agent for tasks and scripts to use.
The recommended ways to set secret variables are in the UI, in a variable group, and in a variable group from Azure Key Vault. You can also set secret variables in a script with a logging command but this method isn't recommended since anyone who can access your pipeline can also see the secret.
Secret variables set in the pipeline settings UI for a pipeline are scoped to the pipeline where they're set. You can use variable groups to share secret variables across pipelines.
You can set secret variables in the pipeline editor when you're editing an individual pipeline. You encrypt and make a pipeline variable secret by selecting the lock icon.
You set secret variables the same way for YAML and Classic.
[!INCLUDE set secret variable in UI]
You need to map secret variable as environment variables to reference them in YAML pipelines. In this example, there are two secret variables defined in the UI, SecretOne
and SecretTwo
. The value of SecretOne
is foo
and the value of SecretTwo
is bar
.
steps:
- powershell: |
Write-Host "My first secret variable is $env:FOO_ONE"
$env:FOO_ONE -eq "foo"
env:
FOO_ONE: $(SecretOne)
- bash: |
echo "My second secret variable: $FOO_TWO"
if [ "$FOO_TWO" = "bar" ]; then
echo "Strings are equal."
else
echo "Strings are not equal."
fi
env:
FOO_TWO: $(SecretTwo)
The pipeline outputs:
My first secret variable is ***
True
My second secret variable: ***
Strings are equal.
[!INCLUDE secrets masked]
For a more detailed example, see Define variables.
Unlike a normal variable, secret aren't automatically decrypted into environment variables for scripts. You need to explicitly map secret variables.
In this example, the variable mySecret
is set on the Variables tab. The value of mySecret
is foo
.
:::image type="content" source="media/variables/set-secret-var-classic.png" alt-text="Screenshot of setting a secret variable in Classic. ":::
Each task that needs to use the secret as an environment variable does remapping. If you want to use the secret variable mySecret
in a script, use the Environment Variables section of the task. Set the environment variable name to FOO_ONE
, and set the value to $(mySecret)
.
:::image type="content" source="media/variables/secret-passed-variable-classic.png" alt-text="Screenshot of mapped secret environment variable in Classic.":::
The script outputs True
.
You can add secrets to a variable group or link secrets from an existing Azure Key Vault.
-
Select Pipelines > Library > + Variable group.
:::image type="content" source="../library/media/add-variable-group.png" alt-text="Screenshot of Add variable group button highlighted with red box.":::
-
Enter a name and description for the group.
-
Optional: Move the toggle to link secrets from an Azure key vault as variables. For more information, see Use Azure Key Vault secrets.
-
Enter the name and value for each variable to include in the group, choosing + Add for each one.
-
To make your variable secure, choose the "lock" icon at the end of the row.
-
When you're finished adding variables, select Save.
Variable groups follow the library security model.
You can create variable groups and link them to an existing Azure key vault, allowing you to map to secrets stored in the key vault. Only the secret names are mapped to the variable group, not the secret values. Pipeline runs that link to the variable group fetch the latest secret values from the vault. For more information, see Link a variable group to secrets in Azure Key Vault.
You can use the Azure Key Vault task to include secrets in your pipeline. This task allows the pipeline to connect to your Azure Key Vault and retrieve secrets to use as pipeline variables.
-
In the pipeline editor, select Show assistant to expand the assistant panel.
-
Search for
vault
and select the Azure Key Vault task.:::image type="content" source="../release/media/azure-key-vault/configure-azure-key-vault-task.png" alt-text="Add the Azure Key Vault task.":::
The Make secrets available to whole job option isn't currently supported in Azure DevOps Server 2019 and 2020.
To learn more about the Azure Key Vault task, see Use Azure Key Vault secrets in Azure Pipelines.
You can use the task.setvariable
logging command to set variables in PowerShell and Bash scripts. This method is the least secure way to work with secret variables but can be useful for debugging. The recommended ways to set secret variables are in the UI, in a variable group, and in a variable group from Azure Key Vault.
To set a variable as a script with a logging command, you need to pass the issecret
flag.
[!INCLUDE set secret variable in UI]
Learn more about setting and using variables in scripts.