You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Pipeline构建MAUI Windows阶段报错NETSDK1018:无效NuGet版本字符串

MAUI应用Azure Pipeline Windows构建失败:NETSDK1018无效版本字符串

我有一个MAUI应用,本地机器上无论是Release还是Debug模式都能正常构建,但推送到GitHub仓库后,Azure Pipeline的Windows阶段构建步骤失败,报错信息如下:

C:\hostedtoolcache\windows\dotnet\sdk\9.0.102\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.GenerateAssemblyInfo.targets(226,5): error NETSDK1018: Invalid NuGet version string: '..'. [D:\a\1\s\src\MyProject\MyProject.csproj::TargetFramework=net9.0-windows10.0.19041.0]

Workload updates are available. Run dotnet workload list for more information.

##[error]Bash exited with code '1'.
Finishing: Build Windows MAUI app

这个Pipeline之前运行正常,现在不清楚哪里出了变化。我已经尽可能验证了版本的正确性,希望有人帮忙排查问题原因。

以下是Pipeline YAML配置:

trigger:
  batch: true
  branches:
    include:
    - master

variables:
  - group: PipelineVariables
  - group: GithubPackages
  - name: app_build_version
    value: $(app_version_major).$(app_version_minor).$(app_version_patch)

name: $(Build.DefinitionName).$(Year:yyyy).$(Month).$(DayOfMonth)$(Rev:.r)

stages:
  - stage: windows_and_android_stage
    displayName: Windows and Android Stage
    pool:
      name: Azure Pipelines
      vmImage: windows-latest
    jobs:
    - job: windows_job
      displayName: Windows job
      steps:
      - template: setup_template.yml

      - task: DotNetCoreCLI@2
        displayName: Restore Nugets and Packages
        inputs:
          command: 'restore'
          projects: '$(System.DefaultWorkingDirectory)/src/MyProject/MyProject.csproj'
          feedsToUse: 'config'
          nugetConfigPath: '$(System.DefaultWorkingDirectory)/nuget.config'
          externalFeedCredentials: 'GitHubNuGetSvcCnn'
          restoreDirectory: '$(System.DefaultWorkingDirectory)/NuGet'

      - task: Bash@3
        displayName: Build Windows MAUI app
        inputs:
          targetType: 'inline'
          script: |
            dotnet publish src/MyProject/MyProject.csproj -f net9.0-windows10.0.19041.0 -c Release --output '$(Build.ArtifactStagingDirectory)/Windows' -p:ApplicationDisplayVersion=$(app_build_version) -p:ApplicationVersion=$(app_version_code)

      - task: PublishBuildArtifacts@1
        displayName: 'Publish Windows app to artifacts'
        inputs:
          PathtoPublish: '$(Build.ArtifactStagingDirectory)/Windows'
          ArtifactName: 'Windows'

    - job: android_job
      dependsOn: [windows_job]
      displayName: Android job
      steps:
      - template: setup_template.yml

      - task: DownloadSecureFile@1
        name: keystore_file
        displayName: Download Keystore File
        inputs:
          secureFile: 'MyProjectMaui.keystore'

      - task: CopyFiles@2
        displayName: Import secure file
        inputs:
          SourceFolder: $(Agent.TempDirectory)
          Contents: 'MyProjectMaui.keystore'
          TargetFolder: src/MyProject

      - task: DotNetCoreCLI@2
        displayName: Restore Nugets and Packages
        inputs:
          command: 'restore'
          projects: '$(System.DefaultWorkingDirectory)/src/MyProject/MyProject.csproj'
          feedsToUse: 'config'
          nugetConfigPath: '$(System.DefaultWorkingDirectory)/nuget.config'
          externalFeedCredentials: 'GitHubNuGetSvcCnn'
          restoreDirectory: '$(System.DefaultWorkingDirectory)/NuGet'

      - task: Bash@3
        displayName: Build Android MAUI app
        inputs:
          targetType: 'inline'
          script: |
            dotnet publish src/MyProject/MyProject.csproj -f net9.0-android -c Release --output '$(Build.ArtifactStagingDirectory)/Android' -p:AndroidKeyStore=true -p:AndroidSigningKeyStore=MyProjectMaui.keystore -p:AndroidSigningKeyAlias=MauiAlias -p:AndroidSigningKeyPass=$(keystore_password) -p:AndroidSigningStorePass=$(keystore_password) -p:ApplicationDisplayVersion=$(app_build_version) -p:ApplicationVersion=$(app_version_code)

      - task: PublishBuildArtifacts@1
        displayName: 'Publish Android app to artifacts'
        inputs:
          PathtoPublish: '$(Build.ArtifactStagingDirectory)/Android'
          ArtifactName: 'Android'

  - stage: ios_stage
    displayName: iOS Stage
    pool:
      name: Azure Pipelines
      vmImage: macOS-13
    jobs:
    - job: ios_job
      displayName: iOS Job
      steps:
        - template: setup_template.yml

        - task: InstallAppleProvisioningProfile@1
          inputs:
            provisioningProfileLocation: 'secureFiles'
            provProfileSecureFile: MyProject.mobileprovision

        - task: InstallAppleCertificate@2
          inputs:
            certSecureFile: Distribution.p12
            keychain: 'temp'
            certPwd: $(ios_distribution_password)

        - task: DotNetCoreCLI@2
          displayName: Restore Nugets and Packages
          inputs:
            command: 'restore'
            projects: '$(System.DefaultWorkingDirectory)/src/MyProject/MyProject.csproj'
            feedsToUse: 'config'
            nugetConfigPath: '$(System.DefaultWorkingDirectory)/nuget.config'
            externalFeedCredentials: 'GitHubNuGetSvcCnn'
            restoreDirectory: '$(System.DefaultWorkingDirectory)/NuGet'

        - task: Bash@3
          displayName: Build iOS app
          inputs:
            targetType: 'inline'
            script: |
              dotnet publish src/MyProject/MyProject.csproj -f net9.0-ios -c Release --output '$(Build.ArtifactStagingDirectory)/iOS' -p:ArchiveOnBuild=true -p:RuntimeIdentifier=ios-arm64 -p:CodesignKey="$(codesign_key)" -p:CodesignProvision="$(codesign_provision)" -p:ApplicationDisplayVersion=$(app_build_version) -p:ApplicationVersion=$(app_version_code)

        - task: PublishBuildArtifacts@1
          displayName: 'Publish iOS app to artifacts'
          inputs:
            PathtoPublish: '$(Build.ArtifactStagingDirectory)/iOS'
            ArtifactName: 'iOS'

