diff --git a/.github/workflows/manual.yml b/.github/workflows/manual.yml index 7309137..809256e 100644 --- a/.github/workflows/manual.yml +++ b/.github/workflows/manual.yml @@ -13,7 +13,7 @@ jobs: os: [ ubuntu-latest, macos-latest, windows-latest ] runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - id: hello uses: ./ with: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 05e5fd3..953cac0 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,7 +7,7 @@ jobs: os: [ ubuntu-latest, macos-latest, windows-latest ] runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - id: hello uses: ./ with: diff --git a/Action.java b/Action.java index ebb28f8..6e3e755 100644 --- a/Action.java +++ b/Action.java @@ -1,10 +1,40 @@ import java.util.Random; class Action { + /** Entry-point for this Java action. */ public static void main(String... args) { System.out.printf("Hello %s.%n", args.length == 0 ? "Java" : args[0]); var gaussian = new Random().nextGaussian(); - System.out.println("::set-output name=random-number::" + gaussian); + GitHub.setOutput("random-number", gaussian); System.out.println("Goodbye and have fun with: " + gaussian); } + + /** GitHub Actions helper. */ + static class GitHub { + /** Sets an action's output parameter. */ + static void setOutput(String name, Object value) { + if (name.isBlank() || value.toString().isBlank()) { // implicit null checks included + throw new IllegalArgumentException("name or value are blank: " + name + "=" + value); + } + var githubOutput = System.getenv("GITHUB_OUTPUT"); + if (githubOutput == null) { + throw new AssertionError("No such environment variable: GITHUB_OUTPUT"); + } + try { + var file = java.nio.file.Path.of(githubOutput); + if (file.getParent() != null) java.nio.file.Files.createDirectories(file.getParent()); + var lines = (name + "=" + value).lines().toList(); + if (lines.size() != 1) { + throw new UnsupportedOperationException("Multiline strings are no supported"); + } + java.nio.file.Files.write(file, lines, + java.nio.charset.StandardCharsets.UTF_8, + java.nio.file.StandardOpenOption.CREATE, + java.nio.file.StandardOpenOption.APPEND, + java.nio.file.StandardOpenOption.WRITE); + } catch (Exception exception) { + throw new RuntimeException(exception); + } + } + } } diff --git a/README.md b/README.md index 4bee5a5..2e02b99 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ jobs: os: [ ubuntu-latest, macos-latest, windows-latest ] runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - id: hello uses: sormuras/hello-world-java-action@v1 with: @@ -49,12 +49,20 @@ class Action { public static void main(String... args) { System.out.printf("Hello %s.%n", args.length == 0 ? "Java" : args[0]); var gaussian = new Random().nextGaussian(); - System.out.println("::set-output name=random-number::" + gaussian); + GitHub.setOutput("random-number", gaussian); System.out.println("Goodbye and have fun with: " + gaussian); } + + /** GitHub Actions helper. */ + static class GitHub { + /** Sets an action's output parameter. */ + static void setOutput(String name, Object value) { + //... + } + } } ``` ## Feedback and Discussion -https://github.community/t/use-java-11-as-github-action-scripting-language/136755 +- [Synchronize default Java version on latest runners](https://github.com/actions/runner-images/discussions/10509) diff --git a/action.yml b/action.yml index 8f135c8..c173dbb 100644 --- a/action.yml +++ b/action.yml @@ -23,5 +23,5 @@ runs: id: java shell: bash run: | - echo "::set-output name=random-number::SOME-INITIAL-VALUE-TO-MAKE-THE-ACTION-YAML-PARSER-HAPPY" - "${JAVA_HOME_11_X64}/bin/java" "${GITHUB_ACTION_PATH}/Action.java" "${{ inputs.who-to-greet }}" + JAVA="${JAVA_HOME_21_X64:-$JAVA_HOME_21_arm64}/bin/java" + $JAVA "${GITHUB_ACTION_PATH}/Action.java" "${{ inputs.who-to-greet }}"