Add pre-release tag to nuget package with octopack

Hi,

I am looking for a way to add a pre-release tag to the nuget version number within the build definition for a development branch.

Something along like the following when calling MSBuild:
/p:OctoPackPackageVersion=<$version_number_set_in_assembly_info>-alpha

So that:

[assembly: AssemblyVersion("1.0.*")]
becomes:
1.0.5236.25553-alpha

What is the best way of doing this please?

Thanks,
Phil

Hi Philip,

Thanks for getting in touch. Unfortunately, there isn’t a way to achieve this out-of-the-box. The best way to achieve this is to manually edit your csproj file to add the pre-release tag. I added the following snippet to my csproj file above the <Target Name="EnsureOctoPackImported" ... > element.

  <Target Name="CustomOctoPackVersion" BeforeTargets="EnsureOctoPackImported">

    <GetAssemblyVersionInfo AssemblyFiles="$(TargetPath)">
      <Output TaskParameter="AssemblyVersionInfo" ItemName="AssemblyVersions"/>
    </GetAssemblyVersionInfo>

    <PropertyGroup>
      <OctoPackPackageVersion>%(AssemblyVersions.Version)-alpha</OctoPackPackageVersion>
    </PropertyGroup>

  </Target>

Hope this helps!

Rob

Thanks Rob, I’ll give it a go.

Hi Rob,

Is there more to this than you included in the post?
I’m getting the error attached.

Do I need anything other than [assembly: AssemblyVersion("1.2.*")] in my AssemblyInfo.cs ?

Would adding .nuspec file with the following version replacement token work also:

<version>$version$-alpha</version>

Hi Philip,

Thanks for following-up. I hit a similar issue when I was testing out the solution and it was caused by the placement of the new xml snippet. I just tested this with a new project and it seems to work well.

  • My .csproj file looks like this. The key is the placement of the new target xml snippet.
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  ...

  <ItemGroup>
    <None Include="App.config" />
    <None Include="packages.config" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <Import Project="..\packages\OctoPack.3.0.45\tools\OctoPack.targets" Condition="Exists('..\packages\OctoPack.3.0.45\tools\OctoPack.targets')" />

  <Target Name="CustomOctoPackVersion" BeforeTargets="EnsureOctoPackImported">

      <GetAssemblyVersionInfo AssemblyFiles="$(TargetPath)">
        <Output TaskParameter="AssemblyVersionInfo" ItemName="AssemblyVersions"/>
      </GetAssemblyVersionInfo>

      <PropertyGroup>
        <OctoPackPackageVersion>%(AssemblyVersions.Version)-alpha</OctoPackPackageVersion>
      </PropertyGroup>

  </Target>

  <Target Name="EnsureOctoPackImported" BeforeTargets="BeforeBuild" Condition="'$(OctoPackImported)' == ''">
    <Error Condition="!Exists('..\packages\OctoPack.3.0.45\tools\OctoPack.targets') And ('$(RunOctoPack)' != '' And $(RunOctoPack))" Text="You are trying to build with OctoPack, but the NuGet targets file that OctoPack depends on is not available on this computer. This is probably because the OctoPack package has not been committed to source control, or NuGet Package Restore is not enabled. Please enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
    <Error Condition="Exists('..\packages\OctoPack.3.0.45\tools\OctoPack.targets') And ('$(RunOctoPack)' != '' And $(RunOctoPack))" Text="OctoPack cannot be run because NuGet packages were restored prior to the build running, and the targets file was unavailable when the build started. Please build the project again to include these packages in the build. You may also need to make sure that your build server does not delete packages prior to each build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
  </Target>
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>
  • My project AssemblyInfo.cs file looks like this. Note I removed the AssemblyFileVersion attribute so it picks up the AssemblyVersion.
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Spike.OctopackAlphaTag")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Spike.OctopackAlphaTag")]
[assembly: AssemblyCopyright("Copyright ©  2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("ffc995ff-7ed4-4582-a2e7-b4dc16b9ea18")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.*")]
  • I’m building my solution with the following command.
    msbuild Spike.OctopackAlphaTag.sln /t:Build /p:RunOctoPack=true

Lastly, using a custom .nuspec file with version replacement as you suggested will likely work as well however I haven’t tested it.

Let me know how you go!

Rob