use of com.bmuschko.gradle.docker.tasks.container.DockerCopyFileFromContainer in project micronaut-gradle-plugin by micronaut-projects.
the class MicronautDockerPlugin method configureNativeDockerBuild.
private TaskProvider<NativeImageDockerfile> configureNativeDockerBuild(Project project, TaskContainer tasks, TaskProvider<BuildLayersTask> buildLayersTask, String imageName) {
File f = project.file(adaptTaskName("DockerfileNative", imageName));
TaskProvider<NativeImageDockerfile> dockerFileTask;
String dockerfileNativeTaskName = adaptTaskName("dockerfileNative", imageName);
Provider<RegularFile> targetDockerFile = project.getLayout().getBuildDirectory().file("docker/native-" + imageName + "/DockerfileNative");
if (f.exists()) {
dockerFileTask = tasks.register(dockerfileNativeTaskName, NativeImageDockerfile.class, task -> {
task.setGroup(BasePlugin.BUILD_GROUP);
task.setDescription("Builds a Native Docker File for image " + imageName);
task.instructionsFromTemplate(f);
task.getDestFile().set(targetDockerFile);
});
} else {
dockerFileTask = tasks.register(dockerfileNativeTaskName, NativeImageDockerfile.class, task -> {
task.setGroup(BasePlugin.BUILD_GROUP);
task.setDescription("Builds a Native Docker File for image " + imageName);
task.getDestFile().set(targetDockerFile);
});
}
TaskProvider<PrepareDockerContext> prepareContext = tasks.register(adaptTaskName("dockerPrepareContext", imageName), PrepareDockerContext.class, context -> {
// Because docker requires all files to be found in the build context we need to
// copy the configuration file directories into the build context
context.getOutputDirectory().set(project.getLayout().getBuildDirectory().dir("docker/native-" + imageName + "/config-dirs"));
context.getInputDirectories().from(dockerFileTask.map(t -> t.getNativeImageOptions().get().getConfigurationFileDirectories()));
});
TaskProvider<DockerBuildImage> dockerBuildTask = tasks.register(adaptTaskName("dockerBuildNative", imageName), DockerBuildImage.class, task -> {
task.setGroup(BasePlugin.BUILD_GROUP);
task.setDescription("Builds a Native Docker Image using GraalVM (image " + imageName + ")");
task.getInputs().files(prepareContext);
if (f.exists()) {
task.getDockerFile().set(f);
} else {
task.getDockerFile().convention(dockerFileTask.flatMap(Dockerfile::getDestFile));
}
task.getImages().set(Collections.singletonList(project.getName()));
task.dependsOn(buildLayersTask);
task.getInputDir().set(dockerFileTask.flatMap(Dockerfile::getDestDir));
});
TaskProvider<DockerPushImage> pushDockerImage = tasks.register(adaptTaskName("dockerPushNative", imageName), DockerPushImage.class);
pushDockerImage.configure(task -> {
task.dependsOn(dockerBuildTask);
task.setGroup("upload");
task.setDescription("Pushes a Native Docker Image using GraalVM (image " + imageName + ")");
task.getImages().set(dockerBuildTask.flatMap(DockerBuildImage::getImages));
});
project.afterEvaluate(p -> {
MicronautRuntime mr = PluginsHelper.resolveRuntime(p);
if (mr.isLambdaProvided()) {
TaskContainer taskContainer = p.getTasks();
TaskProvider<DockerCreateContainer> createLambdaContainer = taskContainer.register(adaptTaskName("createLambdaContainer", imageName), DockerCreateContainer.class, task -> {
task.dependsOn(dockerBuildTask);
task.targetImageId(dockerBuildTask.flatMap(DockerBuildImage::getImageId));
});
TaskProvider<DockerCopyFileFromContainer> buildLambdaZip = taskContainer.register(adaptTaskName("buildNativeLambda", imageName), DockerCopyFileFromContainer.class);
Provider<String> lambdaZip = project.getLayout().getBuildDirectory().dir("libs").map(dir -> dir.file(project.getName() + "-" + project.getVersion() + "-" + simpleNameOf("lambda", imageName) + ".zip").getAsFile().getAbsolutePath());
TaskProvider<DockerRemoveContainer> removeContainer = taskContainer.register(adaptTaskName("destroyLambdaContainer", imageName), DockerRemoveContainer.class);
removeContainer.configure(task -> {
task.mustRunAfter(buildLambdaZip);
task.getContainerId().set(createLambdaContainer.flatMap(DockerCreateContainer::getContainerId));
});
buildLambdaZip.configure(task -> {
task.dependsOn(createLambdaContainer);
task.getContainerId().set(createLambdaContainer.flatMap(DockerCreateContainer::getContainerId));
task.getRemotePath().set("/function/function.zip");
task.getHostPath().set(lambdaZip);
task.doLast(new Action<Task>() {
@Override
public void execute(Task task1) {
System.out.println("AWS Lambda ZIP built: " + lambdaZip.get());
}
});
task.finalizedBy(removeContainer);
});
}
});
return dockerFileTask;
}
Aggregations