You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure YAML Pipeline任务的workingDirectory中使用Glob模式?

解决Azure Pipeline中动态匹配包目录的问题

Azure Pipeline的workingDirectory参数本身不支持Glob通配符(比如abc.*),它会把*当作路径的一部分处理,所以得通过脚本逻辑来动态获取目标目录,下面是几种可行的实现方法:

方法一:安装时捕获路径并设置管道变量

在安装包的任务里,用PowerShell找到最新的abc包目录,把它的build路径设为管道变量,后续任务直接引用这个变量作为工作目录:

- powershell: |
    nuget install abc
    # 获取第一个匹配abc.*的目录名(nuget默认安装最新版本,第一个即为目标)
    $abcPackageDir = Get-ChildItem -Path "$(Build.SourcesDirectory)\src\packages" -Name "abc.*" | Select-Object -First 1
    # 设置管道变量,供后续任务使用
    Write-Host "##vso[task.setvariable variable=ABCBuildDir]$(Build.SourcesDirectory)\src\packages\$abcPackageDir\build"
  displayName: 安装abc包并设置构建目录变量
  workingDirectory: $(Build.SourcesDirectory)\src

- script: |
    abc.exe do_something -o "$(Build.SourcesDirectory)\src"
  displayName: 执行abc命令
  workingDirectory: $(ABCBuildDir)

方法二:在执行脚本内切换目录

不用依赖workingDirectory参数,直接在脚本里先定位到目标目录再执行命令,适合不想额外设置变量的场景:

用CMD脚本实现

- script: |
    @echo off
    # 遍历匹配abc.*的目录,找到后进入build目录执行命令
    for /d %%i in ("$(Build.SourcesDirectory)\src\packages\abc.*") do (
      cd "%%i\build"
      abc.exe do_something -o "$(Build.SourcesDirectory)\src"
      goto :exitloop # 找到第一个匹配目录后退出循环
    )
    :exitloop
  displayName: 执行abc命令
  workingDirectory: $(Build.SourcesDirectory)\src

用PowerShell脚本实现

- powershell: |
    # 获取匹配的build目录完整路径
    $abcBuildPath = Get-ChildItem -Path "$(Build.SourcesDirectory)\src\packages\abc.*\build" | Select-Object -First 1 -ExpandProperty FullName
    # 切换到目标目录
    Set-Location $abcBuildPath
    # 执行命令
    .\abc.exe do_something -o "$(Build.SourcesDirectory)\src"
  displayName: 执行abc命令
  workingDirectory: $(Build.SourcesDirectory)\src

补充说明

之所以不能直接在workingDirectory里用abc.*,是因为Azure Pipeline的任务参数会把路径当作字面量解析,不会自动匹配通配符,必须通过脚本层面的逻辑来动态获取实际路径。

内容的提问来源于stack exchange,提问作者preadeep xsy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 14:50:18