Azure Pipelines遍历.hex文件YAML解析报错,如何实现批量处理?
Azure Pipeline遍历.hex文件的问题解决
错误原因
你用的${{ each }}是编译期表达式,会在管道启动前完成解析:
- 此时
$(Build.SourcesDirectory)这类运行时变量还未被替换为实际路径; - 编译期表达式不支持直接用
*.hex通配符遍历文件系统内容,这就是触发YAML解析错误的核心原因。
需求可实现,两种方案任选
方案一:单步骤内遍历处理(简单场景首选)
用PowerShell脚本在单个步骤里完成文件遍历和操作,适合不需要单独步骤日志的场景:
trigger: none pool: name: "LabelManagementPool" steps: - script: | Get-ChildItem -Path $(Build.SourcesDirectory)\*.hex | ForEach-Object { echo "当前处理文件: $($_.FullName)" # 在这里添加针对每个hex文件的操作逻辑 } displayName: "遍历并处理所有.hex文件"
方案二:每个文件生成独立步骤(需单独日志/并行场景)
如果需要为每个hex文件创建独立的管道步骤,需要分两步实现:先收集文件列表,再用模板遍历生成步骤:
- 创建模板文件
process-hex-files.yml:
parameters: - name: files type: object default: [] steps: - ${{ each file in parameters.files }}: - script: echo "处理文件: ${{ file }}" displayName: "处理${{ file }}"
- 主管道调用模板:
trigger: none pool: name: "LabelManagementPool" steps: # 第一步:收集所有hex文件路径,输出为管道变量 - script: | $files = Get-ChildItem -Path $(Build.SourcesDirectory)\*.hex | Select-Object -ExpandProperty FullName $jsonFiles = $files | ConvertTo-Json -Compress echo "##vso[task.setvariable variable=hexFiles;isOutput=true]$jsonFiles" name: collectFiles displayName: "收集.hex文件列表" # 第二步:用模板遍历文件列表生成步骤 - template: process-hex-files.yml parameters: files: $[convertFromJson(variables['collectFiles.hexFiles'])]
内容的提问来源于stack exchange,提问作者Martin Verjans
相关产品推荐
相关产品推荐

