Search in sources :

Example 1 with ComposeSettings

use of com.avast.gradle.dockercompose.ComposeSettings in project alfresco-docker-gradle-plugin by xenit-eu.

the class DockerConfigPlugin method apply.

@Override
public void apply(Project project) {
    GradleVersionRequirement.atLeast("5.6", "use the " + PLUGIN_ID + " plugin");
    // Set up deprecation warnings
    Deprecation.setStartParameter(project.getGradle().getStartParameter());
    project.getGradle().projectsEvaluated(g -> {
        g.buildFinished(buildResult -> {
            Deprecation.printSummary();
        });
    });
    // Rest of the configuration
    DockerConfig dockerConfig = new DockerConfig(project);
    project.getPluginManager().apply(DockerRemoteApiPlugin.class);
    DockerExtension dockerExtension = project.getExtensions().getByType(DockerExtension.class);
    if (dockerConfig.getUrl() != null) {
        dockerExtension.getUrl().set(dockerConfig.getUrl());
    }
    if (dockerConfig.getCertPath() != null) {
        dockerExtension.getCertPath().set(new File(dockerConfig.getCertPath()));
    }
    if (dockerConfig.getRegistryUrl() != null) {
        dockerExtension.registryCredentials(registryCredentials -> {
            registryCredentials.getUrl().set(dockerConfig.getRegistryUrl());
            registryCredentials.getUsername().set(dockerConfig.getRegistryUsername());
            registryCredentials.getPassword().set(dockerConfig.getRegistryPassword());
        });
    }
    project.getPlugins().withType(DockerComposePlugin.class, dockerComposePlugin -> {
        ComposeSettings composeExtension = dockerComposePlugin.getComposeSettings();
        composeExtension.getUseComposeFiles().add("docker-compose.yml");
        composeExtension.getEnvironment().put("DOCKER_HOST", dockerExtension.getUrl().get());
        composeExtension.getEnvironment().put("SERVICES_HOST", dockerConfig.getExposeIp());
        composeExtension.getEnvironment().put("DOCKER_IP", dockerConfig.getExposeIp());
        if (dockerConfig.getCertPath() != null) {
            composeExtension.getEnvironment().put("DOCKER_CERT_PATH", dockerConfig.getCertPath());
        }
    });
    project.getPlugins().withType(com.avast.gradle.dockercompose.DockerComposePlugin.class, dockerComposePlugin -> {
        throw new GradleException("The com.avast.gradle.docker-compose plugin does not work together with " + PLUGIN_ID + ".\n" + "Use " + DockerComposePlugin.PLUGIN_ID + " for docker-compose functionality.");
    });
    if (project.getGradle().getStartParameter().isOffline()) {
        // We need to create a separate task to disable pull on DockerBuildImage tasks, because in a doFirst block, providers are already finalized.
        TaskProvider<DefaultTask> disableDockerBuildImagePull = project.getTasks().register("_" + PLUGIN_ID + "_disableDockerBuildImagePull_" + UUID.randomUUID(), DefaultTask.class, task -> {
            task.setDescription("[" + PLUGIN_ID + " internal] Disables pull flag on DockerBuildImage tasks when Gradle runs in offline mode.");
            task.doLast(new Action<Task>() {

                @Override
                public void execute(Task task) {
                    project.getTasks().withType(DockerBuildImage.class).configureEach(dockerBuildImage -> {
                        if (dockerBuildImage.getPull().get()) {
                            dockerBuildImage.doFirst(new Action<Task>() {

                                @Override
                                public void execute(Task task) {
                                    LOGGER.warn("Gradle is running in offline mode, automatic pull has been disabled.");
                                }
                            });
                            dockerBuildImage.getPull().set(false);
                        }
                    });
                }
            });
        });
        project.getTasks().withType(DockerBuildImage.class).configureEach(dockerBuildImage -> {
            dockerBuildImage.dependsOn(disableDockerBuildImagePull);
        });
    }
}
Also used : ComposeSettings(com.avast.gradle.dockercompose.ComposeSettings) Action(org.gradle.api.Action) Project(org.gradle.api.Project) DockerBuildImage(com.bmuschko.gradle.docker.tasks.image.DockerBuildImage) DockerComposePlugin(eu.xenit.gradle.docker.compose.DockerComposePlugin) UUID(java.util.UUID) File(java.io.File) Logger(org.gradle.api.logging.Logger) DockerExtension(com.bmuschko.gradle.docker.DockerExtension) GradleVersionRequirement(eu.xenit.gradle.docker.internal.GradleVersionRequirement) Task(org.gradle.api.Task) GradleException(org.gradle.api.GradleException) TaskProvider(org.gradle.api.tasks.TaskProvider) Logging(org.gradle.api.logging.Logging) DockerRemoteApiPlugin(com.bmuschko.gradle.docker.DockerRemoteApiPlugin) Deprecation(eu.xenit.gradle.docker.internal.Deprecation) DefaultTask(org.gradle.api.DefaultTask) Plugin(org.gradle.api.Plugin) Task(org.gradle.api.Task) DefaultTask(org.gradle.api.DefaultTask) Action(org.gradle.api.Action) DockerExtension(com.bmuschko.gradle.docker.DockerExtension) ComposeSettings(com.avast.gradle.dockercompose.ComposeSettings) DockerBuildImage(com.bmuschko.gradle.docker.tasks.image.DockerBuildImage) GradleException(org.gradle.api.GradleException) DefaultTask(org.gradle.api.DefaultTask) File(java.io.File)

Aggregations

ComposeSettings (com.avast.gradle.dockercompose.ComposeSettings)1 DockerExtension (com.bmuschko.gradle.docker.DockerExtension)1 DockerRemoteApiPlugin (com.bmuschko.gradle.docker.DockerRemoteApiPlugin)1 DockerBuildImage (com.bmuschko.gradle.docker.tasks.image.DockerBuildImage)1 DockerComposePlugin (eu.xenit.gradle.docker.compose.DockerComposePlugin)1 Deprecation (eu.xenit.gradle.docker.internal.Deprecation)1 GradleVersionRequirement (eu.xenit.gradle.docker.internal.GradleVersionRequirement)1 File (java.io.File)1 UUID (java.util.UUID)1 Action (org.gradle.api.Action)1 DefaultTask (org.gradle.api.DefaultTask)1 GradleException (org.gradle.api.GradleException)1 Plugin (org.gradle.api.Plugin)1 Project (org.gradle.api.Project)1 Task (org.gradle.api.Task)1 Logger (org.gradle.api.logging.Logger)1 Logging (org.gradle.api.logging.Logging)1 TaskProvider (org.gradle.api.tasks.TaskProvider)1