-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add destination prefix option to publish task #11
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,8 @@ public ExecutionResult execute(TaskConfig config, final TaskExecutionContext con | |
final String bucket = env.get(GO_ARTIFACTS_S3_BUCKET); | ||
final S3ArtifactStore store = new S3ArtifactStore(s3Client(env), bucket); | ||
|
||
final String destinationPrefix = getDestinationPrefix(config, env); | ||
|
||
try { | ||
List<Tuple2<String, String>> sourceDestinations = PublishTask.getSourceDestinations(config.getValue(SOURCEDESTINATIONS)); | ||
foreach(sourceDestinations, new VoidFunction<Tuple2<String, String>>() { | ||
|
@@ -59,7 +61,8 @@ public void execute(Tuple2<String, String> input) { | |
@Override | ||
public void execute(String includedFile) { | ||
File localFileToUpload = new File(String.format("%s/%s", context.workingDir(), includedFile)); | ||
pushToS3(context, env, store, localFileToUpload, destination); | ||
|
||
pushToS3(context, destinationPrefix, store, localFileToUpload, destination); | ||
} | ||
}); | ||
} | ||
|
@@ -69,7 +72,7 @@ public void execute(String includedFile) { | |
log.error(message); | ||
return ExecutionResult.failure(message, e); | ||
} | ||
setMetadata(env, bucket, store); | ||
setMetadata(env, bucket, destinationPrefix, store); | ||
|
||
return ExecutionResult.success("Published all artifacts to S3"); | ||
} | ||
|
@@ -94,10 +97,10 @@ public AmazonS3Client s3Client(GoEnvironment env) { | |
return new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey)); | ||
} | ||
|
||
private void pushToS3(final TaskExecutionContext context, final GoEnvironment env, final S3ArtifactStore store, File localFileToUpload, String destination) { | ||
String templateSoFar = env.artifactsLocationTemplate(); | ||
private void pushToS3(final TaskExecutionContext context, final String destinationPrefix, final S3ArtifactStore store, File localFileToUpload, String destination) { | ||
String templateSoFar = ensureKeySegmentValid(destinationPrefix); | ||
if(!org.apache.commons.lang3.StringUtils.isBlank(destination)) { | ||
templateSoFar += "/" + destination; | ||
templateSoFar += destination; | ||
} | ||
List<FilePathToTemplate> filesToUpload = generateFilesToUpload(templateSoFar, localFileToUpload); | ||
foreach(filesToUpload, new VoidFunction<FilePathToTemplate>() { | ||
|
@@ -123,7 +126,8 @@ private ObjectMetadata metadata(GoEnvironment env) { | |
} | ||
|
||
private List<FilePathToTemplate> generateFilesToUpload(final String templateSoFar, final File fileToUpload) { | ||
final String templateWithFolder = String.format("%s/%s", templateSoFar, fileToUpload.getName()); | ||
final String templateWithFolder = ensureKeySegmentValid(templateSoFar) + fileToUpload.getName(); // ensure it ends with a slash and add filename | ||
|
||
if (fileToUpload.isDirectory()) { | ||
return flatMap(fileToUpload.listFiles(), new Function<File, List<FilePathToTemplate>>() { | ||
@Override | ||
|
@@ -142,17 +146,45 @@ private ExecutionResult envNotFound(String environmentVariable) { | |
return ExecutionResult.failure(message); | ||
} | ||
|
||
private void setMetadata(GoEnvironment env, String bucket, S3ArtifactStore store) { | ||
private void setMetadata(GoEnvironment env, String bucket, String destinationPrefix, S3ArtifactStore store) { | ||
ObjectMetadata metadata = metadata(env); | ||
metadata.setContentLength(0); | ||
InputStream emptyContent = new ByteArrayInputStream(new byte[0]); | ||
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, | ||
env.artifactsLocationTemplate() + "/", | ||
ensureKeySegmentValid(destinationPrefix), | ||
emptyContent, | ||
metadata); | ||
|
||
store.put(putObjectRequest); | ||
} | ||
|
||
private String getDestinationPrefix(final TaskConfig config, final GoEnvironment env) { | ||
String destinationPrefix = config.getValue(DESTINATION_PREFIX); | ||
|
||
if(org.apache.commons.lang3.StringUtils.isBlank(destinationPrefix)) { | ||
return env.artifactsLocationTemplate(); | ||
} | ||
|
||
destinationPrefix = env.replaceVariables(destinationPrefix); | ||
|
||
if(destinationPrefix.endsWith("/")) { | ||
destinationPrefix = destinationPrefix.substring(0, destinationPrefix.length() - 1); | ||
} | ||
|
||
return destinationPrefix; | ||
} | ||
|
||
private String ensureKeySegmentValid(String segment) { | ||
if(org.apache.commons.lang3.StringUtils.isBlank(segment)) { | ||
return segment; | ||
} | ||
|
||
if(!org.apache.commons.lang3.StringUtils.endsWith(segment, "/")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please have them imported (even statically)? Or is there any other reason to use full namespace in here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I just used this as I saw it done this way elsewhere in the project. Most of my experience is with C#, I've only worked on a few small Java projects. As a result, I'm not too sure on the best practice in regards to static import vs import. I've just imported StringUtils (not statically) as that is how ArrayUtils is imported. |
||
segment += "/"; | ||
} | ||
|
||
return segment; | ||
} | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we could extract this,
if
check outside then we could choose to set / not set the metadata.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. I changed to to not set the metadata if a destinationPrefix is configured. I chose this instead of adding another setting as most using a destinationPrefix are deploying rather than storing artifacts.