排查步骤

1. 确认Pipeline变量的有效性

报错里的..说明app_build_version拼接后出现了空段,大概率是PipelineVariables组里的app_version_major、app_version_minor、app_version_patch有未设置或为空的情况。在Pipeline的Windows job中添加打印变量的步骤,查看实际值:

- task: Bash@3
  displayName: 打印构建版本变量
  inputs:
    targetType: 'inline'
    script: |
      echo "app_build_version: $(app_build_version)"
      echo "app_version_major: $(app_version_major)"
      echo "app_version_minor: $(app_version_minor)"
      echo "app_version_patch: $(app_version_patch)"

2. 检查项目文件的版本配置

打开MyProject.csproj,查看是否有硬编码的Version、AssemblyVersion、PackageVersion等属性,确认格式是否正确,是否和Pipeline传入的参数冲突。

3. 修复Azure Agent的MAUI Workload

报错提示有Workload更新,Azure Windows代理可能存在Workload缺失或版本不兼容。在构建前添加Workload安装命令:

- task: Bash@3
  displayName: 安装MAUI Workload
  inputs:
    targetType: 'inline'
    script: |
      dotnet workload install maui --source https://api.nuget.org/v3/index.json

4. 替换Bash任务为PowerShell

Windows环境下Bash的路径解析可能有问题,改用PowerShell执行构建命令:

- task: PowerShell@2
  displayName: 构建Windows MAUI应用
  inputs:
    targetType: 'inline'
    script: |
      dotnet publish src/MyProject/MyProject.csproj -f net9.0-windows10.0.19041.0 -c Release --output '$(Build.ArtifactStagingDirectory)/Windows' -p:ApplicationDisplayVersion=$(app_build_version) -p:ApplicationVersion=$(app_version_code)

5. 回溯近期代码变更

检查最近提交的代码,是否修改了项目版本配置、NuGet源设置,或者引入了新依赖导致版本冲突。


内容的提问来源于stack exchange,提问作者Orgbrat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 22:40:56