Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TFS 2015 builds : Is it possible to use Variables in Repository mappings?

When creating a vNext build on TFS 2015 you can define variables, which are then used in build steps, and can also be used as environment variables in scripts the build runs.

The build I am working on runs scripts that pulls files from mapped locations, so it would be great if I could define a variable and use it in a mapping so that for example, if I update a reference in the project the build is building, I can simply update the variable with the new location and have the repository mappings and scripts all pull correctly from the new location without having to make the change in multiple places.

I have tried doing this by setting up the variable and mapping as follows, enter image description here enter image description here But this generates an error when you try to save the build complaining that there are two '$' characters in the mapping. Is there way to do this or is this not currently possible?

like image 229
Paul Stephen Brittain Avatar asked Apr 06 '16 13:04

Paul Stephen Brittain


People also ask

How to create a build definition in TFS 2015?

How To Create A Build Definition In TFS 2015, Adding Tasks And Setting Build Menu Options. A Build definition may be defined as a collection of steps/tasks which can be executed in a sequence as specified while creating a build definition. We can change a sequence of steps added in the build definition by dragging on the desired place of execution.

How to add tasks in Visual Studio builds?

These tasks can be added in the build definition. Below is a screenshot of adding tasks as per your build definition. We can select any number of tasks in the build definition. To get started, select the project from projects collection in TFS server and click on the Builds link in Visual Studio Team Explorer.

Can I use my own variables when queuing new builds?

In the same way, you can use your own variables. Also, there is a checkbox “Allow at Queue Time” specified in front of your each defined variable which means you can edit the value of these variables at the time of queuing new Build.


2 Answers

This has been causing me havok for quite a while as well.

For starters, there is a uservoice request for this feature. You can add your votes and input here to get Microsoft to allow this feature: https://visualstudio.uservoice.com/forums/330519-team-services/suggestions/14131002-allow-variables-in-repository-variables-and-trigg

Second, we've developed a workaround that gets us most of the way there. It's not perfect, but it might be useful to you if you're comfortable with the tradeoffs or can work around the deficiencies.

Start by turning off the "Label Sources" option of the build and mapping the Server Path field to you base build. You'll want to add a custom variable to the Build Definition to tell the build instance what TFS location to pull from. For example, we have a base project and then multiple branches from the project, so our source is structured like this

$\Team Project\Project1
$\Team Project\Project1Branch1
$\Team Project\Project1Branch2
$\Team Project\Project1Branch3

and we create a variable named "Branch" that we can set to "Branch1", "Branch2", and so forth.

When we want to build the base project, we leave the Branch variable blank when launching the build. For branch builds, we set it to the name of the branch we want to build.

Then our build steps look like this

  • Remap Workspace Folder to Branch Folder
  • Get Files for Specified Branch - We have to do this manually after remapping our workspace
  • Compile the Source in the Specified Branch
  • Publish Build Artifacts from the Specified Branch
  • Label the Code of the Specified Branch Manually

The Remap task runs the command

tf workfold "$/Team Project/Project1$(Branch)" "$(build.sourcesDirectory)\$(Build.DefinitionName)$(Branch)"

The Manual Get task runs the following command

get /recursive /noprompt "$/Team Project/Project1$(Branch)"

The build uses the Branch variable to point to the correct location of the solution file for the specified branch

$(build.sourcesDirectory)\$(Build.DefinitionName)$(Branch)\SolutionFile.sln

The Publish Artifacts task uses the Branch variable in both the Contents field and the Path field Example in Contents

**\$(Build.DefinitionName)$(Branch)\bin

The Label Code task uses the following command

tf label "$(build.buildNumber)" "$/Team Project/Project1$(Branch)" /recursive

The downside of this setup is that you don't capture Associated Changes and Work Items to your subsidiary branches as the Server Path field is always set to the main location. This may not be an issue if you always merge from your branches to your main location prior to launching a build meant to go to production. What you can do to compensate for this really depends on your use case.

With some tweaking, you could use this same format to specify full paths as well if you needed to.

like image 173
mattbbpl Avatar answered Sep 19 '22 10:09

mattbbpl


It's impossible. Just as the error message mentioned: there are two '$' characters in the mapping. Which means your application's path shouldn't vary from build to build.

Mappings on the Repository page are used to specify source control folder which contains projects that need to be built in the build definition. You can set it via clicking the Ellipsis (...) button, however, you can't include variables in the mapping path.

There is a similar question: Variables in TFS Mappings on Visual Studio Online Team Builds

like image 40
PatrickLu-MSFT Avatar answered Sep 17 '22 10:09

PatrickLu-MSFT