Github action: setup-java with multiple JDKs and corresponding environment variables

Tags: ,



Background: I’ve a spring-boot 2.3 project using reactive driver for cassandra that is built on Java 11. For integration test though, when I spin up an embedded Cassandra database, I rely on presence of Java 8 on the machine with accompanying environment variable JAVA8_HOME.

Question: How can I configure GitHub action setup-java to utilise multiple JDKs for my build and let JAVA_HOME point to Java 11 but JAVA8_HOME point to Java8?

Answer

Using multiple JDKs with GitHub actions is already possible today. One great and neat way is to leverage the strategy.matrix job configuration like that in your .github/workflows/maven.yml:

name: github

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        java-version: [ 8, 11, 15 ]

    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-java@v1
      with:
        java-version: ${{ matrix.java-version }}
    - run: mvn -B install --no-transfer-progress --file pom.xml

I didn’t check if this does include the environment variable configuration for JAVA_HOME you need – but it clearly isolates the build environments for the separate Java versions. Here’s a full example project using this setup: https://github.com/codecentric/cxf-spring-boot-starter and here’s a green build log.

Also the GitHub actions GUI for matrix builds is quite nice: github actions matrix build



Source: stackoverflow