如何从 flutter 应用程序中的 pubspec.yaml 中提取应用程序版本,以便在 Windows 上运行的 github 操作中使用它?

err*_*337 1 windows cmd flutter github-actions

我想pubspec.yaml使用 github actions 提取我的 flutter 应用程序的文件版本,然后重用此版本并将其附加到文件名。

这是我的main.yaml步骤:

build_on_push:
    if: github.event_name == 'push'
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v3
      - name: Get version from pubspec.yaml
        # this step echos "D:\a\my_app\my_app>set APP_VERSION=1.0.0+1" 
        run: |
          type pubspec.yaml | findstr /r "version:[^^]*" | 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
      # doesnt work
      - name: Display the version retrieved from pubspec
        run: echo ${{ env.APP_VERSION }}
        shell: cmd
      # doesnt work
      - name: Display the version retrieved from pubspec 3
        run: echo %APP_VERSION%
        shell: cmd
Run Code Online (Sandbox Code Playgroud)

我希望能够使用APP_VERSION后者,但似乎我做错了什么,因为它永远不会正确设置变量,而且我无法回显它,因此我无法在任何地方引用它。

非常感谢任何帮助!

Aze*_*eem 5

中的变量应被例如cmd包围。%%VAR%

要将变量设置VERSIONGITHUB_ENV,您可以使用:

echo VERSION=%VERSION% >> %GITHUB_ENV%
Run Code Online (Sandbox Code Playgroud)

并且,在后续步骤中,可以像这样访问它:

echo VERSION=%VERSION%
Run Code Online (Sandbox Code Playgroud)

或者,结合env上下文:

echo VERSION=${{ env.VERSION }}
Run Code Online (Sandbox Code Playgroud)

为了处理 YAML,您可以使用yq命令行实用程序。与 Linux 和 Mac 运行器相反, Windows 运行器上yq没有预安装它,因此您必须先使用choco install yq.

使用,这将从文件中yq提取:versionpubspec.yaml

yq -r .version pubspec.yaml
Run Code Online (Sandbox Code Playgroud)

以下内容将使用中间文件提取并将其设置为 env var:

yq -r .version pubspec.yaml > version.file
set /p VERSION=<version.file
echo VERSION=%VERSION% >> %GITHUB_ENV%
Run Code Online (Sandbox Code Playgroud)

当您已转向Powershell时,您仍然可以安装和使用yq

$VERSION=(yq -r .version pubspec.yaml)
echo VERSION=$VERSION | Out-File -FilePath $ENV:GITHUB_ENV -Encoding utf8 -Append
Run Code Online (Sandbox Code Playgroud)

如果你以后考虑使用 Linux 运行器,不妨使用yqGitHub Action

echo VERSION=%VERSION% >> %GITHUB_ENV%
Run Code Online (Sandbox Code Playgroud)