From f78879b43663cbe55573aceaf88ae05024f4065c Mon Sep 17 00:00:00 2001 From: Manoj Date: Thu, 13 Jul 2017 17:27:21 +0530 Subject: [PATCH 1/2] Update publish plugin to support multiple buckets --- .../java/com/indix/gocd/s3publish/Config.java | 4 ++- .../indix/gocd/s3publish/PublishExecutor.java | 14 ++++++-- .../com/indix/gocd/s3publish/PublishTask.java | 7 ++++ .../main/resources/views/task.template.html | 13 ++++++- .../gocd/s3publish/PublishExecutorTest.java | 35 +++++++++++++++++++ .../java/com/indix/gocd/utils/Constants.java | 1 + 6 files changed, 70 insertions(+), 4 deletions(-) diff --git a/publish/src/main/java/com/indix/gocd/s3publish/Config.java b/publish/src/main/java/com/indix/gocd/s3publish/Config.java index bb6fc3a..906ab4a 100644 --- a/publish/src/main/java/com/indix/gocd/s3publish/Config.java +++ b/publish/src/main/java/com/indix/gocd/s3publish/Config.java @@ -9,6 +9,7 @@ import java.util.List; import java.util.Map; +import static com.indix.gocd.utils.Constants.ARTIFACTS_BUCKET; import static com.indix.gocd.utils.Constants.DESTINATION_PREFIX; import static com.indix.gocd.utils.Constants.SOURCEDESTINATIONS; @@ -16,11 +17,12 @@ public class Config { public String sourceDestinationsJson; public String destinationPrefix; - + public String artifactsBucket; public Config(Map config) { sourceDestinationsJson = getValue(config, SOURCEDESTINATIONS); destinationPrefix = getValue(config, DESTINATION_PREFIX); + artifactsBucket = getValue(config, ARTIFACTS_BUCKET); } public List sourceDestinations() throws JsonSyntaxException { diff --git a/publish/src/main/java/com/indix/gocd/s3publish/PublishExecutor.java b/publish/src/main/java/com/indix/gocd/s3publish/PublishExecutor.java index fdb7655..e603428 100644 --- a/publish/src/main/java/com/indix/gocd/s3publish/PublishExecutor.java +++ b/publish/src/main/java/com/indix/gocd/s3publish/PublishExecutor.java @@ -33,10 +33,11 @@ public class PublishExecutor { public TaskExecutionResult execute(Config config, final Context context) { try { final GoEnvironment env = new GoEnvironment(context.getEnvironmentVariables()); - if (env.isAbsent(GO_ARTIFACTS_S3_BUCKET)) return envNotFound(GO_ARTIFACTS_S3_BUCKET); if (env.isAbsent(GO_SERVER_DASHBOARD_URL)) return envNotFound(GO_SERVER_DASHBOARD_URL); - final String bucket = env.get(GO_ARTIFACTS_S3_BUCKET); + String bucket = getBucket(env, config); + if(bucket == null) return envNotFound(GO_ARTIFACTS_S3_BUCKET); + final S3ArtifactStore store = getS3ArtifactStore(env, bucket); store.setStorageClass(env.getOrElse(AWS_STORAGE_CLASS, STORAGE_CLASS_STANDARD)); @@ -69,6 +70,15 @@ public TaskExecutionResult execute(Config config, final Context context) { } } + private String getBucket(GoEnvironment env, Config config) { + if(StringUtils.isNotBlank(config.artifactsBucket)) { + return config.artifactsBucket; + } else if(env.has(GO_ARTIFACTS_S3_BUCKET)) { + return env.get(GO_ARTIFACTS_S3_BUCKET); + } + return null; + } + protected S3ArtifactStore getS3ArtifactStore(GoEnvironment env, String bucket) { return new S3ArtifactStore(env, bucket); } diff --git a/publish/src/main/java/com/indix/gocd/s3publish/PublishTask.java b/publish/src/main/java/com/indix/gocd/s3publish/PublishTask.java index e475b76..d0e88ff 100644 --- a/publish/src/main/java/com/indix/gocd/s3publish/PublishTask.java +++ b/publish/src/main/java/com/indix/gocd/s3publish/PublishTask.java @@ -22,6 +22,7 @@ import java.util.List; import java.util.Map; +import static com.indix.gocd.utils.Constants.ARTIFACTS_BUCKET; import static com.indix.gocd.utils.Constants.DESTINATION_PREFIX; import static com.indix.gocd.utils.Constants.SOURCEDESTINATIONS; import static com.indix.gocd.utils.utils.Lists.foreach; @@ -110,6 +111,7 @@ public void execute(SourceDestination input) { private GoPluginApiResponse handleGetConfigRequest() { HashMap config = new HashMap(); + HashMap sourceDestinations = new HashMap(); sourceDestinations.put("default-value", ""); sourceDestinations.put("required", true); @@ -120,6 +122,11 @@ private GoPluginApiResponse handleGetConfigRequest() { destinationPrefix.put("required", false); config.put(DESTINATION_PREFIX, destinationPrefix); + HashMap artifactsBucket = new HashMap(); + artifactsBucket.put("default-value", ""); + artifactsBucket.put("required", false); + config.put(ARTIFACTS_BUCKET, artifactsBucket); + return createResponse(DefaultGoPluginApiResponse.SUCCESS_RESPONSE_CODE, config); } diff --git a/publish/src/main/resources/views/task.template.html b/publish/src/main/resources/views/task.template.html index bc551af..8428375 100644 --- a/publish/src/main/resources/views/task.template.html +++ b/publish/src/main/resources/views/task.template.html @@ -37,7 +37,18 @@

-

Make sure GO_ARTIFACTS_S3_BUCKET and GO_SERVER_DASHBOARD_URL environment variables are present with appropriate values on any of pipeline / Go Environments / on all agent machines.

+ + + {{ GOINPUTNAME[artifactsBucket].$error.server }} +
+
+

+ The artifacts will be pushed to the bucket {{artifactsBucket}} + GO_ARTIFACTS_S3_BUCKET env var must be set if the above is not configured. +

+
+
+

Make sure GO_SERVER_DASHBOARD_URL environment variable is present with appropriate value on any of pipeline / Go Environments / on all agent machines.