Search in sources :

Example 1 with DockerPushImage

use of com.bmuschko.gradle.docker.tasks.image.DockerPushImage in project micronaut-gradle-plugin by micronaut-projects.

the class MicronautDockerPlugin method configureDockerBuild.

private Optional<TaskProvider<MicronautDockerfile>> configureDockerBuild(Project project, TaskContainer tasks, TaskProvider<BuildLayersTask> buildLayersTask, String imageName) {
    File f = project.file(adaptTaskName("Dockerfile", imageName));
    TaskProvider<? extends Dockerfile> dockerFileTask;
    String dockerFileTaskName = adaptTaskName("dockerfile", imageName);
    Provider<RegularFile> targetDockerFile = project.getLayout().getBuildDirectory().file("docker/" + imageName + "/Dockerfile");
    if (f.exists()) {
        dockerFileTask = tasks.register(dockerFileTaskName, Dockerfile.class, task -> {
            task.setGroup(BasePlugin.BUILD_GROUP);
            task.setDescription("Builds a Docker File for image " + imageName);
            task.getDestFile().set(targetDockerFile);
            task.instructionsFromTemplate(f);
        });
    } else {
        dockerFileTask = tasks.register(dockerFileTaskName, MicronautDockerfile.class, task -> {
            task.setGroup(BasePlugin.BUILD_GROUP);
            task.setDescription("Builds a Docker File for image " + imageName);
            task.getDestFile().set(targetDockerFile);
            task.setupDockerfileInstructions();
        });
    }
    TaskProvider<DockerBuildImage> dockerBuildTask = tasks.register(adaptTaskName("dockerBuild", imageName), DockerBuildImage.class, task -> {
        task.dependsOn(buildLayersTask);
        task.setGroup(BasePlugin.BUILD_GROUP);
        task.setDescription("Builds a Docker Image (image " + imageName + ")");
        if (f.exists()) {
            task.getDockerFile().set(f);
        } else {
            task.getDockerFile().convention(dockerFileTask.flatMap(Dockerfile::getDestFile));
        }
        task.getImages().set(Collections.singletonList(project.getName()));
        ;
        task.getInputDir().set(dockerFileTask.flatMap(Dockerfile::getDestDir));
    });
    TaskProvider<DockerPushImage> pushDockerImage = tasks.register(adaptTaskName("dockerPush", imageName), DockerPushImage.class, task -> {
        task.dependsOn(dockerBuildTask);
        task.setGroup("upload");
        task.setDescription("Pushes the " + imageName + " Docker Image");
        task.getImages().set(dockerBuildTask.flatMap(DockerBuildImage::getImages));
    });
    if (!f.exists()) {
        return Optional.of((TaskProvider<MicronautDockerfile>) dockerFileTask);
    }
    return Optional.empty();
}
Also used : Dockerfile(com.bmuschko.gradle.docker.tasks.image.Dockerfile) DockerPushImage(com.bmuschko.gradle.docker.tasks.image.DockerPushImage) HashMap(java.util.HashMap) DefaultMicronautDockerImage(io.micronaut.gradle.docker.model.DefaultMicronautDockerImage) NamedDomainObjectContainer(org.gradle.api.NamedDomainObjectContainer) BasePlugin(org.gradle.api.plugins.BasePlugin) FileCollection(org.gradle.api.file.FileCollection) DockerExtension(com.bmuschko.gradle.docker.DockerExtension) SourceSet(org.gradle.api.tasks.SourceSet) ArrayList(java.util.ArrayList) Configuration(org.gradle.api.artifacts.Configuration) Task(org.gradle.api.Task) Provider(org.gradle.api.provider.Provider) RuntimeKind(io.micronaut.gradle.docker.model.RuntimeKind) DockerRemoveContainer(com.bmuschko.gradle.docker.tasks.container.DockerRemoveContainer) PrepareDockerContext(io.micronaut.gradle.docker.tasks.PrepareDockerContext) TaskProvider(org.gradle.api.tasks.TaskProvider) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) Map(java.util.Map) ExtensionContainer(org.gradle.api.plugins.ExtensionContainer) MicronautExtension(io.micronaut.gradle.MicronautExtension) LayerKind(io.micronaut.gradle.docker.model.LayerKind) Strings.capitalize(io.micronaut.gradle.Strings.capitalize) RUNTIME_CLASSPATH_CONFIGURATION_NAME(org.gradle.api.plugins.JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME) Jar(org.gradle.api.tasks.bundling.Jar) Action(org.gradle.api.Action) Project(org.gradle.api.Project) DockerBuildImage(com.bmuschko.gradle.docker.tasks.image.DockerBuildImage) PluginsHelper(io.micronaut.gradle.PluginsHelper) MicronautBasePlugin(io.micronaut.gradle.MicronautBasePlugin) MicronautDockerImage(io.micronaut.gradle.docker.model.MicronautDockerImage) DockerCopyFileFromContainer(com.bmuschko.gradle.docker.tasks.container.DockerCopyFileFromContainer) JavaApplication(org.gradle.api.plugins.JavaApplication) File(java.io.File) Consumer(java.util.function.Consumer) List(java.util.List) TaskContainer(org.gradle.api.tasks.TaskContainer) RegularFile(org.gradle.api.file.RegularFile) MicronautRuntime(io.micronaut.gradle.MicronautRuntime) Optional(java.util.Optional) BuildLayersTask(io.micronaut.gradle.docker.tasks.BuildLayersTask) Collections(java.util.Collections) Plugin(org.gradle.api.Plugin) DockerCreateContainer(com.bmuschko.gradle.docker.tasks.container.DockerCreateContainer) Dockerfile(com.bmuschko.gradle.docker.tasks.image.Dockerfile) RegularFile(org.gradle.api.file.RegularFile) DockerPushImage(com.bmuschko.gradle.docker.tasks.image.DockerPushImage) DockerBuildImage(com.bmuschko.gradle.docker.tasks.image.DockerBuildImage) File(java.io.File) RegularFile(org.gradle.api.file.RegularFile)

