在 yaml 文件中调用管道变量(Repos)

时间:2021-03-22 10:55:50

标签: azure-devops

我有以下情况: 在 Azure Repos 中,我需要放置 yaml 文件,其中需要放置 OAuth 凭据,并在发布管道任务中发布 PowerShell 脚本时调用该 yaml 文件。 我的问题是,当我尝试将这些凭据保留在管道变量中并通过 $(OAuth) 在 yaml 文件中引用它们时,我需要将我在 Repos yaml 文件下提供的 OAuth 屏蔽为纯文本。 甚至可以使用管道变量在 Repos 下的 yaml 文件中屏蔽 OAuth 吗?请帮忙。

1 个答案:

答案 0 :(得分:0)

<块引用>

当我试图将这些凭据保存在管道变量中并通过 $(OAuth) 在 yaml 文件中引用它们时,它不起作用。

不能在 PowerShell 中直接使用 $(OAuth) 引用秘密管道变量。

以下是在 PowerShell 中使用名为 mySecret 的安全管道变量的示例。 与普通管道变量不同,没有名为 MYSECRET 的环境变量。点击 the link 以获取更多详细信息。

<块引用>
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

上述脚本中任务的输出如下所示:

Using an input-macro works: ***
Using the env var directly does not work:
Using a global secret var mapped in the pipeline does not work either:
Using a global non-secret var mapped in the pipeline works: foo
Using the mapped env var for this task works and is recommended: ***