【问题标题】:Azure Devops: Shorten $(SourceBranchName) when used within pipeline name directiveAzure Devops:在管道名称指令中使用时缩短 $(SourceBranchName)
【发布时间】:2021-03-01 10:54:09
【问题描述】:

我们用于 Azure 管道的 yml 定义文件以

开头

name: $(Build.DefinitionName)_$(SourceBranchName)_$(rev:rrrrr)

这样,我们会得到很长的构建名称,这会对构建结果页面中的显示产生负面影响。因此,我们想将$(SourceBranchName) 缩短为例如前 20 个字符。

有没有办法做到这一点?

【问题讨论】:

  • 你好,这个问题怎么样?下面的答案是否解决了您的问题?如果是,您可以Accept it as an Answer,这样可以帮助遇到相同问题的其他社区成员,我们可以存档此线程,谢谢。

标签: azure-devops azure-pipelines


【解决方案1】:

我们想将 $(SourceBranchName) 缩短为例如前 20 个字符。

您可以尝试使用command line\powershell 脚本将长字符串拆分为短字符串。

steps:
- script: |
   echo $(Build.SourceBranchName)
   
   set  TestVar=$(Build.SourceBranchName)
   
   set MyCustomVar= %TestVar:~0,20%
   
   echo %MyCustomVar%
   
   echo ##vso[task.setvariable variable=CustomVar]%MyCustomVar%
   
  displayName: 'Get the first 20 character versions of Build.SourceBranchName'

然后我们可以得到SourceBranchName的短字符串。

一般SourceBranchNameSourceVersion不同,一般不是很长的字符串。如果你的SourceBranchName确实是一个很长的字符串,那么上面的方法对你有帮助。

您可以查看my previous thread 了解更多详情。

注意:如果您想缩短构建名称,我们需要通过 Logging 命令更新当前构建的构建号(例如Write-Host "##vso[build.updatebuildnumber]buildnumber"):

Write-Host "##vso[build.updatebuildnumber]$(Build.DefinitionName).$(CustomVar).$(rev:rrrrr)"

但是$(rev:rrrrr) 不能在任务中使用,所以我们必须在默认版本号格式(选项)中包含$(rev:rrrrr),例如:$(date:yyyyMMdd)-$(rev:rrrrr)。并从预定义变量(Build.BuildNumber/BUILD_BUILDNUMBER)中获取当前版本号,然后我们解析$(rev:rrrrr)的值。

【讨论】:

  • 我有一个很好的封装任务,称为转换变量,带有一个子字符串选项:marketplace.visualstudio.com/…。还有一个正则表达式搜索和替换选项。
猜你喜欢
  • 1970-01-01
  • 2020-11-01
  • 2020-04-02
  • 2022-10-05
  • 2020-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-01
相关资源
最近更新 更多