AzureDevOps - 无法作为单个文件发布

AzureDevOps - Can't publish as single file

编辑:请参阅 post 末尾了解工作 yaml。

我正在尝试将 .NET Core 3.1 控制台应用程序发布为单个文件,但我似乎无法通过 Azure Devops Pipelines 取得成功:

自包含与否,发布结果总是一堆dll而不是我的可执行文件。

在我的电脑上以单一方式发布效果很好(使用我在下面的 yaml 中设置的参数从 VS 或 .NET Core CLI 发布)

这是负责构建的 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:
- feature/linux

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: UseDotNet@2
  displayName: 'Use .Net Core sdk 3.1.x'
  inputs:
    version: 3.1.x

- task: DotNetCoreCLI@2
  displayName: 'dotnet restore'
  inputs:
    command: restore
    projects: './Project/Project.csproj'
    feedsToUse: config
    nugetConfigPath: ./NuGet/NuGet.Config

- task: DotNetCoreCLI@2
  displayName: 'dotnet publish'
  inputs:
    projects: './Project/Project.csproj'
    arguments: '-o $(build.artifactstagingdirectory) -r linux-x64 -c Release -f netcoreapp3.1 -p:PublishSingleFile=true -p:SelfContained=false'

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: drop'

解法:

我傻了,简直忘记了dotnet publish下的command部分。。生成的命令默认是build。 请注意,您还需要指定

publishWebProjects: false

对于控制台项目,请参阅完整的工作 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:
- feature/linux

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: DotNetCoreCLI@2
  displayName: 'dotnet restore'
  inputs:
    command: restore
    projects: '**/Project.csproj'
    feedsToUse: config
    nugetConfigPath: ./NuGet/NuGet.Config

- task: DotNetCoreCLI@2
  displayName: 'dotnet publish'
  inputs:
    command: 'publish'
    publishWebProjects: false
    projects: '**/Project.csproj'
    modifyOutputPath: true
    arguments: '-o $(build.artifactstagingdirectory) -r linux-x64 -c Release -f netcoreapp3.1 -p:PublishSingleFile=true -p:SelfContained=false'

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: drop'
  inputs:
    artifactName: 'drop' 
    PathtoPublish: '$(build.artifactstagingdirectory)'

欢迎任何帮助:)

如果相同的命令在本地和远程服务器上产生不同的结果,那就太奇怪了。 你用同一个SDK,没有任何意义。问题出在管道的其他地方。我会建议以下解决方案。

1) 尝试在发布构建工件时明确说明要发布哪个文件夹,它似乎正在尝试发布 linux-x64 文件夹(向上一个文件夹)。喜欢:

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact'
  inputs:
    artifactName: 'drop' 
    PathtoPublish: '$(build.artifactstagingdirectory)'

2) 尝试将 PublishSingleFile 改为 zipAfterPublish。喜欢:

- task: DotNetCoreCLI@2
  displayName: Publish
  inputs:
    command: publish
    modifyOutputPath: true
    arguments: '--configuration $(BuildConfiguration) --output "$(build.artifactstagingdirectory)"'
    zipAfterPublish: true

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact'
  inputs:
projects: './Project/Project.csproj'
    artifactName: 'drop' 
    PathtoPublish: '$(build.artifactstagingdirectory)'

希望对您有所帮助...

更新答案

您需要在发布任务中指定命令,如命令:publish。

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:
- feature/linux

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: DotNetCoreCLI@2
  displayName: 'Restore Nuget Packages'
  inputs:
    command: 'restore'
    projects: './Project/Project.csproj'
    feedsToUse: config
    nugetConfigPath: ./NuGet/NuGet.Config

- task: DotNetCoreCLI@2
  displayName: 'dotnet publish'
  inputs:
    command: publish
    modifyOutputPath: true
    arguments: '--configuration Release --output "$(build.artifactstagingdirectory)"'
    zipAfterPublish: true

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: drop'
  inputs:
    artifactName: 'drop' 
    PathtoPublish: '$(build.artifactstagingdirectory)'

通过在发布任务中明确添加发布命令,我确认创建了一个 zip 文件作为工件。

我测试了你的 .yml 文件,日志显示 dotnet build 而不是 dotnet publish。您可以尝试以下代码:

- task: DotNetCoreCLI@2
  inputs:
    command: 'publish'
    projects: '**/*.csproj'
    arguments: '--configuration Release --output "$(build.artifactstagingdirectory)"'
    modifyOutputPath: true
    zipAfterPublish: true

Or


- task: DotNetCoreCLI@2
  inputs:
    command: 'custom'
    projects: '**/*.csproj'
    custom: 'publish'
    arguments: '--configuration Release --output $(build.artifactstagingdirectory)'