skip to Main Content

I want to extract the version of my pubspec.yaml file of my flutter app using github actions and later reuse this version and attach it to a filename.

Here’s my main.yaml step:

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:amy_appmy_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

I want to be able to use the APP_VERSION later but it seems I’m doing something wrong because it’s never setting the variable correctly and I can’t echo it and therefore I can’t reference it anywhere.

Any help is really appreciated!

2

Answers


  1. Chosen as BEST ANSWER

    I got it. I had to switch from cmd to powershell to make it work. The main.yamllooks as follows:

    build_on_push:
        if: github.event_name == 'push'
        runs-on: windows-latest
        steps:
          - name: Get version from pubspec.yaml
            run: |
              $version = (Get-Content pubspec.yaml | Select-String 'version:[^^]*' | ForEach-Object { $_.ToString().Split(":")[1].Trim() })
              echo "APP_VERSION=$version" | Out-File -FilePath $env:GITHUB_ENV -Append
          - name: Use the value
            run: |
              echo "${{ env.APP_VERSION }}"
    

  2. The variables in cmd should be surrounded by % e.g. %VAR%.

    To set a variable VERSION to GITHUB_ENV, you can use:

    echo VERSION=%VERSION% >> %GITHUB_ENV%
    

    and, in the subsequent steps, it can be accessed like this:

    echo VERSION=%VERSION%
    

    or, with env context:

    echo VERSION=${{ env.VERSION }}
    

    For processing YAML, you can use yq command line utility. Contrary to Linux and Mac runners, yq is not preinstalled on the Windows runners so you’ll have to install it first with choco install yq.

    With yq, this would extract the version from the pubspec.yaml file:

    yq -r .version pubspec.yaml
    

    The following will extract and set it to env var using an intermediate file:

    yq -r .version pubspec.yaml > version.file
    set /p VERSION=<version.file
    echo VERSION=%VERSION% >> %GITHUB_ENV%
    

    As you have moved on to Powershell, you can still install and use yq:

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

    If you consider using Linux runners in the future, you might as well use yq GitHub Action:

    name: CI
    
    on: push
    
    jobs:
      build_on_push:
        if: github.event_name == 'push'
        runs-on: ubuntu-latest
        steps:
        - uses: actions/checkout@v3
    
        - name: Get version
          id: yq
          uses: mikefarah/yq@master
          with:
            cmd: yq -r '.version' 'pubspec.yaml'
    
        - name: Print version
          run: echo ${{ steps.yq.outputs.result }}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search