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.
$version = "1.2.3"
$apiKey = "1234123412341234"
$repository = "https://your.repository.manager:8081/repository/repo-name/"
<path to your project>\bin\Release\netcoreapp2.2\publish
.dotnet publish -c Release /p:version=$version
<?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>
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]
choco pack "[path to your nuspec file created in step 3]" --version $version
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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With