@@ -102,7 +102,7 @@ public class AWSCodeDeployPublisher extends Publisher {
102102 private final String credentials ;
103103
104104 private PrintStream logger ;
105-
105+ private Map < String , String > envVars ;
106106 // Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
107107 @ DataBoundConstructor
108108 public AWSCodeDeployPublisher (
@@ -172,13 +172,12 @@ public AWSCodeDeployPublisher(
172172 } else {
173173 this .s3prefix = s3prefix ;
174174 }
175-
176175 }
177176
178177 @ Override
179178 public boolean perform (AbstractBuild build , Launcher launcher , BuildListener listener ) throws IOException , InterruptedException {
180-
181179 this .logger = listener .getLogger ();
180+ envVars = build .getEnvironment (listener );
182181 final boolean buildFailed = build .getResult () == Result .FAILURE ;
183182 if (buildFailed ) {
184183 logger .println ("Skipping CodeDeploy publisher as build failed" );
@@ -213,16 +212,13 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
213212
214213 try {
215214
216- Map <String , String > envVars = build .getEnvironment (listener );
217- String deploymentGroupName = Util .replaceMacro (this .deploymentGroupName , envVars );
218-
219- verifyCodeDeployApplication (aws , deploymentGroupName );
215+ verifyCodeDeployApplication (aws );
220216
221217 String projectName = build .getProject ().getName ();
222- RevisionLocation revisionLocation = zipAndUpload (aws , projectName , getSourceDirectory (build .getWorkspace ()), envVars , deploymentGroupName );
218+ RevisionLocation revisionLocation = zipAndUpload (aws , projectName , getSourceDirectory (build .getWorkspace ()));
223219
224220 registerRevision (aws , revisionLocation );
225- String deploymentId = createDeployment (aws , revisionLocation , deploymentGroupName );
221+ String deploymentId = createDeployment (aws , revisionLocation );
226222
227223 success = waitForDeployment (aws , deploymentId );
228224
@@ -262,31 +258,37 @@ private boolean isSubDirectory(FilePath parent, FilePath child) {
262258 return false ;
263259 }
264260
265- private void verifyCodeDeployApplication (AWSClients aws , String deploymentGroupName ) throws IllegalArgumentException {
261+ private void verifyCodeDeployApplication (AWSClients aws ) throws IllegalArgumentException {
266262 // Check that the application exists
267263 ListApplicationsResult applications = aws .codedeploy .listApplications ();
268-
269- if (!applications .getApplications ().contains (this .applicationName )) {
270- throw new IllegalArgumentException ("Cannot find application named '" + this .applicationName + "'" );
264+ String applicationName = getApplicationNameFromEnv ();
265+ String deploymentGroupName = getDeploymentGroupNameFromEnv ();
266+
267+ if (!applications .getApplications ().contains (applicationName )) {
268+ throw new IllegalArgumentException ("Cannot find application named '" + applicationName + "'" );
271269 }
272270
273271 // Check that the deployment group exists
274272 ListDeploymentGroupsResult deploymentGroups = aws .codedeploy .listDeploymentGroups (
275273 new ListDeploymentGroupsRequest ()
276- .withApplicationName (this . applicationName )
274+ .withApplicationName (applicationName )
277275 );
278276
279277 if (!deploymentGroups .getDeploymentGroups ().contains (deploymentGroupName )) {
280278 throw new IllegalArgumentException ("Cannot find deployment group named '" + deploymentGroupName + "'" );
281279 }
282280 }
283281
284- private RevisionLocation zipAndUpload (AWSClients aws , String projectName , FilePath sourceDirectory , Map < String , String > envVars , String deploymentGroupName ) throws IOException , InterruptedException , IllegalArgumentException {
282+ private RevisionLocation zipAndUpload (AWSClients aws , String projectName , FilePath sourceDirectory ) throws IOException , InterruptedException , IllegalArgumentException {
285283
286284 File zipFile = File .createTempFile (projectName + "-" , ".zip" );
287285 String key ;
288286 File appspec ;
289287 File dest ;
288+ String deploymentGroupName = getDeploymentGroupNameFromEnv ();
289+ String prefix = getS3PrefixFromEnv ();
290+ String bucket = getS3BucketFromEnv ();
291+
290292 try {
291293 if (this .deploymentGroupAppspec ) {
292294 appspec = new File (sourceDirectory + "/appspec." + deploymentGroupName + ".yml" );
@@ -310,22 +312,22 @@ private RevisionLocation zipAndUpload(AWSClients aws, String projectName, FilePa
310312 new DirScanner .Glob (this .includes , this .excludes )
311313 );
312314
313- if (this . s3prefix .isEmpty ()) {
315+ if (prefix .isEmpty ()) {
314316 key = zipFile .getName ();
315317 } else {
316- key = Util .replaceMacro (this . s3prefix , envVars );
317- if (this . s3prefix .endsWith ("/" )) {
318+ key = Util .replaceMacro (prefix , envVars );
319+ if (prefix .endsWith ("/" )) {
318320 key += zipFile .getName ();
319321 } else {
320322 key += "/" + zipFile .getName ();
321323 }
322324 }
323- logger .println ("Uploading zip to s3://" + this . s3bucket + "/" + key );
324- PutObjectResult s3result = aws .s3 .putObject (this . s3bucket , key , zipFile );
325+ logger .println ("Uploading zip to s3://" + bucket + "/" + key );
326+ PutObjectResult s3result = aws .s3 .putObject (bucket , key , zipFile );
325327
326328
327329 S3Location s3Location = new S3Location ();
328- s3Location .setBucket (this . s3bucket );
330+ s3Location .setBucket (bucket );
329331 s3Location .setKey (key );
330332 s3Location .setBundleType (BundleType .Zip );
331333 s3Location .setETag (s3result .getETag ());
@@ -342,25 +344,26 @@ private RevisionLocation zipAndUpload(AWSClients aws, String projectName, FilePa
342344
343345 private void registerRevision (AWSClients aws , RevisionLocation revisionLocation ) {
344346
345- this .logger .println ("Registering revision for application '" + this .applicationName + "'" );
347+ String applicationName = getApplicationNameFromEnv ();
348+ this .logger .println ("Registering revision for application '" + applicationName + "'" );
346349
347350 aws .codedeploy .registerApplicationRevision (
348351 new RegisterApplicationRevisionRequest ()
349- .withApplicationName (this . applicationName )
352+ .withApplicationName (applicationName )
350353 .withRevision (revisionLocation )
351354 .withDescription ("Application revision registered via Jenkins" )
352355 );
353356 }
354357
355- private String createDeployment (AWSClients aws , RevisionLocation revisionLocation , String deploymentGroupName ) throws Exception {
358+ private String createDeployment (AWSClients aws , RevisionLocation revisionLocation ) throws Exception {
356359
357360 this .logger .println ("Creating deployment with revision at " + revisionLocation );
358361
359362 CreateDeploymentResult createDeploymentResult = aws .codedeploy .createDeployment (
360363 new CreateDeploymentRequest ()
361- .withDeploymentConfigName (this . deploymentConfig )
362- .withDeploymentGroupName (deploymentGroupName )
363- .withApplicationName (this . applicationName )
364+ .withDeploymentConfigName (getDeploymentConfigFromEnv () )
365+ .withDeploymentGroupName (getDeploymentGroupNameFromEnv () )
366+ .withApplicationName (getApplicationNameFromEnv () )
364367 .withRevision (revisionLocation )
365368 .withDescription ("Deployment created by Jenkins" )
366369 );
@@ -591,6 +594,10 @@ public String getDeploymentGroupName() {
591594 return deploymentGroupName ;
592595 }
593596
597+ public String getDeploymentConfig () {
598+ return deploymentConfig ;
599+ }
600+
594601 public String getS3bucket () {
595602 return s3bucket ;
596603 }
@@ -619,10 +626,6 @@ public Long getPollingFreqSec() {
619626 return pollingFreqSec ;
620627 }
621628
622- public String getDeploymentConfig () {
623- return deploymentConfig ;
624- }
625-
626629 public String getExternalId () {
627630 return externalId ;
628631 }
@@ -663,5 +666,24 @@ public int getProxyPort() {
663666 return proxyPort ;
664667 }
665668
669+ public String getApplicationNameFromEnv () {
670+ return Util .replaceMacro (this .applicationName , envVars );
671+ }
672+
673+ public String getDeploymentGroupNameFromEnv () {
674+ return Util .replaceMacro (this .deploymentGroupName , envVars );
675+ }
676+
677+ public String getDeploymentConfigFromEnv () {
678+ return Util .replaceMacro (this .deploymentConfig , envVars );
679+ }
680+
681+ public String getS3BucketFromEnv () {
682+ return Util .replaceMacro (this .s3bucket , envVars );
683+ }
684+
685+ public String getS3PrefixFromEnv () {
686+ return Util .replaceMacro (this .s3prefix , envVars );
687+ }
666688}
667689
0 commit comments