Monday 30 July 2012

Automate Nuget Package Creation using MSBuild

There are lots of blogs on this topic, but none of them helped me achieve my goal 100%. To state my problem, I wanted a Continuous Integration strategy which meant that every class library I built for the other members of the team would be accessible via a Nuget package. Every checkin will 
  • Trigger a new version of the Class Library to be built
  • A nuget package created with the new version number of the class library included in the name of the package.
  • The package will be moved to a network shared location where the other team members can consume the package
In a previous blog post I describe how you can create an Assembly file and share it across all your projects in the solution.

Now we create a MSBuild file and add and call the following targets:

<Target Name="CreatePackage">
       <Exec WorkingDirectory="$(WorkingFolder)"
        Command="$(NuGetApp) spec -f" />      
       <Exec WorkingDirectory="$(WorkingFolder)"
        Command="$(NuGetApp) pack MyProject.csproj -Verbose -Prop Configuration=Release" />
</Target>

<Target Name="MovePackage">    
       <Exec WorkingDirectory="$(WorkingFolder)"
          Command="move /y *.nupkg $(PackageDir)" />
</Target>

The NugetApp variable represents the path to the Nuget.exe command line utility used to create the Nuget package. In my case, I downloaded the utility into a folder on the same level as the working folder and named it "Tools" with subfolder "Nuget", I set the NugetApp property to like so:

<NugetApp>$(WorkingFolder)/../Tools/Nuget/Nuget.exe</NugetApp>

The PackageDir property is whatever network share location of your choice.

Now run your your MSBuild file in TeamCity.

Happy building!!!.

No comments:

Post a Comment