-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tokenize-ps3.ps1 does not work with environment variables #99
Comments
update: never mind, it was a red herring in my case... I think I've hit the same problem. |
This is an old thread; but the repository has not been abandoned yet and is one of the top results in Google. They appear to have subtly changed how the environment variables are grabbed and this has changed the behavior of this script. In
This has the unfortunate side effect of only grabbing the Job Variables as per the documentation for You can see this behavior by setting up this toy pipeline: sourcefile.json {
"FirstName": "__FIRST_NAME__",
"Unknown": "__UNKNOWN_VARIABLE__"
} azure-pipeline.yml trigger: none
pool:
vmImage: windows-latest
variables: # These are Job Variables
- name: FIRST_NAME
value: Luke
steps:
- task: CmdLine@2
displayName: 'Echo Out the Source File'
inputs:
script: |
type sourcefile.json
- task: ms-devlabs.utilitytasks.task-tokenizer.Tokenizer@2
displayName: 'Tokenize File'
inputs:
SourcePath: 'sourcefile.json'
env:
FIRST_NAME: 'Paul' # This is not respected
- task: CmdLine@2
displayName: 'Echo Out the Source File (After Modifications)'
inputs:
script: |
type sourcefile.json And here are the screenshots showing this "working": Showing that the task found the variable for replacement: Showing that Unexpectedly (at least if you don't understand the documentation) This causes issues with attempting to use a KeyVault Secret as you MUST explicitly set these as Environment Variables as per the documentation:
variables:
GLOBAL_MYSECRET: $(mySecret) # this will not work because the secret variable needs to be mapped as env
GLOBAL_MY_MAPPED_ENV_VAR: $(nonSecretVariable) # this works because it's not a secret.
steps:
- powershell: |
Write-Host "Using an input-macro works: $(mySecret)"
Write-Host "Using the env var directly does not work: $env:MYSECRET"
Write-Host "Using a global secret var mapped in the pipeline does not work either: $env:GLOBAL_MYSECRET"
Write-Host "Using a global non-secret var mapped in the pipeline works: $env:GLOBAL_MY_MAPPED_ENV_VAR"
Write-Host "Using the mapped env var for this task works and is recommended: $env:MY_MAPPED_ENV_VAR"
env:
MY_MAPPED_ENV_VAR: $(mySecret) # the recommended way to map to an env variable I have a few ideas that I'm willing to try and will respond back with my results. |
Sorry for the double post; but I wanted to come back with that I also got this working with KeyVault Secrets. The key to getting this to work is to tell the KeyVault task to I utilized the secrets filter to ensure we only pull the secret we care about. The following snippet works: sourcefile.json {
"MySecret": "__My-Secret__",
"Unknown": "__UNKNOWN_VARIABLE__"
} azure-pipeline.yml trigger: none
pool:
vmImage: windows-latest
steps:
- task: AzureKeyVault@2
displayName: 'Azure Key Vault: MyKeyVault'
inputs:
azureSubscription: 'SPN'
KeyVaultName: 'MyKeyVault'
secretsFilter: 'My-Secret'
RunAsPreJob: true
- task: CmdLine@2
displayName: 'Echo Out the Source File'
inputs:
script: |
type sourcefile.json
- task: ms-devlabs.utilitytasks.task-tokenizer.Tokenizer@2
displayName: 'Tokenize File'
inputs:
SourcePath: 'sourcefile.json'
- task: CmdLine@2
displayName: 'Echo Out the Source File (After Modifications)'
inputs:
script: |
type sourcefile.json Note that when looking at the output in the console the secret will be redacted in the logs due to the behavior called out in the documentation: Specifically:
|
In
tokenize-ps3.ps1
it is no longer possible to use environment variables as replacement strings.It was possible in
tokenize.ps1
thanks to the following lines:The text was updated successfully, but these errors were encountered: