1

In my yaml file I defined some variables which should contain the value of the month ($MinorVersion) or year ($MajorVersion), by setting the value to '$(Month)' and/or '$(Year:yy)'. When I print them in the task script or task powershell it only displays '$(Month)' and/or '$(Year:yy)' . The expected output of the variable $(MajorVersion) is of course the current month and year 09 and 20.

Can someone tell me how I can access the variable $MinorVersion in the task script / powershell to get the actual value of it? That's the files content:

trigger:
- master

pool:
  vmImage: 'windows-latest'

#import variable group resize-group, local variables are name/value pairs
variables:
- group: resize-group
- name: buildConfiguration
  value: 'Release'
- name: appxPackageDir
  value: '$(build.artifactStagingDirectory)\AppxPackages\\'
- name: MajorVersion
  value: '$(Year:yy)'
- name: MinorVersion
  value: '$(Month)'
- name: PatchVersion
  value: 45
- name: RevVersion
  value: '$(BuildID)'
- name: Packageappxmanifest
  value: Package.appxmanifest
- name: PackageVersion
  value: '$(MajorVersion).$(MinorVersion).$(PatchVersion).$(RevVersion)'


name: $(BuildDefinitionName)_$(MajorVersion).$(MinorVersion).$(PatchVersion)$(RevVersion)

steps:
- checkout: self
  submodules: true


- script: | 
     echo %MinorVersion%
     echo %variables.MinorVersion%
     echo $($[variables.MinorVersion])
     echo ${{ variables.MinorVersion }} # outputs initialValue
     echo $(Month)
     echo $(MajorVersion)
     echo $(MinorVersion)
     echo $(RevVersion)
     echo $(PackageVersion)
     echo $(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)

- powershell: |
    Write-Host  $env:MinorVersion
    Write-Host  $env:variables.MinorVersion%
    Write-Host "$env:MY_MAPPED_PACKAGEVERSION"
    Write-Host "$env:MY_MAPPED_REVVERSION"

Output only displays the unevaluated content of the variables: enter image description here

Briefkasten
  • 1,964
  • 2
  • 25
  • 53

1 Answers1

1

Can someone tell me how I can access the variable $MinorVersion in the task script / powershell to get the actual value of it?

Az devops doesn't have such System variables or Predefined variables like $(Year:yy) and $(Month). The available predefined variables are defined here, we can't expand $(Year:yy) and $(Month) cause they're not predefined. As for value: '$(BuildID)', the correct format of BuildID is $(Build.BuildID) instead of $(BuildID).

The $(Date:yyyyMMdd) and $(Rev:.r) are something that only valid for Build Number(name for yaml pipeline), related document here. So normal variables can't expand the value of them, only the name element can recognize them. You should use:

name: $(BuildDefinitionName)_$(Date:yyyyMMdd)$(Rev:.r)

Instead of wrong format:

name: $(BuildDefinitionName)_$(MajorVersion).$(MinorVersion).xxx

enter image description here

Workaround to get/define variables with value of Date time:

Though the variables of Date time are not predefined, we can put one PowerShell step at the start of steps to define those variables manually. You can check link1 and link2 for more details.

So if you want to define data-related variables, you can check set variables in scripts:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $year=$(Get-Date -Format 'yyyy')
      Write-Host "##vso[task.setvariable variable=MajorVersion]$year"
      $month=$(Get-Date -Format 'MM')
      Write-Host "##vso[task.setvariable variable=MinorVersion]$month"
      $day=$(Get-Date -Format 'dd')
      Write-Host "##vso[task.setvariable variable=DayOfMonth]$day"


- powershell: |
    Write-Host  $(MajorVersion)
    Write-Host  $(MinorVersion)
    Write-Host  $(DayOfMonth)

Output of second PS task:

enter image description here

LoLance
  • 25,666
  • 1
  • 39
  • 73