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

早过忘川 提交于 2020-02-03 04:39:45

问题


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, 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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/36453360/tfs-2015-builds-is-it-possible-to-use-variables-in-repository-mappings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!