Skip to content
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

Single-file upload option #115

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/main/java/hudson/plugins/s3/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public final class Entry implements Describable<Entry> {
*/
public boolean flatten;

/**
* Upload a single file (so the file-name is not appended to the destination bucket)
*/
public boolean singleFile;

/**
* use GZIP to compress files
*/
Expand All @@ -93,8 +98,8 @@ public final class Entry implements Describable<Entry> {
@DataBoundConstructor
public Entry(String bucket, String sourceFile, String excludedFile, String storageClass, String selectedRegion,
boolean noUploadOnFailure, boolean uploadFromSlave, boolean managedArtifacts,
boolean useServerSideEncryption, boolean flatten, boolean gzipFiles, boolean keepForever,
boolean showDirectlyInBrowser, List<MetadataPair> userMetadata) {
boolean useServerSideEncryption, boolean flatten, boolean singleFile, boolean gzipFiles,
boolean keepForever, boolean showDirectlyInBrowser, List<MetadataPair> userMetadata) {
this.bucket = bucket;
this.sourceFile = sourceFile;
this.excludedFile = excludedFile;
Expand All @@ -105,6 +110,7 @@ public Entry(String bucket, String sourceFile, String excludedFile, String stora
this.managedArtifacts = managedArtifacts;
this.useServerSideEncryption = useServerSideEncryption;
this.flatten = flatten;
this.singleFile = singleFile;
this.gzipFiles = gzipFiles;
this.keepForever = keepForever;
this.userMetadata = userMetadata;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/s3/S3BucketPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath ws, @Nonnull Launc
final int workspacePath = FileHelper.getSearchPathLength(ws.getRemote(),
startPath.trim(),
getProfile().isKeepStructure());
filenames.add(getFilename(path, entry.flatten, workspacePath));
filenames.add(entry.singleFile ? null : getFilename(path, entry.flatten, workspacePath));
log(console, "bucket=" + bucket + ", file=" + path.getName() + " region=" + selRegion + ", will be uploaded from slave=" + entry.uploadFromSlave + " managed=" + entry.managedArtifacts + " , server encryption " + entry.useServerSideEncryption);
}
}
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/hudson/plugins/s3/S3Profile.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hudson.plugins.s3;

import com.google.common.base.Joiner;
import hudson.FilePath;

import java.io.IOException;
Expand Down Expand Up @@ -116,7 +117,7 @@ public AmazonS3Client getClient(String region) {
}

public List<FingerprintRecord> upload(Run<?, ?> run,
final String bucketName,
final String mainBucketName,
final List<FilePath> filePaths,
final List<String> fileNames,
final Map<String, String> userMetadata,
Expand All @@ -131,7 +132,21 @@ public List<FingerprintRecord> upload(Run<?, ?> run,
try {
for (int i = 0; i < fileNames.size(); i++) {
final FilePath filePath = filePaths.get(i);
final String fileName = fileNames.get(i);
final String bucketName;
final String fileName;
if (fileNames.get(i) == null) {
final String[] bucketNameArray = mainBucketName.split("/");
if (bucketNameArray.length > 1) {
fileName = bucketNameArray[bucketNameArray.length - 1];
bucketNameArray[bucketNameArray.length - 1] = null;
bucketName = Joiner.on('/').skipNulls().join(bucketNameArray);
} else {
throw new IllegalArgumentException("Destination bucket must contains file name when Single file is enabled");
}
} else {
bucketName = mainBucketName;
fileName = fileNames.get(i);
}

final Destination dest;
final boolean produced;
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/hudson/plugins/s3/Entry/config.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
<f:entry field="flatten" title="Flatten directories">
<f:checkbox />
</f:entry>
<f:entry field="singleFile" title="Single file">
<f:checkbox />
</f:entry>
<f:entry field="gzipFiles" title="GZIP files">
<f:checkbox />
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
When enabled, Jenkins will only upload the first file matching the selector.
The filename is ignored and not appended to the 'Destination bucket'; so the Destination bucket will be used as
final key for the uploaded file.
</div>
2 changes: 1 addition & 1 deletion src/test/java/hudson/plugins/s3/S3Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void multiplePublishersUseExistingActions() throws Exception {
}

private Entry entryForFile(String fileName) {
return new Entry("bucket", fileName, "", "", "", false, false, true, false, false, false, false, false, null);
return new Entry("bucket", fileName, "", "", "", false, false, true, false, false, false, false, false, false, null);
}

private Builder stepCreatingFile(String fileName) {
Expand Down