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
I got it. I had to switch from cmd to powershell to make it work. The
main.yaml
looks as follows:The variables in
cmd
should be surrounded by%
e.g.%VAR%
.To set a variable
VERSION
toGITHUB_ENV
, you can use:and, in the subsequent steps, it can be accessed like this:
or, with
env
context: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 withchoco install yq
.With
yq
, this would extract theversion
from thepubspec.yaml
file:The following will extract and set it to env var using an intermediate file:
As you have moved on to Powershell, you can still install and use
yq
:If you consider using Linux runners in the future, you might as well use
yq
GitHub Action: