首页 > 解决方案 > 在 yaml 文件中调用管道变量(Repos)

问题描述

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

标签: azure-devops

解决方案


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

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

下面是一个关于使用mySecretPowerShell 中调用的 secert 管道变量的示例。与普通管道变量不同,没有名为 MYSECRET 的环境变量。单击链接以获取更多详细信息。

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: ***

推荐阅读