Search in sources :

Example 1 with DeploymentExtension

use of org.curioswitch.gradle.plugins.curioserver.DeploymentExtension in project curiostack by curioswitch.

the class GcloudPlugin method addGenerateCloudBuildTask.

private static void addGenerateCloudBuildTask(Project rootProject) {
    Task generateCloudBuild = rootProject.getTasks().create("gcloudGenerateCloudBuild");
    generateCloudBuild.doLast(t -> {
        String javaBuilder = "gcr.io/$PROJECT_ID/java-cloud-builder";
        String dockerBuilder = "gcr.io/cloud-builders/docker";
        String kubectlBuilder = "gcr.io/cloud-builders/kubectl";
        ImmutableGcloudExtension config = rootProject.getExtensions().getByType(GcloudExtension.class);
        File existingCloudbuildFile = rootProject.file("cloudbuild.yaml");
        final CloudBuild existingCloudBuild;
        try {
            existingCloudBuild = !existingCloudbuildFile.exists() ? null : OBJECT_MAPPER.readValue(existingCloudbuildFile, CloudBuild.class);
        } catch (IOException e) {
            throw new UncheckedIOException("Could not parse existing cloudbuild file.", e);
        }
        String deepenGitRepoId = "curio-generated-deepen-git-repo";
        String refreshBuildImageId = "curio-generated-refresh-build-image";
        String buildAllImageId = "curio-generated-build-all";
        List<CloudBuildStep> serverSteps = rootProject.getAllprojects().stream().filter(proj -> proj.getPlugins().hasPlugin(CurioServerPlugin.class)).flatMap(proj -> {
            String archivesBaseName = proj.getConvention().getPlugin(BasePluginConvention.class).getArchivesBaseName();
            String buildImageId = "curio-generated-build-" + archivesBaseName + "-image";
            String pushLatestTagId = "curio-generated-push-" + archivesBaseName + "-latest";
            String pushRevisionTagId = "curio-generated-push-" + archivesBaseName + "-revision";
            String deployId = "curio-generated-deploy-" + archivesBaseName;
            String latestTag = config.containerRegistry() + "/$PROJECT_ID/" + archivesBaseName;
            String revisionTag = config.containerRegistry() + "/$PROJECT_ID/" + archivesBaseName + ":$REVISION_ID";
            String dockerPath = Paths.get(rootProject.getProjectDir().getAbsolutePath()).relativize(Paths.get(new File(proj.getBuildDir(), "docker").getAbsolutePath())).toString();
            DeploymentExtension deployment = proj.getExtensions().getByType(DeploymentExtension.class);
            DeploymentConfiguration alpha = deployment.getTypes().getByName("alpha");
            ImmutableList.Builder<CloudBuildStep> steps = ImmutableList.builder();
            steps.add(ImmutableCloudBuildStep.builder().id(buildImageId).addWaitFor(buildAllImageId).name(dockerBuilder).entrypoint("/bin/bash").args(ImmutableList.of("-c", "test -e " + dockerPath + " && docker build --tag=" + latestTag + " --tag=" + revisionTag + " " + dockerPath + " || echo Skipping...")).build());
            steps.add(ImmutableCloudBuildStep.builder().id(pushLatestTagId).addWaitFor(buildImageId).name(dockerBuilder).entrypoint("/bin/bash").args(ImmutableList.of("-c", "test -e " + dockerPath + " && docker push " + latestTag + " || echo Skipping...")).build());
            steps.add(ImmutableCloudBuildStep.builder().id(pushRevisionTagId).addWaitFor(buildImageId).name(dockerBuilder).entrypoint("/bin/bash").args(ImmutableList.of("-c", "test -e " + dockerPath + " && docker push " + revisionTag + " || echo Skipping...")).build());
            if (deployment.autoDeployAlpha()) {
                steps.add(ImmutableCloudBuildStep.builder().id(deployId).addWaitFor(pushLatestTagId).name(kubectlBuilder).entrypoint("/bin/bash").args(ImmutableList.of("-c", "test -e " + dockerPath + " && /builder/kubectl.bash --namespace=" + alpha.namespace() + " patch deployment/" + alpha.deploymentName() + " -p " + "'{\"spec\": {\"template\": {\"metadata\": {\"labels\": {\"revision\": \"$REVISION_ID\" }}}}}'" + " || echo Skipping...")).env(ImmutableList.of("CLOUDSDK_COMPUTE_ZONE=" + config.clusterZone(), "CLOUDSDK_CONTAINER_CLUSTER=" + config.clusterName())).build());
            }
            return steps.build().stream();
        }).collect(toImmutableList());
        List<CloudBuildStep> steps = new ArrayList<>();
        steps.add(ImmutableCloudBuildStep.builder().id(deepenGitRepoId).addWaitFor("-").name("gcr.io/cloud-builders/git").args(ImmutableList.of("fetch", "origin", "master", "--depth=10")).build());
        steps.add(ImmutableCloudBuildStep.builder().id(refreshBuildImageId).addWaitFor(deepenGitRepoId).name(dockerBuilder).args(ImmutableList.of("build", "--tag=" + javaBuilder, "--file=./tools/build-images/java-cloud-builder/Dockerfile", ".")).env(ImmutableList.of("CI=true", "CI_MASTER=true")).build());
        steps.add(ImmutableCloudBuildStep.builder().id(buildAllImageId).addWaitFor(refreshBuildImageId).name(javaBuilder).entrypoint("./gradlew").args(ImmutableList.of("continuousBuild", "--stacktrace", "--no-daemon")).env(ImmutableList.of("CI=true", "CI_MASTER=true")).build());
        steps.addAll(serverSteps);
        ImmutableCloudBuild.Builder cloudBuildConfig = ImmutableCloudBuild.builder().addAllSteps(steps).addImages(javaBuilder);
        if (existingCloudBuild != null) {
            CloudBuild existingWithoutGenerated = ImmutableCloudBuild.builder().from(existingCloudBuild).steps(existingCloudBuild.steps().stream().filter(step -> !step.id().startsWith("curio-generated-"))::iterator).images(existingCloudBuild.images().stream().filter(image -> !image.equals(javaBuilder))::iterator).build();
            cloudBuildConfig.from(existingWithoutGenerated);
        }
        try {
            OBJECT_MAPPER.writeValue(rootProject.file("cloudbuild.yaml"), cloudBuildConfig.build());
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    });
}
Also used : Style(org.immutables.value.Value.Style) LoggerFactory(org.slf4j.LoggerFactory) Rule(org.gradle.api.Rule) ArrayList(java.util.ArrayList) Task(org.gradle.api.Task) ImmutableList(com.google.common.collect.ImmutableList) JsonSerialize(com.fasterxml.jackson.databind.annotation.JsonSerialize) Map(java.util.Map) CreateBuildCacheBucket(org.curioswitch.gradle.plugins.gcloud.tasks.CreateBuildCacheBucket) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) GcloudTask(org.curioswitch.gradle.plugins.gcloud.tasks.GcloudTask) RequestNamespaceCertTask(org.curioswitch.gradle.plugins.gcloud.tasks.RequestNamespaceCertTask) Nullable(javax.annotation.Nullable) DeploymentExtension(org.curioswitch.gradle.plugins.curioserver.DeploymentExtension) ExtraPropertiesExtension(org.gradle.api.plugins.ExtraPropertiesExtension) Logger(org.slf4j.Logger) Project(org.gradle.api.Project) SetupTask(org.curioswitch.gradle.plugins.gcloud.tasks.SetupTask) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DeploymentConfiguration(org.curioswitch.gradle.plugins.curioserver.DeploymentConfiguration) Include(com.fasterxml.jackson.annotation.JsonInclude.Include) IOException(java.io.IOException) CurioServerPlugin(org.curioswitch.gradle.plugins.curioserver.CurioServerPlugin) Immutable(org.immutables.value.Value.Immutable) File(java.io.File) UncheckedIOException(java.io.UncheckedIOException) GuavaModule(com.fasterxml.jackson.datatype.guava.GuavaModule) List(java.util.List) Paths(java.nio.file.Paths) ImplementationVisibility(org.immutables.value.Value.Style.ImplementationVisibility) BasePluginConvention(org.gradle.api.plugins.BasePluginConvention) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) Collections(java.util.Collections) BuilderVisibility(org.immutables.value.Value.Style.BuilderVisibility) Feature(com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature) Plugin(org.gradle.api.Plugin) Task(org.gradle.api.Task) GcloudTask(org.curioswitch.gradle.plugins.gcloud.tasks.GcloudTask) RequestNamespaceCertTask(org.curioswitch.gradle.plugins.gcloud.tasks.RequestNamespaceCertTask) SetupTask(org.curioswitch.gradle.plugins.gcloud.tasks.SetupTask) CurioServerPlugin(org.curioswitch.gradle.plugins.curioserver.CurioServerPlugin) DeploymentExtension(org.curioswitch.gradle.plugins.curioserver.DeploymentExtension) ArrayList(java.util.ArrayList) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) File(java.io.File) DeploymentConfiguration(org.curioswitch.gradle.plugins.curioserver.DeploymentConfiguration)

Aggregations

Include (com.fasterxml.jackson.annotation.JsonInclude.Include)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JsonDeserialize (com.fasterxml.jackson.databind.annotation.JsonDeserialize)1 JsonSerialize (com.fasterxml.jackson.databind.annotation.JsonSerialize)1 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)1 Feature (com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature)1 GuavaModule (com.fasterxml.jackson.datatype.guava.GuavaModule)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 File (java.io.File)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 Paths (java.nio.file.Paths)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Nullable (javax.annotation.Nullable)1 CurioServerPlugin (org.curioswitch.gradle.plugins.curioserver.CurioServerPlugin)1 DeploymentConfiguration (org.curioswitch.gradle.plugins.curioserver.DeploymentConfiguration)1