hello-world-java-action

A template to demonstrate how to build a Java action.

MIT License

Stars
12

Hello World Java Action

A template to demonstrate how to build a Java action via JEP 330: Launch Single-File Source-Code Programs.

This action prints "Hello World" to the log or "Hello" + the name of a person to greet.

To learn how this action was built, see "Creating a composite run steps action" in the GitHub Help documentation.

Inputs

who-to-greet

Required The name of the person to greet. Default "World".

Outputs

random-number

Returns the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence.

See java.util.Random#nextGaussian() for details.

Example usage

jobs:
  build:
    strategy:
      matrix:
        os: [ ubuntu-latest, macos-latest, windows-latest ]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - id: hello
        uses: sormuras/hello-world-java-action@v1
        with:
          who-to-greet: 'Mona the Octocat'
      - run: echo random-number ${{ steps.hello.outputs.random-number }}
        shell: bash

Action.java

import java.util.Random;

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();
    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