From 0a740bff61c3c590d3eb86758ff0908c6a66e313 Mon Sep 17 00:00:00 2001 From: Pierre Boutet Date: Thu, 4 Aug 2016 17:56:26 +0200 Subject: [PATCH 1/2] enable to chose file type : .zip or .tar or .tar.gz --- .../codedeploy/AWSCodeDeployPublisher.java | 67 ++++++++++++++----- .../AWSCodeDeployPublisher/config.jelly | 8 +++ 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/amazonaws/codedeploy/AWSCodeDeployPublisher.java b/src/main/java/com/amazonaws/codedeploy/AWSCodeDeployPublisher.java index 3cccd9d..8fc3bee 100644 --- a/src/main/java/com/amazonaws/codedeploy/AWSCodeDeployPublisher.java +++ b/src/main/java/com/amazonaws/codedeploy/AWSCodeDeployPublisher.java @@ -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; @@ -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; @@ -125,7 +127,8 @@ public AWSCodeDeployPublisher( String proxyHost, int proxyPort, String excludes, - String subdirectory) { + String subdirectory, + String fileType) { this.externalId = externalId; this.applicationName = applicationName; @@ -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; @@ -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); @@ -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")) { + 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 { + extension = ".zip"; + bundleType = BundleType.Zip; + } + + File tarzipFile = File.createTempFile(projectName + "-", extension); String key; File appspec; File dest; @@ -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")) { + 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(); @@ -338,7 +373,7 @@ private RevisionLocation zipAndUpload(AWSClients aws, String projectName, FilePa return revisionLocation; } finally { - zipFile.delete(); + tarzipFile.delete(); } } diff --git a/src/main/resources/com/amazonaws/codedeploy/AWSCodeDeployPublisher/config.jelly b/src/main/resources/com/amazonaws/codedeploy/AWSCodeDeployPublisher/config.jelly index ace3153..a2fc097 100644 --- a/src/main/resources/com/amazonaws/codedeploy/AWSCodeDeployPublisher/config.jelly +++ b/src/main/resources/com/amazonaws/codedeploy/AWSCodeDeployPublisher/config.jelly @@ -32,6 +32,14 @@ + + + + From f6db2228dee74fb6abb7f0f4a11a1ec7f4705a60 Mon Sep 17 00:00:00 2001 From: Pierre Boutet Date: Thu, 4 Aug 2016 18:37:12 +0200 Subject: [PATCH 2/2] correct default value --- .../amazonaws/codedeploy/AWSCodeDeployPublisher/config.jelly | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/com/amazonaws/codedeploy/AWSCodeDeployPublisher/config.jelly b/src/main/resources/com/amazonaws/codedeploy/AWSCodeDeployPublisher/config.jelly index a2fc097..95ad926 100644 --- a/src/main/resources/com/amazonaws/codedeploy/AWSCodeDeployPublisher/config.jelly +++ b/src/main/resources/com/amazonaws/codedeploy/AWSCodeDeployPublisher/config.jelly @@ -33,7 +33,7 @@ -