Matt Waldron
Matt Waldron

Reputation: 314

Deploy a specific version of a NuGet package with Octopus

We have an automated build on TFS that creates a release in Octopus, which is autonomously deployed to a test environment.

As part of this build we provide a --defaultpackageversion value that lines up with our release roadmap, e.g. 4.1.1.1 - this version is assigned in the build template and all projects go into Octopus with that version correctly.

As part of this release though, there is one package that comes from our NuGet remote feed that doesn't reflect the release roadmap version, e.g. 1.1.0.0.

So, for every package Octopus deploys 4.1.1.1, but attempts to resolve 4.1.1.1 for this specific package, where only 1.1.0.0 exists.

There is an argument for octo.exe that allows us to provide a version for a given step, --package=StepName.

To provide the version for this step name, I would need to:

  1. Get the NuGet package ID
  2. Use NuGet to get the latest version for this package
  3. Pipe this version with the step name into octo.exe

This means having to specify (as part of the TFS build template) both the NuGet package ID and the Octopus Step that deploys said package. So if the step changed to deploy a different NuGet package, I'd have to reflect that change in each build template that provides this argument.

It seems to me, I should be able to:

  1. Tell Octopus to deploy a specific version of a NuGet package, without providing a particular step.
  2. Tell Octopus to automatically get the latest version of a NuGet package given a step name.

I can't see any arguments that would allow me to achieve either of the options above.

Does anyone know how to achieve either option above (or something similar)? Otherwise I will probably have to go down the route of specifying both step name and NuGet package ID in the build template (not ideal).

Upvotes: 2

Views: 1920

Answers (1)

Matt Waldron
Matt Waldron

Reputation: 314

Sorry for answering my own question, but wanted to add more detail for the solution we went with in the end.

So, I added a new argument to the build template "Deploy Latest NuGet Packages for Steps". This argument contains a comma delimited list of Octopus step/NuGet Package pairs - these are separated by a colon, e.g.

"Deploy Web Site:Company.Common,Deploy Database:Company.Project.Database".

The build activity splits the argument into respective pairs. It spins up a process with NuGet list to identify the latest version of each package. We then use a Regex to determine the latest version:

....
var output = p.StandardOutput.ReadToEnd();
var pattern = string.Format(@"^{0}\s(?<version>\d+\.\d+\.\d+\.\d+)\r?$", package.NuGetPackageId);

package.Version = Regex.Matches(output, pattern, RegexOptions.Multiline).
    OfType<Match>().
    Select(r => new Version(r.Groups["version"].Value)).
    Max();

where p is the NuGet process and package is a POCO with step name, NuGet package ID and version properties.

This specific step name, version pair is then provided to octopus using the --package argument, e.g.

--package "Deploy Web Site:4.1.1.1,Deploy Database:1.1.0.0"

Hope this is of some help to someone in the future.

Upvotes: 2

Related Questions