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);
}
});
}
Aggregations