如何将.net核心控制台应用程序发布到(私有)nuget存储库,并用Chocolatey安装它?

时间:2019-02-16 20:56:15

标签: nuget chocolatey

关于dotnet.exe nuget.exechocolatey,有很多全面而详细的文档,但是我找不到关于一个常见需求的简单明了的教程:推送.NET Core控制台应用程序到私有的Nuget存储库,并与Chocolatey一起安装。一个。

1 个答案:

答案 0 :(得分:2)

  1. 定义环境变量以使其更容易传递值(用实际值代替):
$version = "1.2.3"
$apiKey = "1234123412341234"
$repository = "https://your.repository.manager:8081/repository/repo-name/"
  1. 构建并发布您的应用程序。这样会将DLL(和其他项目项)创建为类似<path to your project>\bin\Release\netcoreapp2.2\publish的东西。
dotnet publish -c Release /p:version=$version
  1. 创建nuspec文件(将值替换在方括号中):
<?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. 创建安装文件。 Chocolatey将使用它们来安装和卸载您的应用程序。首先,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]"

然后是chocolateyuninstall.ps1

$ErrorActionPreference = 'Stop'

Uninstall-BinFile [executable name, e.g. my-tool]
  1. 创建nuget包:
choco pack "[path to your nuspec file created in step 3]" --version $version
  1. 将您的包推送到存储库中:
choco push "[path to the nuget package created in step 5]" -k $apiKey -s $repository

*添加--force,如果您的私人nuget仓库不在https后面

  1. 使用Chocolatey安装(或升级)程序
choco upgrade [your package id] -y -s $repository

现在准备好了!您可以使用chocolateyinstall.ps1文件中定义的可执行文件名称来运行它,例如my-tool --version