【问题标题】:NuGet release managementNuGet 发布管理
【发布时间】:2015-01-28 07:16:36
【问题描述】:

我第一次做了一些我想在 NuGet 上分享的东西,但我不确定这个“发布准备”是如何完成的。

目前我的以下步骤是:

  1. 手动将AssemblyInfo.cs中我的主程序集(MyAssembly)的程序集版本从1.0.0.0更改为1.1.0.0
  2. 在 Visual Studio 中构建主程序集发布版本
  3. 为我的主程序集打包发行版 (nuget pack -prop configuration=release == MyAssembly.1.1.0.0.nupkg)
  4. 将此包发布到 NuGet (nuget push MyAssembly.1.1.0.0.nupkg)
  5. 为 packages.config 中定义的引用 MyAssembly 的程序集更新 NuGet 包(例如,MyAssembly.Web.Mvcv1.0.0.0 更新其 MyAssembly -> v1.1.0.0 使用 NuGet 包管理器)在 packages.config 中定义
  6. MyAssembly.Web.MvcAssemblyInfo.cs版本更改为1.1.0.0,构建发布,NuGet打包发布
  7. 对我的所有引用程序集重复第 6 步

这是一个 PITA 的一些步骤,我总有一天会出错并破坏发布。

这让我想到,我是不是漏掉了一个关键点,我做错了吗?

更新

根据@ialekseev 提供的知识,我现在创建了这个:

构建.ps1

Param(
    [Parameter(Mandatory=$true)]
    [string]$version,
    [string]$configuration = "Release",
    [boolean]$tests = $false,
    [boolean]$publish = $false,
    [boolean]$pack = $false,
    [string]$outputFolder = "build\packages"
)

# Include build functions
. "./BuildFunctions.ps1"

# The solution we are building
$solution = "NerveFramework.sln"
$assemblies = "NerveFramework", "NerveFramework.Web", "NerveFramework.Web.Mvc", "NerveFramework.Web.WebApi"

# Start by changing the assembly version
Write-Host "Changing the assembly versions to '$version'..."
$assemblyInfos = Get-ChildItem $assemblies -Filter "AssemblyInfo.cs" -Recurse | Resolve-Path -Relative
foreach ($assemblyInfo in $assemblyInfos) {
    ChangeAssemblyVersion $assemblyInfo $version
}

# Build the entire solution
Write-Host "Cleaning and building $solution (Configuration: $configuration)"
BuildSolution $solution $configuration

# Change dependency version on all depending assemblies
Write-Host "Changing the NerveFramework(s) NuGet Spec version dependencies to '$version'..."
$nuspecs = Get-ChildItem $assemblies -Filter "NerveFramework*.nuspec" -Recurse | Resolve-Path -Relative
foreach ($nuspec in $nuspecs) {
    ChangeNugetSpecDependencyVersion $nuspec "NerveFramework" $version
} 

# Pack the assemblies and move to output folder
if ($pack) {
    Write-Host "Packaging projects..."
    $projects = Get-ChildItem $assemblies -Filter "NerveFramework*.csproj" -Recurse | Resolve-Path -Relative
    foreach ($project in $projects) {
        PackProject $project $configuration $outputFolder
    } 
}

# Publish the assemblies
if ($publish) {
    Write-Host "Publishing packages..."
    $packages = Get-ChildItem $outputFolder -Filter "*$version.nupkg" -Recurse | Resolve-Path -Relative
    foreach ($package in $packages) {
        PublishPackage $package
    } 
}

BuildFunctions.ps1

Function BuildSolution() {

    Param(
        [Parameter(Mandatory=$true)]
        [string]$solution,
        [Parameter(Mandatory=$true)]
        [string]$configuration
    )

    # Set the path to the .NET folder in order to use "msbuild.exe"
    $env:PATH = "C:\Windows\Microsoft.NET\Framework64\v4.0.30319"

    Invoke-Expression "msbuild.exe $solution /nologo /v:m /p:Configuration=$configuration /t:Clean"
    Invoke-Expression "msbuild.exe $solution /nologo /v:m /p:Configuration=$configuration /clp:ErrorsOnly"
}

Function ChangeAssemblyVersion() {

    Param(
        [Parameter(Mandatory=$true)]
        [string]$filePath,
        [Parameter(Mandatory=$true)]
        [string]$publishVersion
    )

    Write-Host "-- Updating '$filePath' to version '$publishVersion'"

    $assemblyVersionPattern = 'AssemblyVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)'
    $assemblyVersion = 'AssemblyVersion("' + $publishVersion + '")';
    $assemblyFileVersionPattern = 'AssemblyFileVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)'
    $assemblyFileVersion = 'AssemblyFileVersion("' + $publishVersion + '")';

    (Get-Content $filePath -Encoding utf8) | ForEach-Object { 
            % { $_ -Replace $assemblyVersionPattern, $assemblyVersion } |
            % { $_ -Replace $assemblyFileVersionPattern, $assemblyFileVersion } 
    } | Set-Content $filePath
}

Function ChangeNugetSpecDependencyVersion() {

    Param(
        [Parameter(Mandatory=$true)]
        [string]$filePath,
        [Parameter(Mandatory=$true)]
        [string]$packageId,
        [Parameter(Mandatory=$true)]
        [string]$publishVersion
    )

    [xml] $toFile = (Get-Content $filePath)
    $nodes = $toFile.SelectNodes("//package/metadata/dependencies/dependency[starts-with(@id, $packageId)]")
    if ($nodes) {
        foreach ($node in $nodes) {
            $nodeId = $node.id
            Write-Host "-- Updating '$nodeId' in '$filePath' to version '$publishVersion'"
            $node.version = "[" + $publishVersion +"]"
            $toFile.Save($filePath)
        }
   }
}

Function PackProject() {

    Param(
        [Parameter(Mandatory=$true)]
        [string]$project,
        [Parameter(Mandatory=$true)]
        [string]$configuration,
        [Parameter(Mandatory=$true)]
        [string]$outputFolder
    )

    if (!(Test-Path -Path $outputFolder)) {
        New-Item $outputFolder -Type Directory
    }

    Write-Host "-- Packaging '$project'"
    Invoke-Expression ".nuget\NuGet.exe pack $project -OutputDirectory '$outputFolder' -Prop Configuration=$configuration"
}

Function PublishPackage() {

    Param(
        [Parameter(Mandatory=$true)]
        [string]$package
    )

    Write-Host "-- Publishing '$package'"
    Invoke-Expression ".nuget\NuGet.exe push $package"

}

.\Build -version 1.2.0.0 -pack $true一样运行

【问题讨论】:

  • 我们使用构建服务器和自定义构建任务来升级版本。构建服务器在签入时发布 nuget。
  • 有没有更多的信息可以分享给像我这样的菜鸟?
  • 菜鸟自己...我没有设置它
  • 我只能告诉你的是我们使用 teamcity 和 proget
  • 在这里我找到了一些灵​​感:stackoverflow.com/questions/9180904/…

标签: c# .net nuget nuget-package


【解决方案1】:

您需要使您的程序自动化,因为,是的,手动操作是非常容易出错的过程。

我通常编写可执行以下步骤的 powershell/cmd 脚本:

  1. 构建解决方案
  2. 对其进行测试
  3. 更新程序集版本
  4. 创建 NuGet 包
  5. 发布 NuGet 包

然后您可以在本地运行它以自动完成所有这些步骤。您也可以将一些任务委托给您的构建服务器(如果有的话)。例如。 Teamcity 可以负责构建、运行测试,甚至将包发布到 NuGet。无论如何,我喜欢在我的源代码管理中包含所有这些脚本,以便能够在我想要的任何时候在本地运行它们。如果您有兴趣,可以查看我在GitHub 上为我的一个项目编写的脚本。脚本位于 build 文件夹中,publish_local.cmd 是一个入口点。项目有多个 NuGet 依赖项,因此,我相信它与您要完成的任务类似。

我知道最好在答案中直接提供示例,但是这里包含的脚本太多了。如果您有任何问题,请成为我的客人。

【讨论】:

  • 有趣,我看看我能不能解决这个问题。但乍一看,我似乎在做我需要做的事。如果我遇到问题,我会让你知道,但这似乎很好! :-)
  • 您好,我已经用 powershell 脚本更新了我的问题,但不知何故,依赖于我的其他程序集的程序集的构建过程无法构建 - 说缺少程序集或参考。有什么建议么?如果你有时间可以在这里查看我的项目:github.com/janhartmann/nerve-framework
  • @janhartmann,我刚刚分叉了你的项目,我无法在 VS 中构建它,因为 NerveFramework.Web.WebApi 没有引用 NerveFramework.Web。在您的解决方案中,您应该在项目之间有 引用以正确构建它们。但是在创建 NuGet 包时,您可以只创建包 ...WebApi 并在 nuspec 文件中指定 ...Web 包作为 NuGet 依赖项(假设 ...Web 也出现在 NuGet 中)。
  • 该死的。我的错。请继续关注,我需要先开始工作才能解决这个问题。
  • 我第一次发布东西时,我应该把它作为参考添加到“添加参考...”对话框中吗?很抱歉脱离了上下文。
猜你喜欢
  • 2018-01-31
  • 1970-01-01
  • 1970-01-01
  • 2018-10-17
  • 2020-03-31
  • 2010-11-05
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
相关资源
最近更新 更多