Created
December 24, 2022 04:49
-
-
Save alexanderankin/69db1ce0345a9cc69e8c2a4e08c2af64 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.github.dockerjava.api.DockerClient; | |
| import com.github.dockerjava.api.async.ResultCallback; | |
| import com.github.dockerjava.api.model.WaitResponse; | |
| import lombok.SneakyThrows; | |
| import org.eclipse.jgit.api.ArchiveCommand; | |
| import org.eclipse.jgit.api.Git; | |
| import org.eclipse.jgit.archive.TarFormat; | |
| import org.eclipse.jgit.errors.RepositoryNotFoundException; | |
| import org.testcontainers.containers.GenericContainer; | |
| import org.testcontainers.containers.startupcheck.StartupCheckStrategy; | |
| import org.testcontainers.utility.DockerImageName; | |
| import org.testcontainers.utility.MountableFile; | |
| import java.awt.desktop.SystemSleepEvent; | |
| import java.io.File; | |
| import java.io.FileOutputStream; | |
| import java.io.IOException; | |
| import java.io.OutputStream; | |
| import java.nio.file.Files; | |
| import java.util.concurrent.atomic.AtomicReference; | |
| public class GradleContainerDemo { | |
| public static class GradleContainer extends GenericContainer<GradleContainer> { | |
| public GradleContainer(DockerImageName imageName) { | |
| super(imageName); | |
| //noinspection resource | |
| withStartupCheckStrategy(new StartupCheckStrategy() { | |
| @Override | |
| public StartupStatus checkStartupState(DockerClient dockerClient, String containerId) { | |
| return StartupStatus.SUCCESSFUL; | |
| } | |
| }); | |
| } | |
| @Override | |
| public void start() { | |
| super.start(); | |
| followOutput(s -> logger().info(s.getUtf8String())); | |
| waitForContainerExit(); | |
| } | |
| @SneakyThrows | |
| public void waitForContainerExit() { | |
| dockerClient.waitContainerCmd(getContainerId()).exec(new ResultCallback.Adapter<>() { | |
| @Override | |
| public void onNext(WaitResponse object) { | |
| logger().info("waitResponse: {}", object); | |
| } | |
| }).awaitCompletion(); | |
| } | |
| @Override | |
| public StartupCheckStrategy getStartupCheckStrategy() { | |
| return new StartupCheckStrategy() { | |
| @Override | |
| public StartupStatus checkStartupState(DockerClient dockerClient, String containerId) { | |
| return StartupStatus.SUCCESSFUL; | |
| } | |
| }; | |
| } | |
| } | |
| @SneakyThrows | |
| public static void main(String[] args) { | |
| ArchiveCommand.registerFormat("tar", new TarFormat()); | |
| FileOutputStream fos; | |
| String filename; | |
| var dir = new File(System.getProperty("user.dir")); // use rootProject.projectDir or whatever in gradle | |
| try (Git git = Git.open(dir)) { | |
| var branch = git.getRepository().getBranch(); | |
| filename = "archive-" + branch + "-" + System.currentTimeMillis() + ".tar"; | |
| OutputStream tar = git.archive() | |
| .setFormat("tar") | |
| .setTree(git.getRepository().resolve(branch)) | |
| .setFilename(filename) | |
| .setOutputStream(fos = new FileOutputStream(filename)) | |
| .call(); | |
| tar.close(); | |
| fos.flush(); | |
| fos.close(); | |
| Runtime.getRuntime().addShutdownHook(new Thread(() -> { | |
| try { | |
| Files.delete(new File(filename).toPath()); | |
| } catch (IOException ignored) { | |
| } | |
| })); | |
| } catch (RepositoryNotFoundException rnfe) { | |
| throw new RuntimeException("could not find repo", rnfe); | |
| } | |
| try (GradleContainer g = new GradleContainer( | |
| DockerImageName.parse("gradle") | |
| .withTag("jdk17-alpine"))) { | |
| g | |
| .withCopyFileToContainer(MountableFile.forHostPath(filename), "/repo.tar") | |
| // todo abstract the ugliness (meanwhile avert your eyes) | |
| // is busy box acts up again, add "apk add tar" | |
| .withCommand("sh", "-c", """ | |
| mkdir /repo && \ | |
| tar xf /repo.tar -C /repo && \ | |
| cd /repo && \ | |
| gradle tasks | |
| """); | |
| g.start(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment