基于Azure Pipelines构建跨平台多项目MonoGame解决方案的问询
跨平台构建MonoGame项目的Azure Pipelines解决方案
嘿,针对你的MonoGame跨平台构建需求,**强烈推荐在单个Azure Pipelines配置文件中使用作业矩阵(Job Matrix)**来实现——这样既不用维护多个零散的配置文件,还能让不同平台的构建作业并行跑起来,效率拉满。下面是完整的可落地配置,包含你要的所有功能:
核心思路拆解
- 用
strategy.matrix定义所有要跑的构建组合:Windows上同时搞定DirectX和OpenGL项目,MacOS、Linux只构建OpenGL版本 - 针对不同平台适配MonoGame的安装方式(Windows用解压安装包,非Windows直接部署SDK压缩包)
- 明确指定要构建的单个项目(不再构建整个解决方案),彻底解决原来只构建默认启动项目的问题
- 保留GitVersion自动版本控制,还把生成的版本号传到构建步骤里,确保程序集版本正确
- 加了产物发布步骤,每个平台的构建结果都能单独存下来,方便后续取用
完整配置文件
trigger: branches: include: - '*' variables: buildConfiguration: 'Release' monoGameVersion: 'v3.7.1' solution: '**/*.sln' jobs: - job: Build strategy: matrix: # Windows平台:构建DirectX和OpenGL两个项目 Windows_DirectX: vmImage: 'windows-latest' buildPlatform: 'x86' targetProject: '**/AzureTest.DirectX.csproj' artifactName: 'Windows_DirectX_Release' Windows_OpenGL: vmImage: 'windows-latest' buildPlatform: 'x86' targetProject: '**/AzureTest.OpenGL.csproj' artifactName: 'Windows_OpenGL_Release' # MacOS平台:只构建OpenGL项目 MacOS_OpenGL: vmImage: 'macos-latest' buildPlatform: 'x64' targetProject: '**/AzureTest.OpenGL.csproj' artifactName: 'MacOS_OpenGL_Release' # Linux平台:只构建OpenGL项目 Linux_OpenGL: vmImage: 'ubuntu-latest' buildPlatform: 'x64' targetProject: '**/AzureTest.OpenGL.csproj' artifactName: 'Linux_OpenGL_Release' maxParallel: 4 # 最多并行4个作业,可根据你的并发配额调整 pool: vmImage: $(vmImage) steps: # 步骤1:用GitVersion生成自动版本号 - task: GitVersion@5 inputs: runtime: 'full' updateAssemblyInfo: true configFilePath: 'gitversion.yml' displayName: 'Generate version with GitVersion' # 步骤2:Windows平台安装MonoGame - script: | powershell -Command "(New-Object System.Net.WebClient).DownloadFile('https://github.com/MonoGame/MonoGame/releases/download/$(monoGameVersion)/MonoGameSetup.exe', '.\MonoGameSetup.exe')" mkdir -Force ./tmp/ & .\MonoGameSetup.exe /S /D=$(Build.SourcesDirectory)/tmp/ mkdir -Force "${env:ProgramFiles(x86)}\MSBuild" xcopy .\tmp\$PROGRAMFILES\MSBuild "${env:ProgramFiles(x86)}\MSBuild" /E /Y mkdir -Force "${env:ProgramFiles(x86)}\MonoGame\v3.0\Assemblies\" xcopy .\tmp\Assemblies "${env:ProgramFiles(x86)}\MonoGame\v3.0\Assemblies" /E /Y displayName: 'Install MonoGame $(monoGameVersion) - Windows' condition: eq(variables['vmImage'], 'windows-latest') # 步骤3:MacOS/Linux平台安装MonoGame - script: | curl -L "https://github.com/MonoGame/MonoGame/releases/download/$(monoGameVersion)/MonoGameSDK.$(monoGameVersion).zip" -o ./MonoGameSDK.zip unzip MonoGameSDK.zip -d ./tmp/ sudo mkdir -p /usr/local/lib/monogame/v3.0/Assemblies sudo cp -r ./tmp/Assemblies/* /usr/local/lib/monogame/v3.0/Assemblies/ sudo mkdir -p /usr/local/share/msbuild/MonoGame/v3.0 sudo cp -r ./tmp/MSBuild/MonoGame/v3.0/* /usr/local/share/msbuild/MonoGame/v3.0/ displayName: 'Install MonoGame $(monoGameVersion) - MacOS/Linux' condition: ne(variables['vmImage'], 'windows-latest') # 步骤4:还原NuGet依赖包 - task: NuGetToolInstaller@1 displayName: 'Install NuGet tool' - task: NuGetCommand@2 inputs: restoreSolution: '$(solution)' displayName: 'Restore NuGet packages' # 步骤5:Windows/MacOS用VSBuild构建指定项目 - task: VSBuild@1 inputs: solution: '$(targetProject)' platform: '$(buildPlatform)' configuration: '$(buildConfiguration)' msbuildArgs: '/p:Version=$(GitVersion.SemVer)' # 传递GitVersion生成的版本号 displayName: 'Build $(targetProject) - $(buildConfiguration)' condition: or(eq(variables['vmImage'], 'windows-latest'), eq(variables['vmImage'], 'macos-latest')) # 步骤6:Linux用DotNetCoreCLI构建指定项目 - task: DotNetCoreCLI@2 inputs: command: 'build' projects: '$(targetProject)' arguments: '--configuration $(buildConfiguration) --platform $(buildPlatform) /p:Version=$(GitVersion.SemVer)' displayName: 'Build $(targetProject) - $(buildConfiguration)' condition: eq(variables['vmImage'], 'ubuntu-latest') # 步骤7:发布构建产物到Azure Pipelines - task: PublishBuildArtifacts@1 inputs: PathtoPublish: "$(Build.SourcesDirectory)/$(targetProject)/../bin/$(buildPlatform)/$(buildConfiguration)" ArtifactName: '$(artifactName)' publishLocation: 'Container' displayName: 'Publish build artifacts'
关键细节说明
作业矩阵(Matrix):
- 每个矩阵项对应一个独立的构建作业,包含平台、目标项目、产物名称等专属变量
maxParallel控制并行执行的作业数量,默认Azure Pipelines允许最多10个,可根据你的并发配额调整
MonoGame安装适配:
- Windows平台沿用你原来的解压安装包方式,加了静默安装参数
/S避免交互弹窗 - MacOS/Linux平台下载对应的SDK压缩包,解压后复制到系统级目录,确保MSBuild能找到MonoGame的引用
- Windows平台沿用你原来的解压安装包方式,加了静默安装参数
精准构建指定项目:
- 不再构建整个解决方案,而是通过
targetProject变量指定要构建的具体项目,彻底解决了原来只构建默认启动项目的问题 - 把GitVersion生成的版本号传递给MSBuild,确保生成的程序集带有正确的版本信息
- 不再构建整个解决方案,而是通过
产物发布:
- 每个作业将自己的构建产物发布到独立的Artifact中,名称由
artifactName变量定义,方便你区分不同平台和项目的结果
- 每个作业将自己的构建产物发布到独立的Artifact中,名称由
关于单管道vs多配置文件的选择
用单个管道加矩阵的方式是最优解:
- 所有配置集中在一个文件,便于维护和修改
- 不同平台的作业可以并行执行,节省总构建时间
- 统一的变量和步骤,避免重复配置
如果你后续需要针对某个平台做特殊调整,只需要在矩阵项中添加特定变量或者用condition条件判断即可,灵活性拉满。
内容的提问来源于stack exchange,提问作者Matt Clegg
相关产品推荐
相关产品推荐

