首页 > 解决方案 > 在一个管道中构建项目并创建 NuGet 包

问题描述

我在 C# 中有一个带有 2 个类库的解决方案。

在此处输入图像描述

我在 Azure DevOps 中有一个构建和发布一个 NuGet 包的管道,它正在工作。YAML 是

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

trigger:
- main

pool:
  vmImage: ubuntu-latest

steps:
- task: DotNetCoreCLI@2
  displayName: Restore packages
  inputs:
    command: 'restore'
    projects: '**/*.csproj'
    feedsToUse: 'select'
- task: DotNetCoreCLI@2
  displayName: Build project
  inputs:
    command: 'build'
    projects: '**/*.csproj'
- task: DotNetCoreCLI@2
  displayName: Run tests
  inputs:
    command: 'test'
    projects: '**/*[Te]ests/*.csproj'
- task: NuGetCommand@2
  displayName: Prepare the package
  inputs:
    command: 'pack'
    packagesToPack: '**/*.csproj'
    versioningScheme: 'byEnvVar'
    versionEnvVar: 'PackageVersion'
- task: NuGetCommand@2
  displayName: Push the package
  inputs:
    command: 'push'
    packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
    nuGetFeedType: 'internal'
    publishVstsFeed: 'xxx'

当我有 2 个项目时,这个管道失败了。我注意到在构建步骤中,项目是在一个Debug文件夹中创建的。

在此处输入图像描述

在“准备包”中,管道正在Azure DevOps的文件夹中搜索.dll 。Release

在此处输入图像描述

然后,我有几个问题:

标签: azure-devopsazure-pipelines

解决方案


所以你应该提供--configuration Release你的构建步骤。由于您没有提供它,因此将 Debug 视为默认值,并将 Release 视为默认值。

variables:
  buildConfiguration: 'Release'

steps:
- task: DotNetCoreCLI@2
  displayName: Restore packages
  inputs:
    command: 'restore'
    projects: '**/*.csproj'
    feedsToUse: 'select'
- task: DotNetCoreCLI@2
  inputs:
    command: 'build'
    arguments: '--configuration $(buildConfiguration)'
    projects: '**/*.csproj'
  displayName: 'dotnet build $(buildConfiguration) --no-restore'

- task: DotNetCoreCLI@2
  inputs:
    command: test
    projects: '**/*[Te]ests/*.csproj'
    arguments: '--configuration $(buildConfiguration) --no-build'

- task: DotNetCoreCLI@2
  displayName: 'dotnet pack'
  inputs:
    command: 'pack'
    projects: '**/*.csproj'
    arguments: '-o $(Build.ArtifactStagingDirectory)/Output --no-build'
    versioningScheme: 'byEnvVar'
    versionEnvVar: 'PackageVersion'
- task: DotNetCoreCLI@2
  displayName: "Publish packages"
  inputs:
    command: 'push'
    packagesToPush: '$(Build.ArtifactStagingDirectory)/Output/*.*nupkg'
    nuGetFeedType: 'internal'
    publishVstsFeed: 'guid'

回答您的其他问题:

是否可以使用相同的管道创建和发布多个包?

是的

我正在使用 Visual Studio 2021,但尚未创建任何 .nuspec。我必须创建它们吗?


推荐阅读