jpierson
jpierson

Reputation: 17374

How to generate a file with version control information (TFS) during build

I'm tired of not knowing what builds web application is built from when an issue is presented to me by QA. My proposed solution is to generate some static content as part of the build process that has useful information in it. Somebody could reference that file manually on the server and I think it would be nice to offer the ability the HTTP GET the content as json perhaps as part of the API.

Seems simple right? I've done a lot of looking into doing this with Subversion in the past but I'm not familiar with the options for TFS. I realize that I'm really looking for two things, one to harvest the TFS information such as the repository, branch, changeset number, ... as well as a way to write content to a file all within a standard build process. Are there any existing MSBuild targets or tools I can pull in using something like Nuget to ensure that this is done for all builds whether they are local dev builds or ones done through a CI server which don't require any extra configuration or steps by the dev or the one setting up the CI server environment? What I'm hoping is that this is a common enough pattern that maybe somebody has already done this exact thing and packaged it for reuse or that the pieces are simple enough to piece together in a way that can be repeated as a general pattern for various types of .NET projects.

UPDATE: I realized that one issue with the proposed solution is that whatever static file is generated, it would have to be added as a reference to the project in order to be picked up by the build process as content so that it is copied/published properly along with the rest of the web application. Perhaps a broken reference can be added to the project ahead of time that would be fulfilled after the project is built at least once. This is reminiscent of the old way that Package Restore used to work with NuGet.

Related resources:

Upvotes: 0

Views: 1078

Answers (2)

jpierson
jpierson

Reputation: 17374

Here is what I've come up with which is working for now:

  <Target Name="BeforeBuild">
    <PropertyGroup>
      <ProgramFiles32>$(MSBuildProgramFiles32)</ProgramFiles32> 
      <VS14Dir>$(ProgramFiles32)\Microsoft Visual Studio 14.0</VS14Dir>

      <TF Condition="Exists('$(VS14Dir)')">$(VS14Dir)\Common7\IDE\TF.exe</TF>
      <TfCommand>&quot;$(TF)&quot; vc info . &gt; version.txt</TfCommand>
    </PropertyGroup>
    <Message Importance="High" Text="Creating version.txt: $(TfCommand)" />
    <Exec Command="$(TfCommand)"></Exec>
  </Target>
  <ItemGroup>
    <Content Include="$(SolutionDir)\version.txt">
      <Link>%(Filename)%(Extension)</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

This invokes the tf.exe console application with the args vc info . which gets version control information for the current directory and then uses >> version.txt to pipe the output to a file. There are a probably a bunch of improvements that could be made on this but it's a start. Next, I'll have to find some way to include the generated version.txt file as content within the project.

UPDATE: It looks like tf info only gives info about the specified directory and not about the working copy. I'm looking around for alternatives again.

Upvotes: 0

Eddie Chen - MSFT
Eddie Chen - MSFT

Reputation: 29976

The version control information is already generated in environment variables, you just need to write some simple command to read them from the environment variables. Check following links to see if these information meets your requirement:

For vNext build: Predefined variables.

For XAML build: TF_BUILD environment variables.

To include the generated file, you can refer to this question for details: How do you include additional files using VS2010 web deployment packages?

Upvotes: 1

Related Questions