Configure run or build numbers

Note

In Microsoft Team Foundation Server (TFS) 2018 and previous versions, build and release pipelines are called definitions, runs are called builds, service connections are called service endpoints, stages are called environments, and jobs are called phases.

You can customize how your pipeline runs are numbered. The default value for run number is $(Date:yyyyMMdd).$(Rev:r).

In YAML, this property is called name and is at the root level of a pipeline. If not specified, your run is given a unique integer as its name. You can give runs much more useful names that are meaningful to your team. You can use a combination of tokens, variables, and underscore characters.

name: $(TeamProject)_$(Build.DefinitionName)_$(SourceBranchName)_$(Date:yyyyMMdd)$(Rev:.r)

steps:
  - script: echo '$(Build.BuildNumber)' # outputs customized build number like project_def_master_20200828.1

YAML builds are not yet available on TFS.

Example

At the time a run is started:

  • Project name: Fabrikam

  • Pipeline name: CIBuild

  • Branch: master

  • Build ID/Run ID: 752

  • Date: May 5, 2019.

  • Time: 9:07:03 PM.

  • One run completed earlier today.

If you specify this build number format:

$(TeamProject)_$(Build.DefinitionName)_$(SourceBranchName)_$(Date:yyyyMMdd)$(Rev:.r)

Then the second run on this day would be named: Fabrikam_CIBuild_master_20190505.2

Tokens

The following table shows how each token is resolved based on the previous example. You can use these tokens only to define a run number; they don't work anywhere else in your pipeline.

Token Example replacement value
$(Build.DefinitionName) CIBuild

Note: The pipeline name must not contain invalid or whitespace characters.
$(Build.BuildId) 752

$(Build.BuildId) is an internal immutable ID that is also referred to as the Run ID. It is unique across the organization.
$(DayOfMonth) 5
$(DayOfYear) 217
$(Hours) 21
$(Minutes) 7
$(Month) 8
$(Rev:r) 2 (The third run will be 3, and so on.)

Use $(Rev:r) to ensure that every completed build has a unique name. When a build starts, if nothing else in the build number has changed, the Rev integer value is incremented by one.

If you want to show prefix zeros in the number, you can add additional 'r' characters. For example, specify $(Rev:rr) if you want the Rev number to begin with 01, 02, and so on. If you use a zero-padded Rev as part of a version numbering scheme, note that some pipeline tasks or popular tools, like NuGet packages, remove the leading zeros, which causes a version number mismatch in the artifacts that are produced.
$(Date:yyyyMMdd) 20090824

You can specify other date formats such as $(Date:MMddyy)
$(Seconds) 3
$(SourceBranchName) master
$(TeamProject) Fabrikam
$(Year:yy) 09
$(Year:yyyy) 2009

Variables

You can also use user-defined and predefined variables that have a scope of "All" in your number. For example, if you've defined My.Variable, you could specify the following number format:

$(Build.DefinitionName)_$(Build.DefinitionVersion)_$(Build.RequestedFor)_$(Build.BuildId)_$(My.Variable)

The first four variables are predefined. My.Variable is defined by you on the variables tab.

Expressions

If you use an expression to set the build number, you can't use some tokens because their values aren't set at the time expressions are evaluated. These tokens include $(Build.BuildId), $(Build.BuildURL), and $(Build.BuildNumber).

FAQ

How large can a run number be?

Runs may be up to 255 characters.

In what time zone are the build number time values expressed?

The time zone is UTC.

The time zone is the same as the time zone of the operating system of the machine where you are running your application tier server.

How can you reference the run number variable within a script?

The run number variable can be called with $(Build.BuildNumber). You can define a new variable that includes the run number or call the run number directly. In this example, $(MyRunNumber) is a new variable that includes the run number.

# Set MyRunNumber
variables: 
  MyRunNumber: '1.0.0-CI+$(Build.BuildNumber)'


steps:
- script: echo $(MyRunNumber) # display MyRunNumber
- script: echo $(Build.BuildNumber) #display Run Number

How can I set the build number dynamically with conditions?

You can use variables as part of your run number. In this example, the variable why changes depending on the Build.Reason and is used as part of the run number.

variables:
  ${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
    why: pr
  ${{ elseif eq(variables['Build.Reason'], 'Manual' ) }}:
    why: manual
  ${{ elseif eq(variables['Build.Reason'], 'IndividualCI' ) }}:
    why: indivci 
  ${{ else }}:
    why: other

name: $(TeamProject)_$(SourceBranchName)_$(why)_$(Date:yyyyMMdd)$(Rev:.r)

pool:
  vmImage: 'ubuntu-latest'

steps:
- script: echo '$(Build.BuildNumber)' ## output run number