From 110afbe1a78d49f29f8c185e62442a8218442c6a Mon Sep 17 00:00:00 2001 From: Kyle Aure Date: Wed, 19 Jul 2023 15:03:35 -0500 Subject: [PATCH] Create CI build --- .github/workflows/CI.yml | 50 +++++++++++++++ .../scripts/ClassVersionChecker.java | 63 +++++++++++++++++++ .github/workflows/scripts/verifyArtifact.sh | 18 ++++++ gradlew | 0 4 files changed, 131 insertions(+) create mode 100644 .github/workflows/CI.yml create mode 100755 .github/workflows/scripts/ClassVersionChecker.java create mode 100755 .github/workflows/scripts/verifyArtifact.sh mode change 100644 => 100755 gradlew diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml new file mode 100644 index 0000000..7d22dc1 --- /dev/null +++ b/.github/workflows/CI.yml @@ -0,0 +1,50 @@ +name: Java CI with Gradle + +on: + push: + branches: [ "main" ] + +permissions: + contents: read + +jobs: + build: + strategy: + matrix: + java-version: [ '17', '18', '19', '20', '21' ] + + runs-on: ubuntu-latest + + steps: + - name: Checkout project + uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 + + # Gradle java toolchain cannot find early release images, need to preload until GA is available + - name: Preload JDK 21 + if: ${{ matrix.java-version == '21' }} + uses: actions/setup-java@5ffc13f4174014e2d4d4572b3d74c3fa61aeb2c2 # v3.11.0 + with: + java-version: '21-ea' + distribution: 'temurin' + + - name: Set up JDK 11 + uses: actions/setup-java@5ffc13f4174014e2d4d4572b3d74c3fa61aeb2c2 # v3.11.0 + with: + java-version: '11' + distribution: 'temurin' + cache: gradle + + - name: Build application + run: | + ./gradlew io.openliberty.java.internal_fat_${{ matrix.java-version }}:build + + - name: Verify application + run: | + ./.github/workflows/scripts/verifyArtifact.sh ${{ matrix.java-version }} + + - name: Upload application + uses: actions/upload-artifact@013d2b89baa2f354c5ffec54c68bec4ab39a2534 #v3.1.2 + with: + name: Applications + path: io.openliberty.java.internal_fat_${{ matrix.java-version }}/build/libs/io.openliberty.java.internal_fat_${{ matrix.java-version }}.war + if-no-files-found: error diff --git a/.github/workflows/scripts/ClassVersionChecker.java b/.github/workflows/scripts/ClassVersionChecker.java new file mode 100755 index 0000000..52ba278 --- /dev/null +++ b/.github/workflows/scripts/ClassVersionChecker.java @@ -0,0 +1,63 @@ +import java.io.*; +import java.util.*; + +public class ClassVersionChecker { + + private static final HashMap majorCodeMap = new HashMap<>(); + + public static void main(String[] args) throws IOException { + if (args.length != 2) { + throw new RuntimeException("Expected exactly 2 arguments"); + } + + //Adapted from https://docs.oracle.com/javase/specs/jvms/se20/html/jvms-4.html#jvms-4.1 + majorCodeMap.put("1.0.2", 45); + majorCodeMap.put("1.1", 45); + majorCodeMap.put("1.2", 46); + majorCodeMap.put("1.3", 47); + majorCodeMap.put("1.4", 48); + majorCodeMap.put("5.0", 49); + majorCodeMap.put("6", 50); + majorCodeMap.put("7", 51); + majorCodeMap.put("8", 52); + majorCodeMap.put("9", 53); + majorCodeMap.put("10", 54); + majorCodeMap.put("11", 55); + majorCodeMap.put("12", 56); + majorCodeMap.put("13", 57); + majorCodeMap.put("14", 58); + majorCodeMap.put("15", 59); + majorCodeMap.put("16", 60); + majorCodeMap.put("17", 61); + majorCodeMap.put("18", 62); + majorCodeMap.put("19", 63); + majorCodeMap.put("20", 64); + majorCodeMap.put("21", 65); + + String filename = args[0]; + int expected = majorCodeMap.get(args[1]); + + checkClassVersion(filename, expected); + } + + private static void checkClassVersion(String filename, int expected) throws IOException { + try ( DataInputStream in = new DataInputStream(new FileInputStream(filename)) ) { + int magic = in.readInt(); + + if(magic != 0xcafebabe) { + System.out.println(filename + " is not a valid class!"); + } + + int minor = in.readUnsignedShort(); + int major = in.readUnsignedShort(); + + System.out.println("Major version: " + major); + System.out.println("Minor version: " + minor); + + if(expected != major) { + System.out.println("Expected major version of " + expected + " but got " + major); + System.exit(1); //Failing execution + } + } + } +} \ No newline at end of file diff --git a/.github/workflows/scripts/verifyArtifact.sh b/.github/workflows/scripts/verifyArtifact.sh new file mode 100755 index 0000000..bdc8679 --- /dev/null +++ b/.github/workflows/scripts/verifyArtifact.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +if [ $# -ne 1 ]; then + echo "Exactly one argument is required" + exit 0 +fi + +JAVA_VERSION=$1 +SUB_PROJECT=$PWD/io.openliberty.java.internal_fat_$JAVA_VERSION +TEST_CLASS=$SUB_PROJECT/build/classes/java/main/io/openliberty/java/internal/TestApp.class + + +echo "Checking class" +SCRIPTS=$PWD/.github/workflows/scripts/ +pushd $SCRIPTS &> /dev/null + java ClassVersionChecker.java $TEST_CLASS $JAVA_VERSION +popd &> /dev/null +echo "--------------------" diff --git a/gradlew b/gradlew old mode 100644 new mode 100755