J.G.Sable
J.G.Sable

Reputation: 1388

Getting date time in Azure DevOps yml and concat a version number with it

I have the following yml. What I'm attempting to do is to create a version number based on several other variables (version.Major, version.Minor, versionDay, and BuildNumber). But the set variable powershell task isn't overwriting the initial values for versionday, so the output of versionNumber in my echo script below would be like: 1.0.set below.2021037.6 Any ideas? Syntax issues I'm not seeing?

trigger:
  branches:
    include:
      - develop, 
      - sprint/*
      - Sprint/*

pool:
  name: 'MyCustomAgent'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  version.Major: '1'
  version.Minor: '0',
  versionDay: 'set below'
  versionNumber: 'set dynamically'

steps:
- script: |
    echo: $(Build.SourceBranch)

- powershell: |
      if ("$(Build.SourceBranch)".Contains('sprint')) {
        Write-Host "##vso[task.setvariable variable=buildConfiguration;]Release"
        [string] $currentMonthDay= (Get-Date -Format 'MMdd')
        Write-Host "##vso[task.setvariable variable=versionDay]$currentMonthDay"
        Write-Host "##vso[task.setvariable variable=versionNumber]$(version.Major).$(version.Minor).$(versionDay)"

      } else {
        Write-Host "##vso[task.setvariable variable=buildConfiguration;]Debug"
        [string] $currentMonthDay= (Get-Date -Format 'MMdd')
        Write-Host "##vso[task.setvariable variable=versionDay]$currentMonthDay"
        Write-Host "##vso[task.setvariable variable=versionNumber]$(version.Major).$(version.Minor).$(versionDay).$(Build.BuildNumber)"
      }      

- script: | 
    echo building configuration $(buildConfiguration)
    echo $(versionDay)
    echo $(versionNumber)

Upvotes: 2

Views: 362

Answers (1)

WaitingForGuacamole
WaitingForGuacamole

Reputation: 4301

You need to set the variables as output - note the change to your setvariable statements below:

pool:
  name: 'MyCustomAgent'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  version.Major: '1'
  version.Minor: '0',
  versionDay: 'set below'
  versionNumber: 'set dynamically'

steps:
- script: |
    echo: $(Build.SourceBranch)

- powershell: |
      # note the use of ;isOutput=true in setvariable commands
      if ("$(Build.SourceBranch)".Contains('sprint')) {
        Write-Host "##vso[task.setvariable variable=buildConfiguration;]Release"
        [string] $currentMonthDay= (Get-Date -Format 'MMdd')
        Write-Host "##vso[task.setvariable variable=versionDay]$currentMonthDay"
        # You can't use $(versionDay) here as setvariable is for subsequent steps/jobs/stages
        # use its calculated value instead
        Write-Host "##vso[task.setvariable variable=versionNumber]$(version.Major).$(version.Minor).$($currentMonthDay)"

      } else {
        Write-Host "##vso[task.setvariable variable=buildConfiguration;isOutput=true]Debug"
        [string] $currentMonthDay= (Get-Date -Format 'MMdd')
        Write-Host "##vso[task.setvariable variable=versionDay;isOutput=true]$currentMonthDay"
        # You can't use $(versionDay) here as setvariable is for subsequent steps/jobs/stages
        # use its calculated value instead
        Write-Host "##vso[task.setvariable variable=versionNumber;isOutput=true]$(version.Major).$(version.Minor).$($currentMonthDay).$(Build.BuildNumber)"
      }      

- script: | 
    echo building configuration $(buildConfiguration)
    echo $(versionDay)
    echo $(versionNumber)

Upvotes: 1

Related Questions