Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to publish a .net core console application to a (private) nuget repository and install it with chocolatey?

There is a lot of comprehensive and verbose documentation about dotnet.exe nuget.exe and chocolatey but I failed to find a simple and concise tutorial about one common need: to push a .NET Core console application to a private Nuget repository and install it with Chocolatey. Here comes one.

like image 250
Nikolai Koudelia Avatar asked Feb 16 '19 20:02

Nikolai Koudelia


Video Answer


1 Answers

  1. Define environment variables to make it easier to pass the values around (replace with the actual ones):
$version = "1.2.3"
$apiKey = "1234123412341234"
$repository = "https://your.repository.manager:8081/repository/repo-name/"
  1. Build and publish your application. This will create the DLLs (and other project items) to something like <path to your project>\bin\Release\netcoreapp2.2\publish.
dotnet publish -c Release /p:version=$version
  1. Create the nuspec file (replace the values in square brackets):
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
  <metadata>
    <id>[your package id]</id>
    <version>$version$</version>
    <title>[your package title]</title>
    <authors>[author(s)]</authors>
    <owners>[owner(s)]</owners>
    <projectUrl>[project url e.g. containing documentation]</projectUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>[description]</description>
    <copyright>[copyright]</copyright>
  </metadata>
  <files>
    <file src="chocolateyinstall.ps1" target="tools" />
    <file src="chocolateyuninstall.ps1" target="tools" />
    <file src="[path to the publish directory from step 2]\**" target="tools" />
  </files>
</package>
  1. Create the installation files. These will be used by Chocolatey to install and uninstall your application. First, chocolateyinstall.ps1:
$ErrorActionPreference = 'Stop'

$toolsDir   = "$(Split-Path -parent $MyInvocation.MyCommand.Definition)"
$defaultDotnetRuntimePath = "C:\Program Files\dotnet\dotnet.exe"

if (!(Test-Path $defaultDotnetRuntimePath))
{
    Write-Host -ForegroundColor Red "File not found: $defaultDotnetRuntimePath"
    Write-Host "The package depends on the .NET Core Runtime (dotnet.exe) which was not found."
    Write-Host "Please install the latest version of the .NET Core Runtime to use this package."
    exit 1
}

Install-Binfile -Name [executable name, e.g. my-tool] -Path "$defaultDotnetRuntimePath" -Command "$toolsDir\[name of your main dll, e.g. My.Awesome.Cli.Program.dll]"

and then chocolateyuninstall.ps1:

$ErrorActionPreference = 'Stop'

Uninstall-BinFile [executable name, e.g. my-tool]
  1. Create the nuget package:
choco pack "[path to your nuspec file created in step 3]" --version $version
  1. Push your package to the repository:
choco push "[path to the nuget package created in step 5]" -k $apiKey -s $repository

*add --force if your private nuget repo is not behind https

  1. Install (or upgrade) your program with Chocolatey:
choco upgrade [your package id] -y -s $repository

It's ready now! You can run it with the executable name defined in the chocolateyinstall.ps1 file, e.g. my-tool --version.

like image 177
Nikolai Koudelia Avatar answered Jan 02 '23 22:01

Nikolai Koudelia