无法将环境变量传递给 azure 管道 yaml 中的 powershell 脚本(非内联)

Posted

技术标签:

【中文标题】无法将环境变量传递给 azure 管道 yaml 中的 powershell 脚本(非内联)【英文标题】:Unable to pass on Env variables to powershell script (not inline) in azure pipelines yaml 【发布时间】:2022-01-15 08:16:00 【问题描述】:

我的 azure pipelines yaml 运行一个存储在 repo 中的 powershell 脚本。

该 powershell 脚本需要 3 个变量:工作目录、Oauth 访问令牌和源分支名称(触发管道)。

但似乎,每当我尝试传递参数时,powershell 脚本都无法识别它们并出现错误

The term 'env:SYSTEM_ACCESSTOKEN' is not recognized as the 
name of a cmdlet, function, script file, or operable program

The term 'env:BUILD_SOURCEBRANCHNAME' is not recognized as the 
name of a cmdlet, function, script file, or operable program

我的 yaml 看起来像这样:

name: $(Build.DefinitionName)_$(Build.SourceBranchName)_$(Build.BuildId)

trigger:
  branches:
    include:
      - '*'

variables:
  system_accesstoken: $(System.AccessToken)

jobs:
  - job: NoteBookMergeAndOnPremSync
    displayName: Merge Notebooks to Notebooks branch and sync to on prem git
    pool:
      name: Poolname
    steps:
    - task: PowerShell@2
      displayName: 'Merge to Notebooks branch in Azure and Sync to On Prem'
      inputs:
        targetType: filePath
        filePath: ./deploy/MergeAndSync.ps1
        arguments: '-workingdir $(System.DefaultWorkingDirectory)\ -featurebranch $(env:BUILD_SOURCEBRANCHNAME) -accesstoken $(env:SYSTEM_ACCESSTOKEN)'
    

当使用 GUI 使用“发布定义”时,我能够将 powershell 脚本作为“内联 powershell 脚本”成功运行,但我希望所有这些都在 yaml 中的 azure 管道 (yaml) 中,但不幸的是,我就是找不到传递这些环境变量的方法。

如何将 BUILD_SOURCEBRANCHNAME 和 env:SYSTEM_ACCESSTOKEN 从 azure 管道 yaml 传递到 powershell 脚本?

另外,我想避免使用“内联 powershell 脚本”,而是将逻辑保存在我的存储库中。

【问题讨论】:

【参考方案1】:

您似乎将 Azure 宏语法 ($(name)) 与 PowerShell 变量引用 语法($env:name,用于环境变量)混合在一起。 p>

也就是说,由于您正在调用带有参数的 脚本文件 - 这可能意味着使用了 PowerShell CLI 的 -File 参数 - 您不能在 中引用环境变量arguments,因为 PowerShell 然后将 $env:BUILD_SOURCEBRANCHNAME verbatim 之类的东西解释为文字字符串,而不是环境变量引用(后者只能在inside 脚本或在 CLI 调用中使用 -Command)。

因此,我认为解决方案是仅使用 Azure 宏语法将感兴趣的变量的 作为参数传递:

arguments: >-
  -workingdir $(System.DefaultWorkingDirectory)\
  -featurebranch $(Build.SourceBranchName)
  -accesstoken $(system_accesstoken)

更新:正如您所说,您不需要 任何 variables 定义:直接引用 $(System.AccessToken) 也可以:

arguments: >-
  -workingdir $(System.DefaultWorkingDirectory)\
  -featurebranch $(Build.SourceBranchName)
  -accesstoken $(System.AccessToken)

【讨论】:

谢谢,没错。还有,我觉得自己好傻。答案就在我的构建定义之上,我在第一行定义了名称(只是漫长的一天)。但无论如何,感谢您的详细说明。没有它,我就不会更多地了解 PowerShell CLI 的 -File 和 -Command 参数的细微差别。非常感谢! 我刚刚这样做了:参数:'-workingdir $(System.DefaultWorkingDirectory)\ -featurebranch $(Build.SourceBranchName) -accesstoken $(System.AccessToken)'。是的,变量部分是多余的,所以我删除了它 很高兴听到它有帮助,@SaugatMukherjee;我的荣幸。我已更新答案以显示您的 no-variables: 解决方案。 感谢编辑,@VinceBowdren - 不知道这种语法(后来找到了解释 here)。

以上是关于无法将环境变量传递给 azure 管道 yaml 中的 powershell 脚本(非内联)的主要内容,如果未能解决你的问题,请参考以下文章

如何将 Azure 管道变量传递给 AzureResourceManagerTemplateDeployment@3 任务使用的 ARM 模板?

如何将变量传递给 azure devops 管道中的 SqlAzureDacpacDeployment@1 任务

如何将Azure管道变量传递给AzureResourceManagerTemplateDeployment @ 3任务使用的ARM模板?

azure devops yaml管道未设置变量

如何将 gitlab ci/cd 变量传递给 kubernetes(AKS) deployment.yaml

从脚本返回对象变量 - Azure YAML 管道