I want to get project version from pom.xml in Github Actions

Viewed 430

My github actions code is as follows. I want to save the version to Environment.

name: Windows x64 Build
on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  build:
    runs-on: windows-latest

    steps:
    - uses: actions/checkout@v2

    - name: Cache local Maven repository
      uses: actions/cache@v2
      with:
        path: ~/.m2/repository
        key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
        restore-keys: |
          ${{ runner.os }}-maven-
    - name: Set up JDK 14
      uses: actions/setup-java@v2
      with:
        java-version: 14
        distribution: 'adopt'
        architecture: x64
        javafx: true
        
    - name: Get version
      run: echo project_version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) >> $GITHUB_ENV

when I run it I get this error and I couldn't find a solution.

Run echo project_version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) >> $GITHUB_ENV
  echo project_version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) >> $GITHUB_ENV
  shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"
  env:
    JAVA_HOME: C:\hostedtoolcache\windows\Java_Adopt_jdk\14.0.2-12\x64
Error: Process completed with exit code 1.

I'll be happy if you can help me.

2 Answers

The code you have there should work correctly if it were executed on bash. The only problem is that for Windows runner the default shell is powershell, which uses slightly different syntax:

- name: Get version
  run: $project_version=mvn help:evaluate -Dexpression=project.version -q -DforceStdout; echo "project_version=$project_version" >> $Env:GITHUB_ENV

This syntax didn't work either, but I found this link and I changed my actions with this:

- name: Get version
  shell: bash -l {0}
  run: echo project_version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) >> $GITHUB_ENV

And wadaaa.

Related