mirror of
https://github.com/corda/corda.git
synced 2024-12-18 20:47:57 +00:00
TM-67 converting ImageBuilding from groovy to java (#5609)
* TM-67 converting ImageBuilding from groovy to java * TM-67 wrong way to set properties * TM-67 do first does not seem to work * TM-67 setting credentials first * TM-67 small logic fix * TM-67 adding stacktrace * TM-67 addressing PR comments * TM-67 fixed normal builds that don't require docker * TM-67 adding guard rails to the code * TM-67 removing stacktrace * TM-67 addressing pr comments
This commit is contained in:
parent
49e904b3a2
commit
c5c80033d3
@ -1,123 +0,0 @@
|
||||
package net.corda.testing
|
||||
|
||||
import com.bmuschko.gradle.docker.DockerRegistryCredentials
|
||||
import com.bmuschko.gradle.docker.tasks.container.*
|
||||
import com.bmuschko.gradle.docker.tasks.image.*
|
||||
import org.gradle.api.GradleException
|
||||
import org.gradle.api.Plugin
|
||||
import org.gradle.api.Project
|
||||
|
||||
/**
|
||||
this plugin is responsible for setting up all the required docker image building tasks required for producing and pushing an
|
||||
image of the current build output to a remote container registry
|
||||
*/
|
||||
class ImageBuilding implements Plugin<Project> {
|
||||
|
||||
public static final String registryName = "stefanotestingcr.azurecr.io/testing"
|
||||
DockerPushImage pushTask
|
||||
|
||||
@Override
|
||||
void apply(Project project) {
|
||||
|
||||
def registryCredentialsForPush = new DockerRegistryCredentials(project.getObjects())
|
||||
registryCredentialsForPush.username.set("stefanotestingcr")
|
||||
registryCredentialsForPush.password.set(System.getProperty("docker.push.password") ? System.getProperty("docker.push.password") : "")
|
||||
|
||||
DockerPullImage pullTask = project.tasks.create("pullBaseImage", DockerPullImage) {
|
||||
repository = "stefanotestingcr.azurecr.io/buildbase"
|
||||
tag = "latest"
|
||||
doFirst {
|
||||
registryCredentials = registryCredentialsForPush
|
||||
}
|
||||
}
|
||||
|
||||
DockerBuildImage buildDockerImageForSource = project.tasks.create('buildDockerImageForSource', DockerBuildImage) {
|
||||
dependsOn Arrays.asList(project.rootProject.getTasksByName("clean", true), pullTask)
|
||||
inputDir.set(new File("."))
|
||||
dockerFile.set(new File(new File("testing"), "Dockerfile"))
|
||||
}
|
||||
|
||||
DockerCreateContainer createBuildContainer = project.tasks.create('createBuildContainer', DockerCreateContainer) {
|
||||
File baseWorkingDir = new File(System.getProperty("docker.work.dir") ? System.getProperty("docker.work.dir") : System.getProperty("java.io.tmpdir"))
|
||||
File gradleDir = new File(baseWorkingDir, "gradle")
|
||||
File mavenDir = new File(baseWorkingDir, "maven")
|
||||
doFirst {
|
||||
if (!gradleDir.exists()) {
|
||||
gradleDir.mkdirs()
|
||||
}
|
||||
if (!mavenDir.exists()) {
|
||||
mavenDir.mkdirs()
|
||||
}
|
||||
|
||||
logger.info("Will use: ${gradleDir.absolutePath} for caching gradle artifacts")
|
||||
}
|
||||
|
||||
dependsOn buildDockerImageForSource
|
||||
targetImageId buildDockerImageForSource.getImageId()
|
||||
binds = [(gradleDir.absolutePath): "/tmp/gradle", (mavenDir.absolutePath): "/home/root/.m2"]
|
||||
}
|
||||
|
||||
DockerStartContainer startBuildContainer = project.tasks.create('startBuildContainer', DockerStartContainer) {
|
||||
dependsOn createBuildContainer
|
||||
targetContainerId createBuildContainer.getContainerId()
|
||||
}
|
||||
|
||||
DockerLogsContainer logBuildContainer = project.tasks.create('logBuildContainer', DockerLogsContainer) {
|
||||
dependsOn startBuildContainer
|
||||
targetContainerId createBuildContainer.getContainerId()
|
||||
follow = true
|
||||
}
|
||||
|
||||
DockerWaitContainer waitForBuildContainer = project.tasks.create('waitForBuildContainer', DockerWaitContainer) {
|
||||
dependsOn logBuildContainer
|
||||
targetContainerId createBuildContainer.getContainerId()
|
||||
doLast {
|
||||
if (getExitCode() != 0) {
|
||||
throw new GradleException("Failed to build docker image, aborting build")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DockerCommitImage commitBuildImageResult = project.tasks.create('commitBuildImageResult', DockerCommitImage) {
|
||||
dependsOn waitForBuildContainer
|
||||
targetContainerId createBuildContainer.getContainerId()
|
||||
}
|
||||
|
||||
|
||||
DockerTagImage tagBuildImageResult = project.tasks.create('tagBuildImageResult', DockerTagImage) {
|
||||
dependsOn commitBuildImageResult
|
||||
imageId = commitBuildImageResult.getImageId()
|
||||
tag = System.getProperty("docker.provided.tag") ? System.getProperty("docker.provided.tag") : "${UUID.randomUUID().toString().toLowerCase().subSequence(0, 12)}"
|
||||
repository = registryName
|
||||
}
|
||||
|
||||
DockerPushImage pushBuildImage = project.tasks.create('pushBuildImage', DockerPushImage) {
|
||||
dependsOn tagBuildImageResult
|
||||
doFirst {
|
||||
registryCredentials = registryCredentialsForPush
|
||||
}
|
||||
imageName = registryName
|
||||
tag = tagBuildImageResult.tag
|
||||
}
|
||||
this.pushTask = pushBuildImage
|
||||
|
||||
|
||||
DockerRemoveContainer deleteContainer = project.tasks.create('deleteBuildContainer', DockerRemoveContainer) {
|
||||
dependsOn pushBuildImage
|
||||
targetContainerId createBuildContainer.getContainerId()
|
||||
}
|
||||
DockerRemoveImage deleteTaggedImage = project.tasks.create('deleteTaggedImage', DockerRemoveImage) {
|
||||
dependsOn pushBuildImage
|
||||
force = true
|
||||
targetImageId commitBuildImageResult.getImageId()
|
||||
}
|
||||
DockerRemoveImage deleteBuildImage = project.tasks.create('deleteBuildImage', DockerRemoveImage) {
|
||||
dependsOn deleteContainer, deleteTaggedImage
|
||||
force = true
|
||||
targetImageId buildDockerImageForSource.getImageId()
|
||||
}
|
||||
if (System.getProperty("docker.keep.image") == null) {
|
||||
pushBuildImage.finalizedBy(deleteContainer, deleteBuildImage, deleteTaggedImage)
|
||||
}
|
||||
}
|
||||
}
|
147
buildSrc/src/main/groovy/net/corda/testing/ImageBuilding.java
Normal file
147
buildSrc/src/main/groovy/net/corda/testing/ImageBuilding.java
Normal file
@ -0,0 +1,147 @@
|
||||
package net.corda.testing;
|
||||
|
||||
import com.bmuschko.gradle.docker.DockerRegistryCredentials;
|
||||
import com.bmuschko.gradle.docker.tasks.container.*;
|
||||
import com.bmuschko.gradle.docker.tasks.image.*;
|
||||
import org.gradle.api.GradleException;
|
||||
import org.gradle.api.Plugin;
|
||||
import org.gradle.api.Project;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* this plugin is responsible for setting up all the required docker image building tasks required for producing and pushing an
|
||||
* image of the current build output to a remote container registry
|
||||
*/
|
||||
public class ImageBuilding implements Plugin<Project> {
|
||||
|
||||
public static final String registryName = "stefanotestingcr.azurecr.io/testing";
|
||||
public DockerPushImage pushTask;
|
||||
|
||||
@Override
|
||||
public void apply(@NotNull final Project project) {
|
||||
|
||||
final DockerRegistryCredentials registryCredentialsForPush = new DockerRegistryCredentials(project.getObjects());
|
||||
registryCredentialsForPush.getUsername().set("stefanotestingcr");
|
||||
registryCredentialsForPush.getPassword().set(System.getProperty("docker.push.password", ""));
|
||||
|
||||
final DockerPullImage pullTask = project.getTasks().create("pullBaseImage", DockerPullImage.class, dockerPullImage -> {
|
||||
dockerPullImage.doFirst(task -> dockerPullImage.setRegistryCredentials(registryCredentialsForPush));
|
||||
dockerPullImage.getRepository().set("stefanotestingcr.azurecr.io/buildbase");
|
||||
dockerPullImage.getTag().set("latest");
|
||||
});
|
||||
|
||||
|
||||
final DockerBuildImage buildDockerImageForSource = project.getTasks().create("buildDockerImageForSource", DockerBuildImage.class,
|
||||
dockerBuildImage -> {
|
||||
dockerBuildImage.dependsOn(Arrays.asList(project.getRootProject().getTasksByName("clean", true), pullTask));
|
||||
dockerBuildImage.getInputDir().set(new File("."));
|
||||
dockerBuildImage.getDockerFile().set(new File(new File("testing"), "Dockerfile"));
|
||||
});
|
||||
|
||||
final DockerCreateContainer createBuildContainer = project.getTasks().create("createBuildContainer", DockerCreateContainer.class,
|
||||
dockerCreateContainer -> {
|
||||
final File baseWorkingDir = new File(System.getProperty("docker.work.dir") != null &&
|
||||
!System.getProperty("docker.work.dir").isEmpty() ?
|
||||
System.getProperty("docker.work.dir") : System.getProperty("java.io.tmpdir"));
|
||||
final File gradleDir = new File(baseWorkingDir, "gradle");
|
||||
final File mavenDir = new File(baseWorkingDir, "maven");
|
||||
dockerCreateContainer.doFirst(task -> {
|
||||
if (!gradleDir.exists()) {
|
||||
gradleDir.mkdirs();
|
||||
}
|
||||
if (!mavenDir.exists()) {
|
||||
mavenDir.mkdirs();
|
||||
}
|
||||
|
||||
project.getLogger().info("Will use: ${gradleDir.absolutePath} for caching gradle artifacts");
|
||||
});
|
||||
dockerCreateContainer.dependsOn(buildDockerImageForSource);
|
||||
dockerCreateContainer.targetImageId(buildDockerImageForSource.getImageId());
|
||||
final Map<String, String> map = new HashMap<>();
|
||||
map.put(gradleDir.getAbsolutePath(), "/tmp/gradle");
|
||||
map.put(mavenDir.getAbsolutePath(), "/home/root/.m2");
|
||||
dockerCreateContainer.getBinds().set(map);
|
||||
});
|
||||
|
||||
|
||||
final DockerStartContainer startBuildContainer = project.getTasks().create("startBuildContainer", DockerStartContainer.class,
|
||||
dockerStartContainer -> {
|
||||
dockerStartContainer.dependsOn(createBuildContainer);
|
||||
dockerStartContainer.targetContainerId(createBuildContainer.getContainerId());
|
||||
});
|
||||
|
||||
final DockerLogsContainer logBuildContainer = project.getTasks().create("logBuildContainer", DockerLogsContainer.class,
|
||||
dockerLogsContainer -> {
|
||||
dockerLogsContainer.dependsOn(startBuildContainer);
|
||||
dockerLogsContainer.targetContainerId(createBuildContainer.getContainerId());
|
||||
dockerLogsContainer.getFollow().set(true);
|
||||
});
|
||||
|
||||
final DockerWaitContainer waitForBuildContainer = project.getTasks().create("waitForBuildContainer", DockerWaitContainer.class,
|
||||
dockerWaitContainer -> {
|
||||
dockerWaitContainer.dependsOn(logBuildContainer);
|
||||
dockerWaitContainer.targetContainerId(createBuildContainer.getContainerId());
|
||||
dockerWaitContainer.doLast(task -> {
|
||||
if (dockerWaitContainer.getExitCode() != 0) {
|
||||
throw new GradleException("Failed to build docker image, aborting build");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
final DockerCommitImage commitBuildImageResult = project.getTasks().create("commitBuildImageResult", DockerCommitImage.class,
|
||||
dockerCommitImage -> {
|
||||
dockerCommitImage.dependsOn(waitForBuildContainer);
|
||||
dockerCommitImage.targetContainerId(createBuildContainer.getContainerId());
|
||||
});
|
||||
|
||||
|
||||
final DockerTagImage tagBuildImageResult = project.getTasks().create("tagBuildImageResult", DockerTagImage.class, dockerTagImage -> {
|
||||
dockerTagImage.dependsOn(commitBuildImageResult);
|
||||
dockerTagImage.getImageId().set(commitBuildImageResult.getImageId());
|
||||
dockerTagImage.getTag().set(System.getProperty("docker.provided.tag",
|
||||
"${UUID.randomUUID().toString().toLowerCase().subSequence(0, 12)}"));
|
||||
dockerTagImage.getRepository().set(registryName);
|
||||
});
|
||||
|
||||
|
||||
final DockerPushImage pushBuildImage = project.getTasks().create("pushBuildImage", DockerPushImage.class, dockerPushImage -> {
|
||||
dockerPushImage.dependsOn(tagBuildImageResult);
|
||||
dockerPushImage.doFirst(task -> dockerPushImage.setRegistryCredentials(registryCredentialsForPush));
|
||||
dockerPushImage.getImageName().set(registryName);
|
||||
dockerPushImage.getTag().set(tagBuildImageResult.getTag());
|
||||
});
|
||||
|
||||
this.pushTask = pushBuildImage;
|
||||
|
||||
|
||||
final DockerRemoveContainer deleteContainer = project.getTasks().create("deleteBuildContainer", DockerRemoveContainer.class,
|
||||
dockerRemoveContainer -> {
|
||||
dockerRemoveContainer.dependsOn(pushBuildImage);
|
||||
dockerRemoveContainer.targetContainerId(createBuildContainer.getContainerId());
|
||||
});
|
||||
|
||||
|
||||
final DockerRemoveImage deleteTaggedImage = project.getTasks().create("deleteTaggedImage", DockerRemoveImage.class,
|
||||
dockerRemoveImage -> {
|
||||
dockerRemoveImage.dependsOn(pushBuildImage);
|
||||
dockerRemoveImage.getForce().set(true);
|
||||
dockerRemoveImage.targetImageId(commitBuildImageResult.getImageId());
|
||||
});
|
||||
|
||||
final DockerRemoveImage deleteBuildImage = project.getTasks().create("deleteBuildImage", DockerRemoveImage.class,
|
||||
dockerRemoveImage -> {
|
||||
dockerRemoveImage.dependsOn(deleteContainer, deleteTaggedImage);
|
||||
dockerRemoveImage.getForce().set(true);
|
||||
dockerRemoveImage.targetImageId(buildDockerImageForSource.getImageId());
|
||||
});
|
||||
|
||||
if (System.getProperty("docker.keep.image") == null) {
|
||||
pushBuildImage.finalizedBy(deleteContainer, deleteBuildImage, deleteTaggedImage);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user