【问题标题】:""Your build pipeline references an undefined variable" : it's just a powershell variable""您的构建管道引用了一个未定义的变量":它只是一个 powershell 变量
【发布时间】:2021-10-01 07:03:33
【问题描述】:

我在 Azure Devops Server 2020 的管道中的作业中有一个 Powershell 任务。这是 powershell 的一部分:

      $xml = [xml](Get-Content $configPath)
   
       Write-Output "Iterating over appSettings"
       ForEach($add in $xml.configuration.appSettings.add)
       {
           #Write-Output "Processing AppSetting key $($add.key)"
           
           $SecretVarKey = "MAPPED_"+$add.key
           Write-Output $SecretVarKey
   
           $matchingEnvVar = [Environment]::GetEnvironmentVariable($SecretVarKey)
   
           if($matchingEnvVar)
           {
                   Write-Output "Found matching environment variable for key: $($add.key)"
                   Write-Output "Replacing value $($add.value)  with $matchingEnvVar"
   
                   $add.value = $matchingEnvVar
           }
       }

这在任务中工作得很好——我的构建正在做我想做的事。但是当我查看 YAML 时,我会看到这样的 cmets:

#Your build pipeline references an undefined variable named ‘$add.key’. Create or edit the build pipeline for this YAML file, define the variable on the Variables tab. See https://go.microsoft.com/fwlink/?linkid=865972

同样,这不会干扰脚本的执行。

但是,现在我想将此任务提取到一个任务组中。当我这样做时,无害的错误检测现在是一个问题,因为它坚持这些是新参数:

我可以做些什么来改变我的 Powershell 脚本,使它们不被认为是参数?

【问题讨论】:

    标签: powershell azure-pipelines azure-devops-server-2020


    【解决方案1】:

    您的构建管道引用了一个名为“$add.key”的未定义变量

    这是由下面的行触发的:

    Write-Output "Found matching environment variable for key: $($add.key)"
    

    $($add.key) 被 Azure DevOps 解析为macro syntax。您可以通过使用字符串格式来避免这种情况:

    'Found matching environment variable for key: {0}' -f $add.key
    

    顺便说一句,在大多数情况下,您不需要使用 Write-Output - 它既慢又多余。有关详细信息,请参阅此博文:Let’s Kill Write-Output

    【讨论】:

    • 感谢您的快速回复!我今晚晚些时候会对此进行测试,假设你是对的,我会接受答案。
    猜你喜欢
    • 2011-11-14
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    • 2021-11-21
    • 2015-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多