How to construct run command line arguments conditionally?

I have a simple matrix build:

jobs:
  build:
    name: ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    steps:
    - name: Checkout master branch
      uses: actions/checkout@v2
    - name: Unix build
      env:
        PULL_REQUEST: ${{ github.head_ref }}
        REPO_SLUG: ${{ github.repository }}
        REF: ${{ github.ref }}
        COVERALLS_TOKEN: ${{ secrets.COVERALLS_TOKEN }}
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      if: runner.os == 'Linux' || runner.os == 'macOS'
      run: ./dev/main
    - name: Windows build
      run: mvn -B -V verify
      if: runner.os == 'Windows'

The dev/main is a bash script that evaluates the passed environment variables and based on their content constructs a mvn command line.

For instance, if $REPO_SLUG has certain value and $PULL_REQUEST is empty, it will append -P sonar -Dsonar.login=$SONAR_TOKEN sonar:sonar to the arguments and eventually run mvn with these accumulated arguments.

You can see that the Windows build lacks this logic - it runs Maven directly. I’d like all the builds use the same logic however I don’t want to recode the bash script in PowerShell. Is there a way how to make it work solely in the workflow definition ?

Bash is available on Windows too:

    - name: Build
      shell: bash
      env:
        PULL_REQUEST: ${{ github.head_ref }}
        REPO_SLUG: ${{ github.repository }}
        REF: ${{ github.ref }}
        COVERALLS_TOKEN: ${{ secrets.COVERALLS_TOKEN }}
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: ./dev/main

Documentation: Using a specific shell

1 Like

works nicely, thanks !