How to switch conditionally a task in a step?

I have created a workflow that contains this 2 jobs:

build-and-test-with-Java-8-and-later:
    name: Build -> Test (JVM ${{ matrix.java }}, ${{ matrix.architecture }}, ${{ matrix.os }})
    strategy:
      fail-fast: true
      matrix:
        os: [ubuntu-latest, macOS-latest, windows-latest]
        java: [8, 9, 10, 11, 12, 13, 14]
        architecture: [x32, x64]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK ${{ matrix.java }}
        uses: actions/setup-java@v1
        with:
          java-version: ${{ matrix.java }}
          architecture: ${{ matrix.architecture }}
      - name: Build and test
        run: mvn -B clean test -Dproject_jdk_version=${{ matrix.java }} -DskipTests=false --file pom.xml

  build-and-test-with-Java-15-ea:
    name: Build -> Test (JVM ${{ matrix.java }}, ${{ matrix.architecture }}, ${{ matrix.os }})
    strategy:
      fail-fast: true
      matrix:
        os: [ubuntu-latest, macOS-latest, windows-latest]
        java: [15-ea]
        architecture: [x32, x64]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK ${{ matrix.java }}
        uses: actions/setup-java@v1
        with:
          java-version: ${{ matrix.java }}
          architecture: ${{ matrix.architecture }}
      - name: Build and test
        run: mvn -B clean test -Dproject_jdk_version=15 -DskipTests=false --file pom.xml

… And I would like to merge this 2 jobs in a unique job that switch the run element when iterated ${{ matrix.java }} variable contains the ‘-ea’ suffix. in particular I would like to create something like this:

build-and-test-with-Java-8-and-later:
    name: Build -> Test (JVM ${{ matrix.java }}, ${{ matrix.architecture }}, ${{ matrix.os }})
    strategy:
      fail-fast: true
      max-parallel: 10
      matrix:
        os: [ubuntu-latest, macOS-latest, windows-latest]
        java: [8, 9, 10, 11, 12, 13, 14, 15-ea]
        architecture: [x32, x64]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK ${{ matrix.java }}
        uses: actions/setup-java@v1
        with:
          java-version: if${{ matrix.java }}
          architecture: ${{ matrix.architecture }}
      - name: Build and test
        if: ${{ matrix.java }} != '15-ea'
        run: mvn -B clean test -Dproject_jdk_version=${{ matrix.java }} -DskipTests=false --file pom.xml
      - name: Build and test
        if: ${{ matrix.java }} == '15-ea'
        run: mvn -B clean test -Dproject_jdk_version=15 -DskipTests=false --file pom.xml

Hi @burningwave ,

In if expression, please directly use matrix.java without ‘${{}}’. Code sample as below:

build-and-test-with-Java-8-and-later:
    name: Build -> Test (JVM ${{ matrix.java }}, ${{ matrix.architecture }}, ${{ matrix.os }})
    strategy:
      fail-fast: false
      max-parallel: 10
      matrix:
        os: [ubuntu-latest, macOS-latest, windows-latest]
        java: [8, 9, 10, 11, 12, 13, 14, 15-ea]
        architecture: [x32, x64]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK ${{ matrix.java }}
        uses: actions/setup-java@v1
        with:
          java-version: ${{ matrix.java }}
          architecture: ${{ matrix.architecture }}
      - name: Build and test
        if:<font color="#FF0000">matrix.java != '15-ea'</font> run: echo ${{ matrix.java }}
      - name: Build and test
        if:<font color="#FF0000">matrix.java == '15-ea'</font> run: echo ${{ matrix.java }}

Thanks.

Thans a lot: I solved this is the final job:

build-and-test-with-Java-8-and-later:
    name: Build -> Test (JVM ${{ matrix.java }}, ${{ matrix.architecture }}, ${{ matrix.os }})
    strategy:
      fail-fast: true
      max-parallel: 10
      matrix:
        os: [ubuntu-latest, macOS-latest, windows-latest]
        java: [8, 9, 10, 11, 12, 13, 14, 15-ea]
        architecture: [x32, x64]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK ${{ matrix.java }}
        uses: actions/setup-java@v1
        with:
          java-version: ${{ matrix.java }}
          architecture: ${{ matrix.architecture }}
      - if: endsWith(matrix.java, '-ea') != true
        name: Build and test for final JDK version
        run: mvn -B clean test -Dproject_jdk_version=${{ matrix.java }} -DskipTests=false --file pom.xml
      - if: endsWith(matrix.java, '-ea') == true
        name: Build and test for Early Access JDK version
        run: mvn -B clean test -Dproject_jdk_version=15 -DskipTests=false --file pom.xml

… and do you know how to replace a String for writing something like this in the last row?:

run: mvn -B clean test -Dproject_jdk_version=${{matrix.java.replace('-ea', '')}} -DskipTests=false --file pom.xml