Search code examples
azure-devopsyamlazure-pipelines

How to receive Revision in Azure Pipelines YAML build definition


I created a new build with Azure Pipelines (Azure DevOps) and it worked really well.

Usually, you use $(Rev:.r) to get the revision in the build. Unfortunately, it seems the variable isn't replaced/set in the build steps. The only place where you can use it is the name: property in the YAML document.

Now I set it in the name and extract it in some PowerShell, which isn't necessary if you can get it via an environment variable.

How do I get the Revision (like $(Rev)) in the new builds (outside of the name: property in the YAML document)?

(The Build Agents running on-premise, inside Docker - but this shouldn't affect the things above)


Solution

  • You can't get the revision number without parsing, it is not stored as a separate field somewhere or in an environment variable.

    The $(Rev:.r) portion instructs Azure DevOps to come up with the first number that makes the build number unique (and, in that specific example, put a dot in front of it).

    Like you said, the only way is to use PowerShell script to get the value:

    $buildNumber = $Env:BUILD_BUILDNUMBER
    $revision= $buildNumber.Substring($buildNumber.LastIndexOf('.') + 1)
    

    Edit:

    You can install the Get Revision Number extension that does it.