63 lines
3.2 KiB
Java
63 lines
3.2 KiB
Java
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 <command>. */
|
|
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());
|
|
Path root = Path.of("").toAbsolutePath();
|
|
var env = Recipe.environment(root, System.getenv());
|
|
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]), Setup.required(env, "INSECURE_REGISTRY"));
|
|
}
|
|
case "install-runner" ->
|
|
Setup.installRunner(root, env, commands, args.length == 2 && args[1].equals("--prepare-only"));
|
|
case "test" -> CiTests.run();
|
|
case "deploy", "rollback" -> Release.execute(root, env, args[0]);
|
|
case "release-test" -> ReleaseTests.run();
|
|
case "configure-deployment" -> ReleaseSetup.configure(root, env);
|
|
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"
|
|
+ " <path>|install-runner [--prepare-only]|deploy|rollback|release-test|configure-deployment";
|
|
}
|
|
}
|