Windows环境GitHub Actions提取Flutter项目pubspec.yaml版本失败求助
解决GitHub Actions中提取Flutter pubspec.yaml版本的问题
问题根源
你的脚本存在两个核心问题:
- Windows cmd环境下,GitHub环境变量文件的引用需使用
%GITHUB_ENV%而非$GITHUB_ENV(后者是bash语法) - cmd中使用
for循环处理变量时,未启用延迟变量扩展,导致!APP_VERSION!无法正确解析
修正后的完整步骤代码
build_on_push: if: github.event_name == 'push' runs-on: windows-latest steps: - uses: actions/checkout@v3 - name: Extract version from pubspec.yaml run: | @echo off setlocal enabledelayedexpansion for /f "tokens=2 delims=: " %%a in ('findstr /r /c:"version:[^^]*" pubspec.yaml') do set APP_VERSION=%%a echo APP_VERSION=!APP_VERSION!>>%GITHUB_ENV% shell: cmd - name: Print version (cmd variable) run: echo %APP_VERSION% shell: cmd - name: Print version (GitHub env context) run: echo ${{ env.APP_VERSION }} shell: cmd
关键修改说明
- 启用延迟扩展:添加
setlocal enabledelayedexpansion,确保cmd能正确处理循环内的变量赋值,避免变量值为空 - 修正环境变量文件路径:将
$GITHUB_ENV替换为%GITHUB_ENV%,符合Windows cmd的环境变量引用规则 - 简化解析逻辑:移除冗余的管道命令,直接用
for循环解析findstr输出,提升执行效率
后续使用示例(附加版本到文件名)
若需将提取的版本附加到构建产物文件名,可参考以下步骤:
- name: Build Flutter release APK run: flutter build apk --release shell: cmd - name: Rename APK with version run: ren build\app\outputs\apk\release\app-release.apk app-release-${{ env.APP_VERSION }}.apk shell: cmd
内容的提问来源于stack exchange,提问作者novas1r1
相关产品推荐
相关产品推荐

