Azure DevOps Pipeline循环调用PowerShell脚本路径找不到问题
问题场景
搭建Azure DevOps Pipeline时,需要遍历variableFiles参数中的每个值,调用steps.yml模板执行对应的PowerShell脚本。当variableFiles传入多个值时,出现错误:
##[error]The term 'D:\a\1\s\ADO\powershell\CreateAADGroup\createAADGroup.ps1' is not recognized as a name of a cmdlet, function, script file, or executable program
但传入单个值时执行正常。
相关配置
Pipeline主配置
pool: vmImage: windows-latest parameters: - name: variableFiles displayName: Variable File type: object default: - test - testApp - name: environment displayName: Environment type: string default: Dev values: - Dev - Test - Stag - Prod variables: - group: ADO_statefile name: 'ADO Automation' stages: - stage: powershellDeployment displayName: "PowerShell Deployment" jobs: - deployment: "deployment" displayName: "PowerShell Deployment" environment: 'ado - Dev' strategy: runOnce: deploy: steps: - ${{ each variableFile in parameters.variableFiles }}: - template: steps.yml parameters: spn: $(service_connection) JsonPath: ${{ variableFile }} validate: true
steps.yml模板配置
steps: - checkout: self - task: PowerShell@2 displayName: "List environment variables" inputs: pwsh: true targetType: "inline" script: | # List all pipeline variables dir env: - task: AzurePowerShell@5 displayName: "create ADD groups" inputs: azureSubscription: ${{ parameters.spn }} pwsh: true azurePowerShellVersion: LatestVersion ScriptType: 'FilePath' ScriptPath: "$(Build.SourcesDirectory)/ADO/powershell/CreateAADGroup/createAADGroup.ps1" ScriptArguments: '-JsonPath ${{ parameters.JsonPath }}'
PowerShell脚本内容
Param( [string]$JsonPath ) $parameters = Get-Content -Raw -Path "$($env:BUILD_SOURCESDIRECTORY)\ADO\terraform\$JsonPath.tfvars.json" | ConvertFrom-Json foreach ($GroupName in $parameters.AADGroupName) { # Check if the group already exists $existingGroup = Get-AzADGroup -DisplayName $GroupName -ErrorAction SilentlyContinue if ($existingGroup) { Write-Host "Group $GroupName already exists. Skipping creation." } else { Write-Host "Creating group: $GroupName" New-AzADGroup -DisplayName $GroupName -MailNickname $GroupName } }
问题原因
问题出在重复执行checkout: self步骤:
在steps.yml模板中,每次调用都会执行checkout: self,而同一个Deployment Job的runOnce策略下,工作区是持久化的。第一次checkout会正常拉取代码到构建目录,但第二次执行checkout时,会尝试重新拉取代码,导致工作区文件状态异常,最终引发脚本路径解析冲突,系统找不到指定的PowerShell脚本文件。
解决办法
将checkout: self步骤从steps.yml模板中移除,放到Pipeline主配置的deploy步骤里、循环的外面,确保整个Job只执行一次代码拉取:
修改后的Pipeline主配置
stages: - stage: powershellDeployment displayName: "PowerShell Deployment" jobs: - deployment: "deployment" displayName: "PowerShell Deployment" environment: 'ado - Dev' strategy: runOnce: deploy: steps: - checkout: self # 移到循环外,只执行一次 - ${{ each variableFile in parameters.variableFiles }}: - template: steps.yml parameters: spn: $(service_connection) JsonPath: ${{ variableFile }} validate: true
修改后的steps.yml模板配置
steps: # 移除checkout: self步骤 - task: PowerShell@2 displayName: "List environment variables" inputs: pwsh: true targetType: "inline" script: | # List all pipeline variables dir env: - task: AzurePowerShell@5 displayName: "create ADD groups" inputs: azureSubscription: ${{ parameters.spn }} pwsh: true azurePowerShellVersion: LatestVersion ScriptType: 'FilePath' ScriptPath: "$(Build.SourcesDirectory)/ADO/powershell/CreateAADGroup/createAADGroup.ps1" ScriptArguments: '-JsonPath ${{ parameters.JsonPath }}'
调整后,整个Deployment Job只会拉取一次代码,后续循环调用模板时直接使用已拉取的文件,不会再出现路径或文件找不到的问题。
内容的提问来源于stack exchange,提问作者Tobias Rasmussen
相关产品推荐
相关产品推荐

