Azure-devops-docs: Build Number Date Format options

Created on 1 May 2018  Â·  9Comments  Â·  Source: MicrosoftDocs/azure-devops-docs

I am trying to customize the date format in the build number. Where are the format docs for that option? For example, I want my build numbers to appear as 2018.5.1.5 and not 2018.05.01.5. But when I try $(date:yyyy).$(date:M).$(date:d)$(rev:.r) I get an unexpected and erroneous format.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Pri1 devops-cictech devopprod doc-bug unspecifiesvc

Most helpful comment

I needed to customize the build number, but slightly differently as I needed to start at a different $rev.

If you create an Inline Powershell task as the first task in the build, you can customize the build number pretty much any way you want to. This code block should get you the format you want.

$dottedDate = (Get-Date).ToString("yyyy.M.d")
$previousBuildNumber = $($env:BUILD_BUILDNUMBER) 

$BuildNumberParts = $($env:BUILD_BUILDNUMBER) -split '\.' 
$TFSRevision = [int]$BuildNumberParts[$BuildNumberParts.Length-1] 
$newBuildNumber = "$dottedDate.$TFSRevision"

# This will be what sets the build number
Write-Host "##vso[build.updatebuildnumber]$newBuildNumber " 

Write-Host "Updated  build number from '$previousBuildNumber' to '$newBuildNumber "

The build will start with the old number, but after it executes this task it will have the build number you want to have.

This was the original script I found that pointed me in the right direction.

# https://stackoverflow.com/a/46195443/16391
#Set the BuildNumberOffset. (Change this to the difference between the TFS build number,
#and the number that your build needs.) 
$BuildNumberOffset = 5000

$previousBuildNumber = $($env:BUILD_BUILDNUMBER) 

#Don't change 
$BuildNumberParts = $($env:BUILD_BUILDNUMBER) -split '\.' 
$TFSRevision = [int]$BuildNumberParts[$BuildNumberParts.Length-1] 
$BuildNumberParts[$BuildNumberParts.Length-1] = ($TFSRevision + $BuildNumberOffset).ToString() 
$BuildNumber = [string]::Join(".", $BuildNumberParts) 
Write-Host "##vso[build.updatebuildnumber]$BuildNumber" 

Write-Host "Updated  build number from '$previousBuildNumber' to '$BuildNumber"

All 9 comments

I needed to customize the build number, but slightly differently as I needed to start at a different $rev.

If you create an Inline Powershell task as the first task in the build, you can customize the build number pretty much any way you want to. This code block should get you the format you want.

$dottedDate = (Get-Date).ToString("yyyy.M.d")
$previousBuildNumber = $($env:BUILD_BUILDNUMBER) 

$BuildNumberParts = $($env:BUILD_BUILDNUMBER) -split '\.' 
$TFSRevision = [int]$BuildNumberParts[$BuildNumberParts.Length-1] 
$newBuildNumber = "$dottedDate.$TFSRevision"

# This will be what sets the build number
Write-Host "##vso[build.updatebuildnumber]$newBuildNumber " 

Write-Host "Updated  build number from '$previousBuildNumber' to '$newBuildNumber "

The build will start with the old number, but after it executes this task it will have the build number you want to have.

This was the original script I found that pointed me in the right direction.

# https://stackoverflow.com/a/46195443/16391
#Set the BuildNumberOffset. (Change this to the difference between the TFS build number,
#and the number that your build needs.) 
$BuildNumberOffset = 5000

$previousBuildNumber = $($env:BUILD_BUILDNUMBER) 

#Don't change 
$BuildNumberParts = $($env:BUILD_BUILDNUMBER) -split '\.' 
$TFSRevision = [int]$BuildNumberParts[$BuildNumberParts.Length-1] 
$BuildNumberParts[$BuildNumberParts.Length-1] = ($TFSRevision + $BuildNumberOffset).ToString() 
$BuildNumber = [string]::Join(".", $BuildNumberParts) 
Write-Host "##vso[build.updatebuildnumber]$BuildNumber" 

Write-Host "Updated  build number from '$previousBuildNumber' to '$BuildNumber"

@StingyJack thank you for posting this tip! We've got it on the backlog to add this info to the docs.

@StingyJack 3h of googling -,- thanks!

This should give you what you want:
$(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)

Any update on official documentation for this? It would be nice to know the full set of available options.

Any update on official documentation for this? It would be nice to know the full set of available options.

I think the set of options are documented here: https://docs.microsoft.com/en-us/azure/devops/pipelines/process/run-number?view=azure-devops&tabs=yaml

I think the set of options are documented here: https://docs.microsoft.com/en-us/azure/devops/pipelines/process/run-number?view=azure-devops&tabs=yaml

Only provides a couple of examples, no definition of the actual format string.

Yeah, I would like to get the week of the year (as a formatted number from 01-52). If that is possible, it is not documented there.

Is there a way to get timestamp value rather than date format?
Would love a proper documentation of all the options supported.

Was this page helpful?
0 / 5 - 0 ratings