Skip to content

Commit 3ac9346

Browse files
committed
Merge pull request #6 from awslabs/master
Merging in latest changes from awslabs repo.
2 parents 9f783e3 + f78bf7f commit 3ac9346

File tree

1 file changed

+56
-34
lines changed

1 file changed

+56
-34
lines changed

src/main/java/com/amazonaws/codedeploy/AWSCodeDeployPublisher.java

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ public class AWSCodeDeployPublisher extends Publisher {
7777
public static final long DEFAULT_TIMEOUT_SECONDS = 900;
7878
public static final long DEFAULT_POLLING_FREQUENCY_SECONDS = 15;
7979
public static final String ROLE_SESSION_NAME = "jenkins-codedeploy-plugin";
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};
80+
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};
8181

8282
private final String s3bucket;
8383
private final String s3prefix;
8484
private final String applicationName;
85-
private String deploymentGroupName; // TODO allow for deployment to multiple groups
85+
private final String deploymentGroupName; // TODO allow for deployment to multiple groups
8686
private final String deploymentConfig;
8787
private final Long pollingTimeoutSec;
8888
private final Long pollingFreqSec;
@@ -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,13 +212,10 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
213212

214213
try {
215214

216-
Map<String, String> envVars = build.getEnvironment(listener);
217-
this.deploymentGroupName = Util.replaceMacro(this.deploymentGroupName, envVars);
218-
219215
verifyCodeDeployApplication(aws);
220216

221217
String projectName = build.getProject().getName();
222-
RevisionLocation revisionLocation = zipAndUpload(aws, projectName, getSourceDirectory(build.getWorkspace()), envVars);
218+
RevisionLocation revisionLocation = zipAndUpload(aws, projectName, getSourceDirectory(build.getWorkspace()));
223219

224220
registerRevision(aws, revisionLocation);
225221
String deploymentId = createDeployment(aws, revisionLocation);
@@ -265,38 +261,44 @@ private boolean isSubDirectory(FilePath parent, FilePath child) {
265261
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

279-
if (!deploymentGroups.getDeploymentGroups().contains(this.deploymentGroupName)) {
280-
throw new IllegalArgumentException("Cannot find deployment group named '" + this.deploymentGroupName + "'");
277+
if (!deploymentGroups.getDeploymentGroups().contains(deploymentGroupName)) {
278+
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) 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) {
292-
appspec = new File(sourceDirectory + "/appspec." + this.deploymentGroupName + ".yml");
294+
appspec = new File(sourceDirectory + "/appspec." + deploymentGroupName + ".yml");
293295
if (appspec.exists()) {
294296
dest = new File(sourceDirectory + "/appspec.yml");
295297
FileUtils.copyFile(appspec, dest);
296-
logger.println("Use appspec." + this.deploymentGroupName + ".yml");
298+
logger.println("Use appspec." + deploymentGroupName + ".yml");
297299
}
298300
if (!appspec.exists()) {
299-
throw new IllegalArgumentException("/appspec." + this.deploymentGroupName + ".yml file does not exist" );
301+
throw new IllegalArgumentException("/appspec." + deploymentGroupName + ".yml file does not exist" );
300302
}
301303

302304
}
@@ -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,11 +344,12 @@ 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
);
@@ -358,9 +361,9 @@ private String createDeployment(AWSClients aws, RevisionLocation revisionLocatio
358361

359362
CreateDeploymentResult createDeploymentResult = aws.codedeploy.createDeployment(
360363
new CreateDeploymentRequest()
361-
.withDeploymentConfigName(this.deploymentConfig)
362-
.withDeploymentGroupName(this.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

Comments
 (0)