Skip to content

Commit c8f39d2

Browse files
committed
Merge pull request #4 from awslabs/master
Merging pull requests from the parent repo
2 parents 9431d9a + 40d7b24 commit c8f39d2

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

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

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import net.sf.json.JSONObject;
5151

5252
import org.apache.commons.lang.StringUtils;
53+
import org.apache.commons.io.FileUtils;
5354
import org.kohsuke.stapler.DataBoundConstructor;
5455
import org.kohsuke.stapler.QueryParameter;
5556
import 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
}

src/main/resources/com/amazonaws/codedeploy/AWSCodeDeployPublisher/config.jelly

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
<f:textbox default="" />
3434
</f:entry>
3535

36+
<f:entry title="Appspec.yml per Deployment Group" field="deploymentGroupAppspec">
37+
<f:checkbox field="deploymentGroupAppspec" checked="${instance.getDeploymentGroupAppspec}"/>
38+
</f:entry>
39+
3640
<f:optionalBlock title="Wait for deployment to finish?" field="waitForCompletion" checked="${instance.getWaitForCompletion()}" inline="true">
3741
<f:entry title="Polling Timeout (s)" field="pollingTimeoutSec">
3842
<f:textbox default="300"/>

src/main/resources/com/amazonaws/codedeploy/AWSCodeDeployPublisher/help-applicationName.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
The name of the AWS CodeDeploy application you wish to deploy to. This plugin assumes that you've
33
already created the application and deployment group. If you haven't already, work through the <a
44
href="http://alpha-docs-aws.amazon.com/codedeploy/latest/userguide/how-to-create-application.html">How
5-
to create an Application with AWS CodeDeploy</a> documentation.
5+
to create an Application with AWS CodeDeploy</a> documentation. You can use environment variables
6+
in this field.
67
</div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div>
2+
<p>If checked, the build will use a dedicated appspec.yml file per deployment group.</p>
3+
<p>The appspec file should be named "appspec.DEPLOYMENT_GROUP_NAME.yml" and must be present in the jenkins project workspace.</p><br><br>
4+
<p>e.g.: appsec.staging.yml</p>
5+
</div>

0 commit comments

Comments
 (0)