【问题标题】:How to perform arithmetic operations in Azure devops YAML script?如何在 Azure devops YAML 脚本中执行算术运算?
【发布时间】:2021-03-12 10:32:22
【问题描述】:

我正在尝试根据逻辑获取一些值,以形成我的 dot net 应用程序的版本号。

我正在使用 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。计算冲刺的方式相同。这样做是为了形成我们的 Dot Net 应用程序的版本号。像这样的“2.212.2.0312”

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

感谢您对此的回应。

【问题讨论】:

    标签: azure azure-devops yaml azure-pipelines devops


    【解决方案1】:

    这是不可能的。变量表达式中不支持数学表达式。你可以检查它here。但是,您可以像上面一样声明静态变量并将所有计算移到步骤中并动态设置内部版本号,就像显示的 here

    可能是这样的:

    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"
    

    【讨论】:

      猜你喜欢
      • 2014-10-20
      • 1970-01-01
      • 2013-04-16
      • 2017-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-02
      • 2022-07-07
      相关资源
      最近更新 更多