优化GitHub CI Action中Windows环境的VS2022依赖以缩短构建时间
问题
配置GitHub CI Action时,用VS2019在Windows环境编译项目出现std::format_strings未定义错误(本地VS2022构建正常)。改用Action安装VS2022后项目可编译,但构建耗时约10分钟,其中8分钟用于VS安装,希望优化Windows环境的构建性能,接近Linux的2分钟耗时。
当前使用的CI配置:
name: Windows on: push: branches: [ master ] pull_request: branches: [ master ] jobs: build: runs-on: windows-latest steps: - uses: actions/checkout@v2 with: fetch-depth: 0 - name: Install Visual Studio Build Tools run: | choco install visualstudio2022buildtools --package-parameters "--add Microsoft.VisualStudio.Component.VC.Tools.x86.x64 --add Microsoft.VisualStudio.Component.Windows10SDK --add Microsoft.VisualStudio.Component.TestTools.BuildTools --includeOptional" -y choco install visualstudio2022-workload-vctools -y - name: Add MSBuild to PATH run: | echo "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\MSBuild\Current\Bin" | Out-File -Append -FilePath $env:GITHUB_PATH - name: Print MSBuild Version run: | msbuild -version - name: Run Premake for Visual Studio run: .\setup.bat - name: Compile Project with MSBuild run: | msbuild sigma.sln /p:Configuration=Debug /p:Platform="x64" /p:stdcpp=latest
优化方案
- 直接使用预装VS2022的Runner:GitHub官方
windows-latestrunner已预装VS2022 Build Tools、VC工具链、Windows SDK等核心组件,无需手动通过Choco安装,直接跳过8分钟的安装步骤。 - 启用并行编译:在MSBuild命令中添加
/m参数,利用Runner多核CPU并行处理编译任务,大幅压缩编译耗时。 - 缓存构建产物:用
actions/cache缓存Premake生成的工程文件、中间构建产物和依赖,避免每次构建重复生成或下载。 - 精简安装(如需自定义):若必须手动安装组件,去掉
--includeOptional参数,仅保留项目必需的组件,减少冗余安装时间。
修改后的CI配置
name: Windows on: push: branches: [ master ] pull_request: branches: [ master ] jobs: build: runs-on: windows-latest steps: - uses: actions/checkout@v4 with: fetch-depth: 0 # 缓存Premake生成文件与构建产物 - name: Cache build artifacts uses: actions/cache@v3 with: path: | sigma.sln ./*.vcxproj ./*.vcxproj.filters ./bin ./obj key: ${{ runner.os }}-build-${{ hashFiles('setup.bat', 'premake5.lua') }} restore-keys: | ${{ runner.os }}-build- - name: Run Premake for Visual Studio run: .\setup.bat - name: Compile Project with MSBuild (Parallel) run: | msbuild sigma.sln /m /p:Configuration=Debug /p:Platform="x64" /p:stdcpp=latest
内容的提问来源于stack exchange,提问作者Goubermouche
相关产品推荐
相关产品推荐

