简体   繁体   English

如何在可能未定义的 Azure Devops bash 任务中使用秘密变量

[英]How can I use a secret variable in Azure Devops bash task which could be undefined

I am trying to create a config file using a bash task in Azure Devops.我正在尝试使用 Azure Devops 中的 bash 任务创建配置文件。 The variables come from azure keyvault, so I don't know which variables are defined and which ones are undefined.变量来自azure keyvault,所以我不知道哪些变量是定义的,哪些是未定义的。

- script: |
    touch config.txt
    echo "1. $(MyDefinedVariable)" >> config.txt
    echo "2. $(MyUndefinedVariable)" >> config.txt
    cat config.txt

Since MyUndefinedVariable is not defined, the pipeline doesn't substitute $(MyUndefinedVariable) , resulting in a bash error MyUndefinedVariable: command not found .由于MyUndefinedVariable ,管道不会替换$(MyUndefinedVariable) ,从而导致 bash 错误MyUndefinedVariable: command not found

I have tried using the env argument to use bash variables but I get the same error since "$(MyUndefinedVariable)" is being passed in to the bash environment.我曾尝试使用env参数来使用 bash 变量,但由于“$(MyUndefinedVariable)”被传递到 bash 环境中,我得到了同样的错误。

- script: |
    touch config.txt
    echo "1. $MY_DEFINED_VARIABLE" >> config.txt
    echo "2. $MY_UNDEFINED_VARIABLE" >> config.txt
    cat config.txt
  env:
    MY_DEFINED_VARIABLE: $(MyDefinedVariable)
    MY_UNDEFINED_VARIABLE: $(MyUndefinedVariable)

I just want undefined variables to resolve to an empty string but can't find a sensible way to do it.我只想将未定义的变量解析为空字符串,但找不到合理的方法。

All variables mapped from Azure KeyVault are considered as secrets so mapping like this one is necessary:从 Azure KeyVault 映射的所有变量都被视为机密,因此需要像这样映射:

  env:
    MY_DEFINED_VARIABLE: $(MyDefinedVariable)
    MY_UNDEFINED_VARIABLE: $(MyUndefinedVariable)

I'm afraid that if you are not aware of values in your KeyVault you need to use Azure CLI to check this.恐怕如果您不知道 KeyVault 中的值,则需要使用Azure CLI进行检查。 To checks all secret keys you can use this command :要检查所有密钥,您可以使用以下命令

az keyvault secret list [--id]
                        [--maxresults]
                        [--query-examples]
                        [--subscription]
                        [--vault-name]

You can combine this CLI with Azure CLI task .您可以将此 CLI 与Azure CLI 任务结合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM