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



My 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)'

我能够成功地运行powershell脚本;在线powershell脚本";当使用";释放定义";使用GUI,但我希望所有这些都在yaml中的azure管道(yaml(中,但不幸的是,我找不到传递这些env变量的方法。

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

此外,我希望避免出现";内联powershell脚本";而是把逻辑保存在我的回购中。

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

也就是说,由于您使用参数调用脚本文件(这可能意味着使用了PowerShell CLI的-File参数(,因此无法在参数中引用环境变量,因为PowerShell会逐字逐句地解释类似$env:BUILD_SOURCEBRANCHNAME的内容(作为文字字符串(,而不是环境变量引用(后者只能在脚本内部或在使用-Command的CLI调用中工作(。

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

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

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

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

最新更新