我的 azure 管道 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 腳本”,而是將邏輯保存在我的存盤庫中。
uj5u.com熱心網友回復:
看起來您將Azure 宏語法( $(name)) 與PowerShell 變數參考語法($env:name, 用于環境變數)混合在一起。
也就是說,由于您正在呼叫帶有引數的腳本檔案- 這可能意味著-File使用了 PowerShell CLI的引數 - 您不能在引數中參考環境變數,因為 PowerShell 然后會$env:BUILD_SOURCEBRANCHNAME 逐字解釋(作為文字字串)之類的東西,而不是而不是作為環境變數參考(后者只能在腳本內部或在 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/380580.html
