There's yet another way to set the build number without using the scripts. The advantage of this method is that the build name is set from the beginning, unlike the script that changes the original name during the build. And the sequence number generation is cleaner (see the $(Rev:r) piece that works only in the "name" property of the pipeline.
You must use the "format" expression instead of ($Variable) syntax, because the build name is created during the Compile stage when the $(Variable) syntax does not work yet. Otherwise the variables will not be evaluated and will go into the build name as $(Variable).
name: '$(buildNumber).$(Rev:r)'
variables:
majorVersion: '1'
minorVersion: '0'
${{ if eq(variables['Build.SourceBranch'], 'refs/heads/master') }}:
buildNumber: ${{ format('{0}.{1}', variables['majorVersion'], variables['minorVersion']) }}
${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
buildNumber: ${{ format('{0}.{1}.PR-{0}', variables['majorVersion'], variables['minorVersion'], variables['System.PullRequest.PullRequestId']) }}
More about expressions here: Azure Pipelines Reference