如何在 Azure devops YAML 脚本中执行算术运算?

How to perform arithmetic operations in Azure devops YAML script?

我正在尝试根据逻辑获取一些值以构成我的点网应用程序的版本号。

我正在使用 Azure devops 并创建了一个管道 (YAML)。在管道中,我创建了几个变量,这些变量通过使用一些计算逻辑来获取值。下面是代码。

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  old_date: '20210601'
  latest_date: '20210603'
  PSI: ($(latest_date)-$(old_date))/56+1
  totalsprints: '($(latest_date)-$(old_date))/14'
  sprint: '$(totalsprints)%4+1'
  majorversion: '2.'
  dot: '.'

name: '$(majorversion)$(Year:yy)$(PSI)$(dot)$(sprint)$(dot)$(Date:MMdd)'

以上逻辑在 old_date 中设置了一个默认日期,从 latest_date 中减去它并除以 56 得到一些值。结果的下一个值是我们的 PSI。 sprint 的计算方式相同。这样做是为了形成我们的 Dot Net 应用程序的版本号。像这样的“2.212.2.0312”

相同类型的逻辑在 Jenkins 的 groovy 脚本中起作用。但在这里,它显示 PSI = ($(latest_date)-$(old_date))/56+1 并且不评估任何内容。

感谢您对此的回应。

这是不可能的。变量表达式中不支持数学表达式。你可以看看here. You can however declare static variables as above and move all calculation into step and set build number dynamically like it is shown

它可能是这样的:

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  old_date: '20210601'
  latest_date: '20210603'
  # PSI: ${{ (variables.latest_date-variables.old_date)/56+1}}
  # totalsprints: ${{ (variables.latest_date-variables.old_date)/14 }}
  # sprint: ${{ variables.totalsprints%4+1 }}
  majorversion: '2.'
  dot: '.'

#name: '$(majorversion)$(Year:yy)$(PSI)$(dot)$(sprint)$(dot)$(Date:MMdd)'

trigger: none
pr: none

pool:
  vmImage: ubuntu-latest

name: 'Set dynamically below in a task'

steps:
- task: PowerShell@2
  displayName: Set the name of the build (i.e. the Build.BuildNumber)
  inputs:
    targetType: 'inline'
    script: |
      [int] $PSI = (([int] $(latest_date))-([int] $(old_date)))/56+1
      [int] $totalsprints = ( ([int] $(latest_date))-([int] $(old_date)))/14
      [int] $sprint = $totalsprints%4+1
      $year = Get-Date -Format "yy"
      $monthDay = Get-Date -Format "MMdd"
      [string] $buildName = "$(majorversion)$year$PSI.$sprint.$monthDay"
      Write-Host "Setting the name of the build to '$buildName'."
      Write-Host "##vso[build.updatebuildnumber]$buildName"