import java.nio.file.*; import java.time.*; import java.time.format.DateTimeFormatter; import java.util.*; /** Run from the repository root with JDK 25: java ci/Ci.java . */ public class Ci { public static void main(String[] args) { try { if (Runtime.version().feature() < 25) throw new IllegalArgumentException("JDK 25 or newer is required"); if (args.length == 0) throw new IllegalArgumentException(usage()); var env = new HashMap<>(System.getenv()); Path root = Path.of("").toAbsolutePath(); var commands = new Commands(Duration.ofMinutes(args[0].equals("publish") ? 15 : 75)); switch (args[0]) { case "verify", "publish", "cleanup" -> { var pipeline = new Pipeline(root, env, commands); switch (args[0]) { case "verify" -> pipeline.verify(); case "publish" -> pipeline.publish(); default -> pipeline.cleanup(); } } case "attempt" -> { Path file = Path.of(Setup.required(env, "GITHUB_ENV")); String id = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSSSSSSSS") .withZone(ZoneOffset.UTC).format(Instant.now()); Files.writeString(file, "CI_RUN_ATTEMPT=" + id + "\n", StandardOpenOption.CREATE, StandardOpenOption.APPEND); } case "configure-gitea" -> Setup.configureGitea(env); case "configure-docker" -> { if (args.length != 2) throw new IllegalArgumentException("Supply the daemon.json path"); Setup.configureDocker(Path.of(args[1])); } case "install-runner" -> Setup.installRunner(root, env, commands, args.length == 2 && args[1].equals("--prepare-only")); case "test" -> CiTests.run(); default -> throw new IllegalArgumentException(usage()); } } catch (Commands.Failure e) { System.err.println(e.getMessage()); System.exit(e.exitCode); } catch (Exception e) { // Never dump HTTP bodies, process arguments, environment or tokens. System.err.println(e instanceof IllegalArgumentException || e instanceof Setup.ApiFailure ? e.getMessage() : "CI failed (" + e.getClass().getSimpleName() + "). Check diagnostics and configuration."); System.exit(1); } } static String usage() { return "Usage: java ci/Ci.java verify|publish|cleanup|attempt|test|configure-gitea|configure-docker |install-runner [--prepare-only]"; } }