azure-devopsazure-pipelinesazure-pipelines-yaml

How to Format Azure DevOps YAML Pipeline Build Number with leading zeros?


I'm trying to customise the build numbers that each pipeline run gets with a counter and then formatting to 4 characters (leading zeros), ideally as hex as well but can work with just decimal.

I've tried the suggested: name: $[format('{0:D4}', counter(0))] but get the error An error occurred while loading the YAML build pipeline. The format specifiers 'D4' are not valid for objects of type 'Number' but this is the general form that I've been able to find examples of.


Solution

  • Shamrai's answer works fine but keep in mind that it will modify the initial build number somewhere after the pipeline starts.

    You can also try the following using special variable $(Rev) (see Run number):

    $(Rev:rrrr)
    

    But the first revision might not be 0001 if you already ran the pipeline before:

    Initial build number

    $(Rev:r) resets to 1 when any other part of the build number changes:

    name: $(Date:yyMMdd)-$(Rev:rrrr)
    

    Build number with date prefix

    So in the case above the revision will reset every day.

    Going back to your original problem, and if you need to start from 0001 the following workaround won't work - it's basically the same as using $(Rev:rrrr) if you have less than 1000 revisions:

    name: 0$(Rev:rrr)
    

    Build number workaround 1

    But changing the prefix to 1 resets the build number:

    name: 1$(Rev:rrr)
    

    Build number - changing prefix