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

enable to chose file type : .zip or .tar or .tar.gz #8

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
67 changes: 51 additions & 16 deletions src/main/java/com/amazonaws/codedeploy/AWSCodeDeployPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.amazonaws.services.codedeploy.model.S3Location;

import hudson.FilePath;
import hudson.FilePath.TarCompression;
import hudson.Launcher;
import hudson.Extension;
import hudson.Util;
Expand Down Expand Up @@ -96,6 +97,7 @@ public class AWSCodeDeployPublisher extends Publisher {
private final String subdirectory;
private final String proxyHost;
private final int proxyPort;
private final String fileType;

private final String awsAccessKey;
private final String awsSecretKey;
Expand Down Expand Up @@ -125,7 +127,8 @@ public AWSCodeDeployPublisher(
String proxyHost,
int proxyPort,
String excludes,
String subdirectory) {
String subdirectory,
String fileType) {

this.externalId = externalId;
this.applicationName = applicationName;
Expand All @@ -141,6 +144,7 @@ public AWSCodeDeployPublisher(
this.subdirectory = subdirectory;
this.proxyHost = proxyHost;
this.proxyPort = proxyPort;
this.fileType = fileType;
this.credentials = credentials;
this.awsAccessKey = awsAccessKey;
this.awsSecretKey = awsSecretKey;
Expand Down Expand Up @@ -215,7 +219,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
verifyCodeDeployApplication(aws);

String projectName = build.getProject().getName();
RevisionLocation revisionLocation = zipAndUpload(aws, projectName, getSourceDirectory(build.getWorkspace()));
RevisionLocation revisionLocation = compressAndUpload(aws, projectName, getSourceDirectory(build.getWorkspace()), this.fileType);

registerRevision(aws, revisionLocation);
String deploymentId = createDeployment(aws, revisionLocation);
Expand Down Expand Up @@ -279,9 +283,25 @@ private void verifyCodeDeployApplication(AWSClients aws) throws IllegalArgumentE
}
}

private RevisionLocation zipAndUpload(AWSClients aws, String projectName, FilePath sourceDirectory) throws IOException, InterruptedException, IllegalArgumentException {
private RevisionLocation compressAndUpload(AWSClients aws, String projectName, FilePath sourceDirectory, String fileType) throws IOException, InterruptedException, IllegalArgumentException {

File zipFile = File.createTempFile(projectName + "-", ".zip");
String extension;
BundleType bundleType;
if (fileType == null || fileType.equals("Tar")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be fileType.equals("Zip")

extension = ".zip";
bundleType = BundleType.Zip;
} else if (fileType.equals("Tar")) {
extension = ".tar";
bundleType = BundleType.Tar;
} else if (fileType.equals("Tgz")) {
extension = ".tar.gz";
bundleType = BundleType.Tgz;
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather we throw on an unknown file type since you're already handling the default null case (maybe update it to include empty string too)

extension = ".zip";
bundleType = BundleType.Zip;
}

File tarzipFile = File.createTempFile(projectName + "-", extension);
String key;
File appspec;
File dest;
Expand All @@ -303,33 +323,48 @@ private RevisionLocation zipAndUpload(AWSClients aws, String projectName, FilePa

}

logger.println("Zipping files into " + zipFile.getAbsolutePath());

logger.println("package files into " + tarzipFile.getAbsolutePath());


sourceDirectory.zip(
new FileOutputStream(zipFile),
if (fileType == null || fileType.equals("Zip")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might change this to switch based on bundleType since you set it earlier into 1 of the 3 categories (avoids having to have the fallback / null check here)

sourceDirectory.zip(
new FileOutputStream(tarzipFile),
new DirScanner.Glob(this.includes, this.excludes)
);
} else if (fileType.equals("Tar")) {
sourceDirectory.tar(
new FileOutputStream(tarzipFile),
new DirScanner.Glob(this.includes, this.excludes)
);
);
} else if (fileType.equals("Tgz")) {
sourceDirectory.tar(
TarCompression.GZIP.compress(new FileOutputStream(tarzipFile)),
new DirScanner.Glob(this.includes, this.excludes)
);
} else {
sourceDirectory.zip(
new FileOutputStream(tarzipFile),
new DirScanner.Glob(this.includes, this.excludes)
);
}

if (prefix.isEmpty()) {
key = zipFile.getName();
key = tarzipFile.getName();
} else {
key = Util.replaceMacro(prefix, envVars);
if (prefix.endsWith("/")) {
key += zipFile.getName();
key += tarzipFile.getName();
} else {
key += "/" + zipFile.getName();
key += "/" + tarzipFile.getName();
}
}
logger.println("Uploading zip to s3://" + bucket + "/" + key);
PutObjectResult s3result = aws.s3.putObject(bucket, key, zipFile);
PutObjectResult s3result = aws.s3.putObject(bucket, key, tarzipFile);


S3Location s3Location = new S3Location();
s3Location.setBucket(bucket);
s3Location.setKey(key);
s3Location.setBundleType(BundleType.Zip);
s3Location.setBundleType(bundleType);
s3Location.setETag(s3result.getETag());

RevisionLocation revisionLocation = new RevisionLocation();
Expand All @@ -338,7 +373,7 @@ private RevisionLocation zipAndUpload(AWSClients aws, String projectName, FilePa

return revisionLocation;
} finally {
zipFile.delete();
tarzipFile.delete();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
<f:entry title="Proxy Port" field="proxyPort">
<f:textbox default="" />
</f:entry>
<f:entry name="fileType" title="File type" field="fileType">
<select name="fileType">
<option value="Zip">zip</option>
<option value="Tar">tar</option>
<option value="Tgz">tar.gz</option>
</select>
</f:entry>


<f:entry title="Appspec.yml per Deployment Group" field="deploymentGroupAppspec">
<f:checkbox field="deploymentGroupAppspec" checked="${instance.getDeploymentGroupAppspec}"/>
Expand Down