@@ -107,8 +107,6 @@ public class AWSCodeDeployPublisher extends Publisher implements SimpleBuildStep
107107 private final String deploymentMethod ;
108108 private final String versionFileName ;
109109
110- private PrintStream logger ;
111- private Map <String , String > envVars ;
112110 // Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
113111 @ DataBoundConstructor
114112 public AWSCodeDeployPublisher (
@@ -184,8 +182,8 @@ public AWSCodeDeployPublisher(
184182
185183 @ Override
186184 public void perform (@ Nonnull Run <?,?> build , @ Nonnull FilePath workspace , @ Nonnull Launcher launcher , @ Nonnull TaskListener listener ) throws IOException , InterruptedException {
187- this . logger = listener .getLogger ();
188- envVars = build .getEnvironment (listener );
185+ final PrintStream logger = listener .getLogger ();
186+ final Map < String , String > envVars = build .getEnvironment (listener );
189187 final boolean buildFailed = build .getResult () == Result .FAILURE ;
190188 if (buildFailed ) {
191189 logger .println ("Skipping CodeDeploy publisher as build failed" );
@@ -220,39 +218,39 @@ public void perform(@Nonnull Run<?,?> build, @Nonnull FilePath workspace, @Nonnu
220218
221219 try {
222220
223- verifyCodeDeployApplication (aws );
221+ verifyCodeDeployApplication (aws , envVars );
224222
225223 final String projectName = build .getDisplayName ();
226224 if (workspace == null ) {
227225 throw new IllegalArgumentException ("No workspace present for the build." );
228226 }
229- final FilePath sourceDirectory = getSourceDirectory (workspace );
230- final RevisionLocation revisionLocation = zipAndUpload (aws , projectName , sourceDirectory );
227+ final FilePath sourceDirectory = getSourceDirectory (workspace , envVars );
228+ final RevisionLocation revisionLocation = zipAndUpload (aws , projectName , sourceDirectory , logger , envVars );
231229
232- registerRevision (aws , revisionLocation );
230+ registerRevision (aws , revisionLocation , logger , envVars );
233231 if ("onlyRevision" .equals (deploymentMethod )){
234232 success = true ;
235233 } else {
236234
237- String deploymentId = createDeployment (aws , revisionLocation );
235+ String deploymentId = createDeployment (aws , revisionLocation , logger , envVars );
238236
239- success = waitForDeployment (aws , deploymentId );
237+ success = waitForDeployment (aws , deploymentId , logger );
240238 }
241239
242240 } catch (Exception e ) {
243241
244- this . logger .println ("Failed CodeDeploy post-build step; exception follows." );
245- this . logger .println (e .getMessage ());
246- e .printStackTrace (this . logger );
242+ logger .println ("Failed CodeDeploy post-build step; exception follows." );
243+ logger .println (e .getMessage ());
244+ e .printStackTrace (logger );
247245 }
248246
249247 if (!success ) {
250248 throw new AbortException ();
251249 }
252250 }
253251
254- private FilePath getSourceDirectory (FilePath basePath ) throws IOException , InterruptedException {
255- String subdirectory = StringUtils .trimToEmpty (getSubdirectoryFromEnv ());
252+ private FilePath getSourceDirectory (FilePath basePath , Map < String , String > envVars ) throws IOException , InterruptedException {
253+ String subdirectory = StringUtils .trimToEmpty (getSubdirectoryFromEnv (envVars ));
256254 if (!subdirectory .isEmpty () && !subdirectory .startsWith ("/" )) {
257255 subdirectory = "/" + subdirectory ;
258256 }
@@ -275,11 +273,11 @@ private boolean isSubDirectory(FilePath parent, FilePath child) {
275273 return false ;
276274 }
277275
278- private void verifyCodeDeployApplication (AWSClients aws ) throws IllegalArgumentException {
276+ private void verifyCodeDeployApplication (AWSClients aws , Map < String , String > envVars ) throws IllegalArgumentException {
279277 // Check that the application exists
280278 ListApplicationsResult applications = aws .codedeploy .listApplications ();
281- String applicationName = getApplicationNameFromEnv ();
282- String deploymentGroupName = getDeploymentGroupNameFromEnv ();
279+ String applicationName = getApplicationNameFromEnv (envVars );
280+ String deploymentGroupName = getDeploymentGroupNameFromEnv (envVars );
283281
284282 if (!applications .getApplications ().contains (applicationName )) {
285283 throw new IllegalArgumentException ("Cannot find application named '" + applicationName + "'" );
@@ -296,7 +294,7 @@ private void verifyCodeDeployApplication(AWSClients aws) throws IllegalArgumentE
296294 }
297295 }
298296
299- private RevisionLocation zipAndUpload (AWSClients aws , String projectName , FilePath sourceDirectory ) throws IOException , InterruptedException , IllegalArgumentException {
297+ private RevisionLocation zipAndUpload (AWSClients aws , String projectName , FilePath sourceDirectory , PrintStream logger , Map < String , String > envVars ) throws IOException , InterruptedException , IllegalArgumentException {
300298
301299 File zipFile = null ;
302300 File versionFile ;
@@ -329,9 +327,9 @@ private RevisionLocation zipAndUpload(AWSClients aws, String projectName, FilePa
329327 String key ;
330328 File appspec ;
331329 File dest ;
332- String deploymentGroupName = getDeploymentGroupNameFromEnv ();
333- String prefix = getS3PrefixFromEnv ();
334- String bucket = getS3BucketFromEnv ();
330+ String deploymentGroupName = getDeploymentGroupNameFromEnv (envVars );
331+ String prefix = getS3PrefixFromEnv (envVars );
332+ String bucket = getS3BucketFromEnv (envVars );
335333
336334 if (bucket .indexOf ("/" ) > 0 ){
337335 throw new IllegalArgumentException ("S3 Bucket field cannot contain any subdirectories. Bucket name only!" );
@@ -395,10 +393,10 @@ private RevisionLocation zipAndUpload(AWSClients aws, String projectName, FilePa
395393 }
396394 }
397395
398- private void registerRevision (AWSClients aws , RevisionLocation revisionLocation ) {
396+ private void registerRevision (AWSClients aws , RevisionLocation revisionLocation , PrintStream logger , Map < String , String > envVars ) {
399397
400- String applicationName = getApplicationNameFromEnv ();
401- this . logger .println ("Registering revision for application '" + applicationName + "'" );
398+ String applicationName = getApplicationNameFromEnv (envVars );
399+ logger .println ("Registering revision for application '" + applicationName + "'" );
402400
403401 aws .codedeploy .registerApplicationRevision (
404402 new RegisterApplicationRevisionRequest ()
@@ -408,23 +406,23 @@ private void registerRevision(AWSClients aws, RevisionLocation revisionLocation)
408406 );
409407 }
410408
411- private String createDeployment (AWSClients aws , RevisionLocation revisionLocation ) throws Exception {
409+ private String createDeployment (AWSClients aws , RevisionLocation revisionLocation , PrintStream logger , Map < String , String > envVars ) throws Exception {
412410
413- this . logger .println ("Creating deployment with revision at " + revisionLocation );
411+ logger .println ("Creating deployment with revision at " + revisionLocation );
414412
415413 CreateDeploymentResult createDeploymentResult = aws .codedeploy .createDeployment (
416414 new CreateDeploymentRequest ()
417- .withDeploymentConfigName (getDeploymentConfigFromEnv ())
418- .withDeploymentGroupName (getDeploymentGroupNameFromEnv ())
419- .withApplicationName (getApplicationNameFromEnv ())
415+ .withDeploymentConfigName (getDeploymentConfigFromEnv (envVars ))
416+ .withDeploymentGroupName (getDeploymentGroupNameFromEnv (envVars ))
417+ .withApplicationName (getApplicationNameFromEnv (envVars ))
420418 .withRevision (revisionLocation )
421419 .withDescription ("Deployment created by Jenkins" )
422420 );
423421
424422 return createDeploymentResult .getDeploymentId ();
425423 }
426424
427- private boolean waitForDeployment (AWSClients aws , String deploymentId ) throws InterruptedException {
425+ private boolean waitForDeployment (AWSClients aws , String deploymentId , PrintStream logger ) throws InterruptedException {
428426
429427 if (!this .waitForCompletion ) {
430428 return true ;
@@ -460,7 +458,7 @@ private boolean waitForDeployment(AWSClients aws, String deploymentId) throws In
460458 Date now = new Date ();
461459
462460 if (now .getTime () - startTimeMillis >= pollingTimeoutMillis ) {
463- this . logger .println ("Exceeded maximum polling time of " + pollingTimeoutMillis + " milliseconds." );
461+ logger .println ("Exceeded maximum polling time of " + pollingTimeoutMillis + " milliseconds." );
464462 success = false ;
465463 break ;
466464 }
@@ -471,7 +469,7 @@ private boolean waitForDeployment(AWSClients aws, String deploymentId) throws In
471469 logger .println ("Deployment status: " + deployStatus .getStatus () + "; instances: " + deployStatus .getDeploymentOverview ());
472470
473471 if (!deployStatus .getStatus ().equals (DeploymentStatus .Succeeded .toString ())) {
474- this . logger .println ("Deployment did not succeed. Final status: " + deployStatus .getStatus ());
472+ logger .println ("Deployment did not succeed. Final status: " + deployStatus .getStatus ());
475473 success = false ;
476474 }
477475
@@ -726,27 +724,27 @@ public int getProxyPort() {
726724 return proxyPort ;
727725 }
728726
729- public String getApplicationNameFromEnv () {
727+ public String getApplicationNameFromEnv (final Map < String , String > envVars ) {
730728 return Util .replaceMacro (this .applicationName , envVars );
731729 }
732730
733- public String getDeploymentGroupNameFromEnv () {
731+ public String getDeploymentGroupNameFromEnv (final Map < String , String > envVars ) {
734732 return Util .replaceMacro (this .deploymentGroupName , envVars );
735733 }
736734
737- public String getDeploymentConfigFromEnv () {
735+ public String getDeploymentConfigFromEnv (final Map < String , String > envVars ) {
738736 return Util .replaceMacro (this .deploymentConfig , envVars );
739737 }
740738
741- public String getS3BucketFromEnv () {
739+ public String getS3BucketFromEnv (final Map < String , String > envVars ) {
742740 return Util .replaceMacro (this .s3bucket , envVars );
743741 }
744742
745- public String getS3PrefixFromEnv () {
743+ public String getS3PrefixFromEnv (Map < String , String > envVars ) {
746744 return Util .replaceMacro (this .s3prefix , envVars );
747745 }
748746
749- public String getSubdirectoryFromEnv () {
747+ public String getSubdirectoryFromEnv (Map < String , String > envVars ) {
750748 return Util .replaceMacro (this .subdirectory , envVars );
751749 }
752750}
0 commit comments