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

Azure DevOps:从分支名称提取数值到变量的实现问题

从分支名称提取数值到Azure Pipelines变量的实现方法

针对你需要从test_branch.$someNumber格式的分支名中提取数值到releaseMinorVersion变量的需求,可以通过Azure Pipelines的模板表达式直接实现,无需复杂正则:

方法一:使用split函数分割字符串

利用分支名的固定前缀格式,用.作为分隔符拆分字符串,取拆分后的第二个元素即可:

trigger:
- master
- "test_branch.*"

${{ if startsWith(variables['Build.SourceBranchName'], 'test_branch') }}:
  releaseMinorVersion: ${{ split(variables['Build.SourceBranchName'], '.')[1] }}
version: 5.$(releaseMinorVersion).$(Build.BuildId)

方法二:使用replace函数移除前缀

直接替换掉固定前缀test_branch.,剩余部分就是目标数值:

trigger:
- master
- "test_branch.*"

${{ if startsWith(variables['Build.SourceBranchName'], 'test_branch') }}:
  releaseMinorVersion: ${{ replace(variables['Build.SourceBranchName'], 'test_branch.', '') }}
version: 5.$(releaseMinorVersion).$(Build.BuildId)

说明

这两种方法都基于Azure Pipelines编译阶段执行的模板表达式,Build.SourceBranchName属于编译时可用变量,因此能正确提取数值并赋值。如果后续分支名格式有调整(比如前缀分隔符变化),只需对应修改分隔符或替换的前缀内容即可。

内容的提问来源于stack exchange,提问作者Elias Bucci

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 11:42:04