I know it sounds convoluted by bear with me:
I define a secret variable
- name: SECRET_VAR
value: dev-secret
which I can use to get the secret from a keyvault by its name (dev-secret)
- task: AzureKeyVault@2
displayName: Load Key Vault Secrets
inputs:
azureSubscription: $(SUBSCRIPTION_SERVICE_ACCOUNT)
KeyVaultName: $(KEYVAULT_NAME)
SecretsFilter: $(SECRET_VAR)
RunAsPreJob: true
I now want to use the secret in subsequent jobs with
- task: Bash@3
name : getToken
inputs:
targetType: 'inline'
script: |
echo "##vso[task.setvariable variable=myToken;isOutput=true]$(SECRET_VAR)"
and this obviously does not work as it evaluates to the dev-secret string but not the secret itself..
For sure I can use it with
echo "##vso[task.setvariable variable=myToken;isOutput=true]$(dev-secret)"
But since I have multiple pipelines that have to get different secrets I need to make it dynamic.
how can I achieve this?
2
Answers
You should set the "SecretFilter" to ‘*’, and prepare serveral secrets in azure keyvault. I have added two as my sample:
and below is my code, just apply to your own parameters and run. it will achieve your dynamic tokens.
As it’s key valut secret, which will NOT mapped to environment. You can use
$(${{ variables.SECRET_VAR }})
in the logging command, and invoke in the next job.My yaml below:
Check in Job A:
test1.txt:
Check in Job B test2.txt: