无法读取Marketplace任务中的Secret Azure发布管道变量

时间:2020-06-07 00:20:30

标签: azure-devops azure-pipelines azure-pipelines-build-task azure-pipelines-release-task azure-pipelines-tasks

当尝试读取在Azure发布管道中设置的秘密变量时,出现以下错误。

术语“ SecretVariableName”不能识别为cmdlet,函数,脚本文件或可运行程序的名称。 ....

我知道该变量是100%正确的,但是以下任何一种方式都无法帮助读取它。其他非秘密变量也可以。

$myvar1 = $(SecretVariableName)
$myvar2 = "$(SecretVariableName)"
$myvar3 = $Env:SecretVariableName

所有这些都行不通。其中SecretVariableName是Release Pipeline中的秘密变量。

作为旁注:

  1. 对于非秘密变量,它可以正常工作。
  2. 在管道中作为内联脚本运行也可以。

问题是,当尝试读取用于创建vsix文件并上传到visualstudio marketplace

的市场任务中的秘密变量时

如何成功访问它?

谢谢

2 个答案:

答案 0 :(得分:0)

秘密变量使用2048位RSA密钥进行静态加密。它们会自动从构建或发行版的任何日志输出中屏蔽掉。

与普通变量不同,它们不会自动解密为脚本的环境变量。您需要显式映射秘密变量。

每个需要将密钥用作环境变量的任务都会重新映射。如果要在脚本中使用称为mySecret的秘密变量,请使用脚本任务输入变量的Environment部分。将环境变量名称设置为MYSECRET,并将值设置为$(mySecret)

有关详细信息,请查看此官方document。另外,您可以通过类似问题参考此case

更新

以下示例显示了如何在PowerShell脚本中使用名为mySecret的秘密变量。

variables:
 GLOBAL_MYSECRET: $(mySecret) # this will not work because the variable needs to be mapped as env

steps:

- powershell: |
    # Using an input-macro:
    Write-Host "This works: $(mySecret)"

    # Using the mapped env var:
    Write-Host "This works: $env:MY_MAPPED_ENV_VAR"    # Recommended

  env:
    MY_MAPPED_ENV_VAR: $(mySecret) # right way to map to an env variable

您还可以使用variables定义来映射秘密变量。此示例说明如何在Azure文件复制任务中使用机密变量$(vmsUser)$(vmsAdminPass)

variables:
  VMS_USER: $(vmsUser)
  VMS_PASS: $(vmsAdminPass)    

steps:
- task: AzureFileCopy@4
  inputs:
    SourcePath: 'my/path'
    azureSubscription: 'my-subscription'
    Destination: 'AzureVMs'
    storage: 'my-storage'
    resourceGroup: 'my-rg'
    vmsAdminUserName: $(VMS_USER)
    vmsAdminPassword: $(VMS_PASS)

有关示例,请参阅this

答案 1 :(得分:0)

没有没有我可以找到有用的文档,并且花了几天的时间自己弄清楚这个问题-尝试许多事情和建议都没有成功。我终于自己解决了,希望能帮助别人不要浪费我太多的时间。


# This gets ALL Task Variables that you can access (including Secret variables)
$allTaskVariablesIncludingSecrets = Get-VstsTaskVariableInfo

# Convert it to json it to see whats available during your debugging - this is just for you to see whats available for you to access.
$allTaskVariablesIncludingSecrets | ConvertTo-Json
#that will give you array of objects with three properties (Name, Secret and Value) in this format:
# [
#     {
#         "Name":  "SecretVariableName",
#         "Secret":  true,
#         "Value":  "***"
#     },
#     {
#         "Name":  "NotSecretVar",
#         "Secret":  false,
#         "Value":  "Some stuff here"
#     }
# ]

# Since our objective is to get a hold of Secret varibales, lets filter them
$secVariables = $allTaskVariablesIncludingSecrets | Where-Object {$_.Secret -eq $true}
# If one of your Secret Variable is called 'SecretVariableName', here is how you access it
$mySecretVarObject = $secVariables |  Where-Object {$_.Name -eq "SecretVariableName"}
$mySecret = $($mySecretVarObject.Value)
# This will give display *** for the value but Length will show you the actual length. So you are good to use $mySecret in your script. You don't NEED to SEE the actual value.
Write-Host "Value: $mySecret and Length: $($mySecret.Length)"

# Simply use $mySecret the way you would any local variable. No special treatment or husle needed

https://bitbucket.org/ZelalemW/how-to-access-secrets-in-ado/src/master/