5656import org .kohsuke .stapler .StaplerRequest ;
5757
5858import java .io .File ;
59+ import java .io .FileInputStream ;
5960import java .io .FileOutputStream ;
60- import java .io .FileReader ;
61+ import java .io .InputStreamReader ;
6162import java .io .IOException ;
6263import java .io .PrintStream ;
6364import java .util .Date ;
@@ -78,7 +79,7 @@ public class AWSCodeDeployPublisher extends Publisher {
7879 public static final long DEFAULT_TIMEOUT_SECONDS = 900 ;
7980 public static final long DEFAULT_POLLING_FREQUENCY_SECONDS = 15 ;
8081 public static final String ROLE_SESSION_NAME = "jenkins-codedeploy-plugin" ;
81- public static final Regions [] AVAILABLE_REGIONS = {Regions .AP_NORTHEAST_1 , Regions .AP_SOUTHEAST_1 , Regions .AP_SOUTHEAST_2 , Regions .EU_WEST_1 , Regions .US_EAST_1 , Regions .US_WEST_2 , Regions .EU_CENTRAL_1 , Regions .US_WEST_1 , Regions .SA_EAST_1 , Regions .AP_NORTHEAST_2 , Regions .AP_SOUTH_1 };
82+ private static final Regions [] AVAILABLE_REGIONS = {Regions .AP_NORTHEAST_1 , Regions .AP_SOUTHEAST_1 , Regions .AP_SOUTHEAST_2 , Regions .EU_WEST_1 , Regions .US_EAST_1 , Regions .US_WEST_2 , Regions .EU_CENTRAL_1 , Regions .US_WEST_1 , Regions .SA_EAST_1 , Regions .AP_NORTHEAST_2 , Regions .AP_SOUTH_1 };
8283
8384 private final String s3bucket ;
8485 private final String s3prefix ;
@@ -190,7 +191,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
190191 return true ;
191192 }
192193
193- AWSClients aws ;
194+ final AWSClients aws ;
194195 if ("awsAccessKey" .equals (credentials )) {
195196 if (StringUtils .isEmpty (this .awsAccessKey ) && StringUtils .isEmpty (this .awsSecretKey )) {
196197 aws = AWSClients .fromDefaultCredentialChain (
@@ -220,8 +221,13 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
220221
221222 verifyCodeDeployApplication (aws );
222223
223- String projectName = build .getProject ().getName ();
224- RevisionLocation revisionLocation = zipAndUpload (aws , projectName , getSourceDirectory (build .getWorkspace ()));
224+ final String projectName = build .getProject ().getName ();
225+ final FilePath workspace = build .getWorkspace ();
226+ if (workspace == null ) {
227+ throw new IllegalArgumentException ("No workspace present for the build." );
228+ }
229+ final FilePath sourceDirectory = getSourceDirectory (workspace );
230+ final RevisionLocation revisionLocation = zipAndUpload (aws , projectName , sourceDirectory );
225231
226232 registerRevision (aws , revisionLocation );
227233 if ("onlyRevision" .equals (deploymentMethod )){
@@ -296,10 +302,10 @@ private RevisionLocation zipAndUpload(AWSClients aws, String projectName, FilePa
296302 File versionFile ;
297303 versionFile = new File (sourceDirectory + "/" + versionFileName );
298304
299- FileReader reader = null ;
305+ InputStreamReader reader = null ;
300306 String version = null ;
301307 try {
302- reader = new FileReader ( versionFile );
308+ reader = new InputStreamReader ( new FileInputStream ( versionFile ), "UTF-8" );
303309 char [] chars = new char [(int ) versionFile .length () -1 ];
304310 reader .read (chars );
305311 version = new String (chars );
@@ -312,7 +318,10 @@ private RevisionLocation zipAndUpload(AWSClients aws, String projectName, FilePa
312318
313319 if (version != null ){
314320 zipFile = new File ("/tmp/" + projectName + "-" + version + ".zip" );
315- zipFile .createNewFile ();
321+ final boolean fileCreated = zipFile .createNewFile ();
322+ if (!fileCreated ) {
323+ logger .println ("File already exists, overwriting: " + zipFile .getPath ());
324+ }
316325 } else {
317326 zipFile = File .createTempFile (projectName + "-" , ".zip" );
318327 }
@@ -344,12 +353,15 @@ private RevisionLocation zipAndUpload(AWSClients aws, String projectName, FilePa
344353
345354 logger .println ("Zipping files into " + zipFile .getAbsolutePath ());
346355
347-
348-
349- sourceDirectory .zip (
350- new FileOutputStream (zipFile ),
351- new DirScanner .Glob (this .includes , this .excludes )
352- );
356+ FileOutputStream outputStream = new FileOutputStream (zipFile );
357+ try {
358+ sourceDirectory .zip (
359+ outputStream ,
360+ new DirScanner .Glob (this .includes , this .excludes )
361+ );
362+ } finally {
363+ outputStream .close ();
364+ }
353365
354366 if (prefix .isEmpty ()) {
355367 key = zipFile .getName ();
@@ -376,7 +388,10 @@ private RevisionLocation zipAndUpload(AWSClients aws, String projectName, FilePa
376388
377389 return revisionLocation ;
378390 } finally {
379- zipFile .delete ();
391+ final boolean deleted = zipFile .delete ();
392+ if (!deleted ) {
393+ logger .println ("Failed to clean up file " + zipFile .getPath ());
394+ }
380395 }
381396 }
382397
@@ -531,7 +546,7 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc
531546 awsAccessKey = formData .getString ("awsAccessKey" );
532547 awsSecretKey = formData .getString ("awsSecretKey" );
533548 proxyHost = formData .getString ("proxyHost" );
534- proxyPort = Integer .valueOf (formData .getString ("proxyPort" ));
549+ proxyPort = Integer .parseInt (formData .getString ("proxyPort" ));
535550
536551 req .bindJSON (this , formData );
537552 save ();
0 commit comments