5050import net .sf .json .JSONObject ;
5151
5252import org .apache .commons .lang .StringUtils ;
53+ import org .apache .commons .io .FileUtils ;
5354import org .kohsuke .stapler .DataBoundConstructor ;
5455import org .kohsuke .stapler .QueryParameter ;
5556import org .kohsuke .stapler .StaplerRequest ;
@@ -76,15 +77,16 @@ public class AWSCodeDeployPublisher extends Publisher {
7677 public static final long DEFAULT_TIMEOUT_SECONDS = 900 ;
7778 public static final long DEFAULT_POLLING_FREQUENCY_SECONDS = 15 ;
7879 public static final String ROLE_SESSION_NAME = "jenkins-codedeploy-plugin" ;
79- public static final Regions [] AVAILABLE_REGIONS = {Regions .AP_NORTHEAST_1 , Regions .AP_SOUTHEAST_2 , Regions .EU_WEST_1 , Regions .US_EAST_1 , Regions .US_WEST_2 };
80+ public static final Regions [] AVAILABLE_REGIONS = {Regions .AP_NORTHEAST_1 , Regions .AP_SOUTHEAST_2 , Regions .EU_WEST_1 , Regions .US_EAST_1 , Regions .US_WEST_2 , Regions . EU_CENTRAL_1 };
8081
8182 private final String s3bucket ;
8283 private final String s3prefix ;
8384 private final String applicationName ;
84- private final String deploymentGroupName ; // TODO allow for deployment to multiple groups
85+ private String deploymentGroupName ; // TODO allow for deployment to multiple groups
8586 private final String deploymentConfig ;
8687 private final Long pollingTimeoutSec ;
8788 private final Long pollingFreqSec ;
89+ private final boolean deploymentGroupAppspec ;
8890 private final boolean waitForCompletion ;
8991 private final String externalId ;
9092 private final String iamRoleArn ;
@@ -110,6 +112,7 @@ public AWSCodeDeployPublisher(
110112 String deploymentGroupName ,
111113 String deploymentConfig ,
112114 String region ,
115+ Boolean deploymentGroupAppspec ,
113116 Boolean waitForCompletion ,
114117 Long pollingTimeoutSec ,
115118 Long pollingFreqSec ,
@@ -142,6 +145,7 @@ public AWSCodeDeployPublisher(
142145 this .awsAccessKey = awsAccessKey ;
143146 this .awsSecretKey = awsSecretKey ;
144147 this .iamRoleArn = iamRoleArn ;
148+ this .deploymentGroupAppspec = deploymentGroupAppspec ;
145149
146150 if (waitForCompletion != null && waitForCompletion ) {
147151 this .waitForCompletion = waitForCompletion ;
@@ -209,10 +213,13 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
209213
210214 try {
211215
216+ Map <String , String > envVars = build .getEnvironment (listener );
217+ this .deploymentGroupName = Util .replaceMacro (this .deploymentGroupName , envVars );
218+
212219 verifyCodeDeployApplication (aws );
213220
214221 String projectName = build .getProject ().getName ();
215- RevisionLocation revisionLocation = zipAndUpload (aws , projectName , getSourceDirectory (build .getWorkspace ()), build . getEnvironment ( listener ) );
222+ RevisionLocation revisionLocation = zipAndUpload (aws , projectName , getSourceDirectory (build .getWorkspace ()), envVars );
216223
217224 registerRevision (aws , revisionLocation );
218225 String deploymentId = createDeployment (aws , revisionLocation );
@@ -274,13 +281,29 @@ private void verifyCodeDeployApplication(AWSClients aws) throws IllegalArgumentE
274281 }
275282 }
276283
277- private RevisionLocation zipAndUpload (AWSClients aws , String projectName , FilePath sourceDirectory , Map <String , String > envVars ) throws IOException , InterruptedException {
284+ private RevisionLocation zipAndUpload (AWSClients aws , String projectName , FilePath sourceDirectory , Map <String , String > envVars ) throws IOException , InterruptedException , IllegalArgumentException {
278285
279286 File zipFile = File .createTempFile (projectName + "-" , ".zip" );
280287 String key ;
281-
288+ File appspec ;
289+ File dest ;
282290 try {
283- this .logger .println ("Zipping files into " + zipFile .getAbsolutePath ());
291+ if (this .deploymentGroupAppspec ) {
292+ appspec = new File (sourceDirectory + "/appspec." + this .deploymentGroupName + ".yml" );
293+ if (appspec .exists ()) {
294+ dest = new File (sourceDirectory + "/appspec.yml" );
295+ FileUtils .copyFile (appspec , dest );
296+ logger .println ("Use appspec." + this .deploymentGroupName + ".yml" );
297+ }
298+ if (!appspec .exists ()) {
299+ throw new IllegalArgumentException ("/appspec." + this .deploymentGroupName + ".yml file does not exist" );
300+ }
301+
302+ }
303+
304+ logger .println ("Zipping files into " + zipFile .getAbsolutePath ());
305+
306+
284307
285308 sourceDirectory .zip (
286309 new FileOutputStream (zipFile ),
@@ -608,6 +631,10 @@ public boolean getWaitForCompletion() {
608631 return waitForCompletion ;
609632 }
610633
634+ public boolean getDeploymentGroupAppspec () {
635+ return deploymentGroupAppspec ;
636+ }
637+
611638 public String getCredentials () {
612639 return credentials ;
613640 }
0 commit comments