openrewrite / openrewrite/rewrite-github-actions

Recipe - Refactor similar jobs with a matrix

Open
#75 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

recipe
Dominant language
Java
Stars
1
Forks
9
Avg merge
5h 27m
Merged PRs (30d)
12

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the referenced .github/workflows/unit-tests.yml and compare its three Java-version jobs with the matrix example in the issue. Implement the recipe so the workflow runs Java 8, 11, and 17 through one matrix job, then verify that the generated workflow retains the checkout, Maven cache, and unit-test steps.

Written by the indexing model from the issue text.

Assessment

Tech stack
github-actions
Domain
ci-cd, devops
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.