Example 2 with DockerPushImage

use of com.bmuschko.gradle.docker.tasks.image.DockerPushImage 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;
}
Also used : Dockerfile(com.bmuschko.gradle.docker.tasks.image.Dockerfile) DockerPushImage(com.bmuschko.gradle.docker.tasks.image.DockerPushImage) HashMap(java.util.HashMap) DefaultMicronautDockerImage(io.micronaut.gradle.docker.model.DefaultMicronautDockerImage) NamedDomainObjectContainer(org.gradle.api.NamedDomainObjectContainer) BasePlugin(org.gradle.api.plugins.BasePlugin) FileCollection(org.gradle.api.file.FileCollection) DockerExtension(com.bmuschko.gradle.docker.DockerExtension) SourceSet(org.gradle.api.tasks.SourceSet) ArrayList(java.util.ArrayList) Configuration(org.gradle.api.artifacts.Configuration) Task(org.gradle.api.Task) Provider(org.gradle.api.provider.Provider) RuntimeKind(io.micronaut.gradle.docker.model.RuntimeKind) DockerRemoveContainer(com.bmuschko.gradle.docker.tasks.container.DockerRemoveContainer) PrepareDockerContext(io.micronaut.gradle.docker.tasks.PrepareDockerContext) TaskProvider(org.gradle.api.tasks.TaskProvider) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) Map(java.util.Map) ExtensionContainer(org.gradle.api.plugins.ExtensionContainer) MicronautExtension(io.micronaut.gradle.MicronautExtension) LayerKind(io.micronaut.gradle.docker.model.LayerKind) Strings.capitalize(io.micronaut.gradle.Strings.capitalize) RUNTIME_CLASSPATH_CONFIGURATION_NAME(org.gradle.api.plugins.JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME) Jar(org.gradle.api.tasks.bundling.Jar) Action(org.gradle.api.Action) Project(org.gradle.api.Project) DockerBuildImage(com.bmuschko.gradle.docker.tasks.image.DockerBuildImage) PluginsHelper(io.micronaut.gradle.PluginsHelper) MicronautBasePlugin(io.micronaut.gradle.MicronautBasePlugin) MicronautDockerImage(io.micronaut.gradle.docker.model.MicronautDockerImage) DockerCopyFileFromContainer(com.bmuschko.gradle.docker.tasks.container.DockerCopyFileFromContainer) JavaApplication(org.gradle.api.plugins.JavaApplication) File(java.io.File) Consumer(java.util.function.Consumer) List(java.util.List) TaskContainer(org.gradle.api.tasks.TaskContainer) RegularFile(org.gradle.api.file.RegularFile) MicronautRuntime(io.micronaut.gradle.MicronautRuntime) Optional(java.util.Optional) BuildLayersTask(io.micronaut.gradle.docker.tasks.BuildLayersTask) Collections(java.util.Collections) Plugin(org.gradle.api.Plugin) DockerCreateContainer(com.bmuschko.gradle.docker.tasks.container.DockerCreateContainer) Task(org.gradle.api.Task) BuildLayersTask(io.micronaut.gradle.docker.tasks.BuildLayersTask) DockerBuildImage(com.bmuschko.gradle.docker.tasks.image.DockerBuildImage) DockerCopyFileFromContainer(com.bmuschko.gradle.docker.tasks.container.DockerCopyFileFromContainer) MicronautRuntime(io.micronaut.gradle.MicronautRuntime) RegularFile(org.gradle.api.file.RegularFile) DockerCreateContainer(com.bmuschko.gradle.docker.tasks.container.DockerCreateContainer) DockerRemoveContainer(com.bmuschko.gradle.docker.tasks.container.DockerRemoveContainer) PrepareDockerContext(io.micronaut.gradle.docker.tasks.PrepareDockerContext) TaskContainer(org.gradle.api.tasks.TaskContainer) DockerPushImage(com.bmuschko.gradle.docker.tasks.image.DockerPushImage) File(java.io.File) RegularFile(org.gradle.api.file.RegularFile)

Example 3 with DockerPushImage

use of com.bmuschko.gradle.docker.tasks.image.DockerPushImage in project alfresco-docker-gradle-plugin by xenit-eu.

the class DockerPlugin method apply.

@Override
public void apply(Project project) {
    dockerExtension = project.getExtensions().create("dockerBuild", DockerExtension.class);
    project.getPluginManager().apply(DockerConfigPlugin.class);
    project.getPluginManager().apply(DockerAutotagPlugin.class);
    project.getPluginManager().apply(DockerLabelPlugin.class);
    Deprecation.whileDisabled(() -> {
        project.getPluginManager().apply(DockerLegacyPlugin.class);
    });
    // Configure labeling from git
    dockerExtension.getExtensions().getByType(DockerLabelExtension.class).fromGit();
    // Configure an empty createDockerFile task that can be configured by the user.
    // If no Dockerfile is present in the project, the Dockerfile created by this task
    // will be used by buildDockerImage
    TaskProvider<? extends Dockerfile> dockerfileProvider = project.getTasks().register("createDockerFile", DockerfileWithCopyTask.class, dockerfile -> {
        dockerfile.setDescription("Create a Dockerfile");
        dockerfile.setGroup(TASK_GROUP);
        if (readPropertyFlag(project, WorkaroundDockerdConsecutiveCopyBugAction.FEATURE_FLAG)) {
            dockerfile.doFirst("Mitigate Docker COPY bug", new WorkaroundDockerdConsecutiveCopyBugAction());
        }
        dockerfile.doFirst("Consolidate COPY instructions", new ConsolidateFileCopyInstructionsAction());
    });
    dockerExtension.getDockerFile().convention(dockerfileProvider.flatMap(Dockerfile::getDestFile));
    // Fallback to using the Dockerfile in the root of the project if it exists
    RegularFile detectedDockerFile = project.getLayout().getProjectDirectory().file("Dockerfile");
    if (detectedDockerFile.getAsFile().exists()) {
        dockerExtension.getDockerFile().convention(detectedDockerFile);
    }
    TaskProvider<? extends DockerBuildImage> buildImageProvider = project.getTasks().register("buildDockerImage", DockerBuildImage.class, buildImage -> {
        buildImage.setDescription("Build the docker image");
        buildImage.setGroup(TASK_GROUP);
        buildImage.getPull().set(true);
        buildImage.getRemove().set(true);
        buildImage.getImages().set(dockerExtension.getImages());
        buildImage.getDockerFile().set(dockerExtension.getDockerFile());
        buildImage.getInputDir().set(buildImage.getDockerFile().flatMap(dockerFile -> {
            DirectoryProperty directoryProperty = project.getObjects().directoryProperty();
            directoryProperty.set(dockerFile.getAsFile().getParentFile());
            return directoryProperty;
        }));
    });
    project.getTasks().register("pushDockerImage", DockerPushImage.class, dockerPushImage -> {
        dockerPushImage.setDescription("Push all docker image tags");
        dockerPushImage.setGroup(TASK_GROUP);
        dockerPushImage.getImages().set(buildImageProvider.flatMap(DockerBuildImage::getImages));
        dockerPushImage.dependsOn(buildImageProvider);
    });
    project.getPlugins().withType(DockerComposePlugin.class, dockerComposePlugin -> {
        dockerComposePlugin.getDockerComposeConvention().fromBuildImage("DOCKER_IMAGE", buildImageProvider);
    });
}
Also used : Project(org.gradle.api.Project) Dockerfile(com.bmuschko.gradle.docker.tasks.image.Dockerfile) DockerAutotagPlugin(eu.xenit.gradle.docker.autotag.DockerAutotagPlugin) DockerComposePlugin(eu.xenit.gradle.docker.compose.DockerComposePlugin) DockerPushImage(com.bmuschko.gradle.docker.tasks.image.DockerPushImage) DockerLabelExtension(eu.xenit.gradle.docker.label.DockerLabelExtension) DockerLabelPlugin(eu.xenit.gradle.docker.label.DockerLabelPlugin) DockerLegacyPlugin(eu.xenit.gradle.docker.DockerLegacyPlugin) DockerBuildImage(eu.xenit.gradle.docker.tasks.internal.DockerBuildImage) DockerConfigPlugin(eu.xenit.gradle.docker.config.DockerConfigPlugin) ConsolidateFileCopyInstructionsAction(eu.xenit.gradle.docker.tasks.internal.ConsolidateFileCopyInstructionsAction) TaskProvider(org.gradle.api.tasks.TaskProvider) DirectoryProperty(org.gradle.api.file.DirectoryProperty) RegularFile(org.gradle.api.file.RegularFile) Deprecation(eu.xenit.gradle.docker.internal.Deprecation) Optional(java.util.Optional) DockerfileWithCopyTask(eu.xenit.gradle.docker.tasks.DockerfileWithCopyTask) WorkaroundDockerdConsecutiveCopyBugAction(eu.xenit.gradle.docker.tasks.internal.WorkaroundDockerdConsecutiveCopyBugAction) Plugin(org.gradle.api.Plugin) DirectoryProperty(org.gradle.api.file.DirectoryProperty) RegularFile(org.gradle.api.file.RegularFile) DockerLabelExtension(eu.xenit.gradle.docker.label.DockerLabelExtension) WorkaroundDockerdConsecutiveCopyBugAction(eu.xenit.gradle.docker.tasks.internal.WorkaroundDockerdConsecutiveCopyBugAction) ConsolidateFileCopyInstructionsAction(eu.xenit.gradle.docker.tasks.internal.ConsolidateFileCopyInstructionsAction)

Aggregations

DockerPushImage (com.bmuschko.gradle.docker.tasks.image.DockerPushImage)3 Dockerfile (com.bmuschko.gradle.docker.tasks.image.Dockerfile)3 Optional (java.util.Optional)3 Plugin (org.gradle.api.Plugin)3 Project (org.gradle.api.Project)3 RegularFile (org.gradle.api.file.RegularFile)3 DockerExtension (com.bmuschko.gradle.docker.DockerExtension)2 DockerCopyFileFromContainer (com.bmuschko.gradle.docker.tasks.container.DockerCopyFileFromContainer)2 DockerCreateContainer (com.bmuschko.gradle.docker.tasks.container.DockerCreateContainer)2 DockerRemoveContainer (com.bmuschko.gradle.docker.tasks.container.DockerRemoveContainer)2 DockerBuildImage (com.bmuschko.gradle.docker.tasks.image.DockerBuildImage)2 MicronautBasePlugin (io.micronaut.gradle.MicronautBasePlugin)2 MicronautExtension (io.micronaut.gradle.MicronautExtension)2 MicronautRuntime (io.micronaut.gradle.MicronautRuntime)2 PluginsHelper (io.micronaut.gradle.PluginsHelper)2 Strings.capitalize (io.micronaut.gradle.Strings.capitalize)2 DefaultMicronautDockerImage (io.micronaut.gradle.docker.model.DefaultMicronautDockerImage)2 LayerKind (io.micronaut.gradle.docker.model.LayerKind)2 MicronautDockerImage (io.micronaut.gradle.docker.model.MicronautDockerImage)2 RuntimeKind (io.micronaut.gradle.docker.model.RuntimeKind)2