NuGet Pack with IncludeReferencedProject in VSTS using BuildNumber Versioning

I recently needed to create a NuGet package from a project that had some dependencies on some other projects. Normally this isn’t a huge deal the following command would take care of it for you.

nuget pack pathToYourProject.csproj -IncludeReferencedProjects

The VSTS NuGet task with the pack command doesn’t currently provide to pass the include referenced projects flag. I had to jump through a few hoops to get this to work so I thought I would share.

The first step is to add the NuGet Tool Installer task, all the default settings work fine. Next, after the NuGet Tool Installer task add the Run Inline Powershell task. This task takes a bit more configuration. In the Script to run field enter the following.


$version = $buildBuildNumber.Substring($buildBuildNumber.IndexOf("_") + 1)

Invoke-Expression "nuget pack 
                   -OutputDirectory $($buildArtifactStagingDirectory) 
                   -Properties Configuration=release -version $($version) 
                   -Verbosity Detailed 

Then in the Arguments field enter the following.

-buildSourcesDirectory $(Build.SourcesDirectory) 
-buildArtifactStagingDirectory $(Build.ArtifactStagingDirectory)
-buildBuildNumber $(Build.BuildNumber)

If it hadn’t been for the version above which is based the Build Number the custom command type of the NuGet task would have worked fine.

Also published on Medium.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.