5

I am trying to get some values based on the logic to form the version number for my dot net application.

I am using Azure devops and created a Pipeline (YAML). In the pipeline, I have created few variables, which gets the values by using some calculation logic. Below is the code.

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)'

the above logic has a default date set in old_date and it is subtracted from latest_date and divided by 56 to get some value. the next value of the resultant is our PSI. the same way sprint is calculated. this is done in order to form the version number of our Dot Net application. something like this "2.212.2.0312"

The same type of logic works in groovy script in Jenkins. But here, it shows PSI = ($(latest_date)-$(old_date))/56+1 and doesnt evaluate anything.

Appreciate your response on the same.

Pravesh Pesswani
  • 481
  • 8
  • 16

1 Answers1

4

This is not possible. There is no support for math expressions in variables expressions. You can check it here. You can however declare static variables as above and move all calculation into step and set build number dynamically like it is shown here

It could like this:

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"
Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107