0

I am using the GitInfo nuget package from https://github.com/kzu/GitInfo to provide branch and commit information within my web application.

I have a publish profile set up to deploy to the filesystem and I'd like to include the git branch name in the path. I tried changing this line in the pubxml file:

   <publishUrl>C:\Development\Web\Publish_$(GitBranch)</publishUrl>

...however the '$(GitBranch)' was ignored (website was successfully published to 'Publish_' folder). I am publishing using the 'Publish...' wizard in Visual Studio 2015.

My website vbproj file has the following imports and targets:

  <Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />
  <PropertyGroup>
    <PreBuildEvent />
    <PostBuildEvent />
  </PropertyGroup>
  <Import Project="packages\GitInfo.2.0.3\build\GitInfo.targets" Condition="Exists('packages\GitInfo.2.0.3\build\GitInfo.targets')" />
  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('packages\GitInfo.2.0.3\build\GitInfo.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\GitInfo.2.0.3\build\GitInfo.targets'))" />
  </Target>
  <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v‌​$(MSBuildToolsVersio‌​n)\WebApplications\M‌​icrosoft.WebApplicat‌​ion.targets" Condition="false" />

Is what I want to do possible?

Daz
  • 2,833
  • 2
  • 23
  • 30

1 Answers1

1

Is what I want to do possible?

Open your website vbproj file after installed the NuGet package, you would notice that below code line:

<Import Project="packages\GitInfo.2.0.3\build\GitInfo.targets" Condition="Exists('packages\GitInfo.2.0.3\build\GitInfo.targets')" />

NuGet imported .targets files into project file that contain items, properties, targets, and tasks for GitInfo. These imported variables can only be accessed during build process. When you access it during the publish, you will get a null value. That is the reason why the value $(GitBranch) was ignored and website published to Publish_ folder.

As a workaround, you can custom a target to set the value $(GitBranch) to the environment variable, so that you can access it anytime.

See How to set envrionment variables in MSBuild file ? for more detail information.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • Thanks for your answer. I added the 'UsingTask' and a Target to set an environment variable. I can see the variable has been set correctly from a command prompt. I've used the var in the pubxml: `C:\Development\Web\Publish_$(GITINFO_GITCOMMIT)`. However the variable still isn't recognised from either the VS wizard or performing the publish with MSBuild. This page seems to confirm this should all work and the format is correct: https://programmaticponderings.com/tag/publish-profiles/ I think I am going to forget it for a couple of hours and have another go later! – Daz Oct 20 '17 at 09:50
  • I got further with this, although it doesn't 100% answer my use case as described in the question, I'm going to mark it as the correct answer (for now!)The Visual Studio publish wizard will pick up the environment variable in the publishUrl if that variable is already present when the IDE is opened, but isn't updated if I make a further commit before publishing. – Daz Oct 25 '17 at 12:20