openrewrite / rewrite-github-actions

OpenRewrite recipes for performing GitHub action hygiene and migration tasks.
Apache License 2.0
9 stars 10 forks source link

Recipe - Refactor similar jobs with a matrix #75

Open aalmiray opened 1 year ago

aalmiray commented 1 year ago

Given the following workflow definition https://raw.githubusercontent.com/asyncer-io/r2dbc-mysql/f4ca1652219074c337d7db12d2572686da2c3a64/.github/workflows/unit-tests.yml

name: Unit tests

on: [ pull_request ]

jobs:
  build_java_8:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - name: Set up Temurin 8
        uses: actions/setup-java@v3
        with:
          distribution: temurin
          java-version: 8
          cache: maven
      - name: Unit test with Maven
        run: ./mvnw -B test -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN
  build_java_11:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - name: Set up Temurin 11
        uses: actions/setup-java@v3
        with:
          distribution: temurin
          java-version: 11
          cache: maven
      - name: Unit test with Maven
        run: ./mvnw -B test -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN
  build_java_17:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - name: Set up Temurin 17
        uses: actions/setup-java@v3
        with:
          distribution: temurin
          java-version: 17
          cache: maven
      - name: Unit test with Maven
        run: ./mvnw -B test -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN

One could rewrite this workflow using a matrix configuration like so

name: Unit tests

on: [ pull_request ]

jobs:
  build:
    name: "Build ${{ matrix.java }}"
    strategy:
      fail-fast: false
      matrix:
        java: [8, 11, 17]
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - name: "Set up Temurin ${{ matrix.java }}"
        uses: actions/setup-java@v3
        with:
          distribution: temurin
          java-version: ${{ matrix.java }}
          cache: maven
      - name: Unit test with Maven
        run: ./mvnw -B test -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN