Visual Studio Online (or Visual Studio Team Services, as it is now) supports variables and their expansion in most fields. For example, there is a $(BuildPlatform)
variable that may be used in the 'Platform' field in the Visual Studio Build task. There is also a $(Build.SourceBranchName) variable, this may be used for setting the build number format (ours is $(Build.SourceBranchName).$(Date:MM)$(Date:dd)$(rev:.r)).
There is also the possibility to define capabilities on build agents. A build definition may require certain capabilities to be present (called 'demands'). On queueing a build, only agents that support all demands are eligible to process the build.
Now, we have hosted some build servers on our own. We also have the requirement to support multiple versions of our product at the same time. For a specific version of the product, the build environment may not change. So, version 1 may just be built on build server 1, version 2 on build server 2 and so on.
This process is to be automated, depending on the value in $(Build.SourceBranchName), which may be "1", "2" and so on, depending on the current version branch in the git repo. A straighforward idea is to define some suited capability on a build server (e.g. "CanBuildVersion1" or "CanBuildVersion = 1"). We would then require this capability to be present by adding a demand that "CanBuildVersion$(Build.SourceBranchName)" exists, or that "CanBuildVersion = $(Build.SourceBranchName)". However, the variable is never expanded, not on the key side nor on the value side of the demand.
Is there a way?
No, the variable is never expanded outside the build/release environment. So you need to change the demand value manually to match the branch you selected when queue a new build.