Skip to content
This repository was archived by the owner on Nov 30, 2021. It is now read-only.

Commit ac12738

Browse files
authored
Merge pull request #8 from danieljimenez/skipDuplicateVersions
First pass: Skip upload of Application Version if one already exists …
2 parents 9772cfb + 7422d29 commit ac12738

File tree

7 files changed

+55
-6
lines changed

7 files changed

+55
-6
lines changed

aws-elasticbeanstalk-agent/src/main/java/jetbrains/buildServer/runner/elasticbeanstalk/ElasticBeanstalkRunner.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ protected BuildFinishedStatus runImpl() throws RunBuildException {
5454
final String applicationName = runnerParameters.get(APP_NAME_PARAM);
5555
final String environmentName = runnerParameters.get(ENV_NAME_PARAM);
5656
final String versionLabel = runnerParameters.get(APP_VERSION_PARAM);
57+
final Boolean skipDuplicateVersions = Boolean.valueOf(runnerParameters.get(APP_VERSION_SKIP_DUPE_PARAM));
5758

5859
if (!m.problemOccurred && !isInterrupted()) {
59-
awsClient.createApplicationVersion(applicationName, versionLabel, s3BucketName, s3ObjectKey);
60+
awsClient.createApplicationVersion(applicationName, versionLabel, skipDuplicateVersions, s3BucketName, s3ObjectKey);
6061
}
6162

6263
if (!m.problemOccurred && !isInterrupted()) {

aws-elasticbeanstalk-agent/src/main/java/jetbrains/buildServer/runner/elasticbeanstalk/LoggingDeploymentListener.java

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ void createVersionStarted(@NotNull String applicationName, @NotNull String versi
5757
log(String.format("Creating application %s version %s with bucket %s and key %s", applicationName, versionLabel, s3BucketName, s3ObjectKey));
5858
}
5959

60+
@Override
61+
void createVersionSkipped(@NotNull String applicationName, @NotNull String versionLabel) {
62+
log(String.format("Application %s version %s already exists, skipping upload...", applicationName, versionLabel));
63+
close(CREATE_VERSION);
64+
}
65+
6066
@Override
6167
void createVersionFinished(@NotNull String applicationName, @NotNull String versionLabel,
6268
@NotNull String s3BucketName, @NotNull String s3ObjectKey) {
@@ -144,6 +150,7 @@ private Collection<String> getIdentityFormingParameters() {
144150
myRunnerParameters.get(ElasticBeanstalkConstants.S3_BUCKET_NAME_PARAM),
145151
myRunnerParameters.get(ElasticBeanstalkConstants.ENV_NAME_PARAM),
146152
myRunnerParameters.get(ElasticBeanstalkConstants.APP_NAME_PARAM),
153+
myRunnerParameters.get(ElasticBeanstalkConstants.APP_VERSION_SKIP_DUPE_PARAM),
147154
myRunnerParameters.get(ElasticBeanstalkConstants.APP_VERSION_PARAM));
148155
}
149156

aws-elasticbeanstalk-common/src/main/java/jetbrains/buildServer/runner/elasticbeanstalk/AWSClient.java

+30-5
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,25 @@ AWSClient withListener(@NotNull Listener listener) {
5353
* <p>
5454
* For performing this operation target AWSClient must have corresponding S3 permissions.
5555
*
56-
* @param s3BucketName valid S3 bucket name
57-
* @param s3ObjectKey valid S3 object key
56+
* @param skipDuplicateVersions if the application version already exists, do we error out?
57+
* @param s3BucketName valid S3 bucket name
58+
* @param s3ObjectKey valid S3 object key
5859
*/
59-
void createApplicationVersion(@NotNull String applicationName, @NotNull String versionLabel,
60+
void createApplicationVersion(@NotNull String applicationName, @NotNull String versionLabel, @NotNull Boolean skipDuplicateVersions,
6061
@NotNull String s3BucketName, @NotNull String s3ObjectKey) {
6162
try {
6263
myListener.createVersionStarted(applicationName, versionLabel, s3BucketName, s3ObjectKey);
6364
S3Location location = new S3Location().withS3Bucket(s3BucketName).withS3Key(s3ObjectKey);
6465
CreateApplicationVersionRequest request = new CreateApplicationVersionRequest(applicationName, versionLabel)
6566
.withSourceBundle(location);
66-
myElasticBeanstalkClient.createApplicationVersion(request);
67-
myListener.createVersionFinished(applicationName, versionLabel, s3BucketName, s3ObjectKey);
67+
68+
if (skipDuplicateVersions && doesApplicationVersionExist(applicationName, versionLabel)) {
69+
myListener.createVersionSkipped(applicationName, versionLabel);
70+
} else {
71+
myElasticBeanstalkClient.createApplicationVersion(request);
72+
myListener.createVersionFinished(applicationName, versionLabel, s3BucketName, s3ObjectKey);
73+
}
74+
6875
} catch (Throwable t) {
6976
processFailure(t);
7077
}
@@ -233,6 +240,20 @@ private String removeTrailingDot(@Nullable String msg) {
233240
return (msg != null && msg.endsWith(".")) ? msg.substring(0, msg.length() - 1) : msg;
234241
}
235242

243+
private boolean doesApplicationVersionExist(@NotNull String applicationName, @NotNull String versionLabel) {
244+
DescribeApplicationVersionsRequest request =
245+
new DescribeApplicationVersionsRequest()
246+
.withApplicationName(applicationName)
247+
.withVersionLabels(versionLabel);
248+
249+
DescribeApplicationVersionsResult describeApplicationVersionsResult
250+
= myElasticBeanstalkClient.describeApplicationVersions(request);
251+
252+
List<ApplicationVersionDescription> applicationVersions = describeApplicationVersionsResult.getApplicationVersions();
253+
254+
return applicationVersions != null && applicationVersions.size() > 0;
255+
}
256+
236257
static class Listener {
237258
void createVersionStarted(@NotNull String applicationName, @NotNull String versionLabel,
238259
@NotNull String s3BucketName, @NotNull String s3ObjectKey) {
@@ -242,6 +263,10 @@ void createVersionFinished(@NotNull String applicationName, @NotNull String vers
242263
@NotNull String s3BucketName, @NotNull String s3ObjectKey) {
243264
}
244265

266+
void createVersionSkipped(@NotNull String applicationName, @NotNull String versionLabel) {
267+
268+
}
269+
245270
void deploymentStarted(@NotNull String environmentId, @NotNull String environmentName, @NotNull String versionLabel) {
246271
}
247272

aws-elasticbeanstalk-common/src/main/java/jetbrains/buildServer/runner/elasticbeanstalk/ElasticBeanstalkConstants.java

+5
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,15 @@ public interface ElasticBeanstalkConstants {
4747
String APP_VERSION_PARAM = "elasticbeanstalk_version_label";
4848
String APP_VERSION_LABEL = "Application Version";
4949

50+
String APP_VERSION_SKIP_DUPE_PARAM = "elasticbeanstalk_version_skip_dupe_label";
51+
String APP_VERSION_SKIP_DUPE_LABEL = "Skip Upload If Application Version Already Exists?";
52+
5053
String WAIT_FLAG_PARAM = "elasticbeanstalk_wait";
5154
String WAIT_FLAG_LABEL = "Wait for deployment finish";
55+
5256
String WAIT_TIMEOUT_SEC_PARAM = "elasticbeanstalk_wait_timeout_sec";
5357
String WAIT_TIMEOUT_SEC_LABEL = "Timeout (seconds)";
58+
5459
String WAIT_POLL_INTERVAL_SEC_CONFIG_PARAM = "elasticbeanstalk.wait.poll.interval.sec";
5560
int WAIT_POLL_INTERVAL_SEC_DEFAULT = 20;
5661

aws-elasticbeanstalk-server/src/main/resources/buildServerResources/editElasticBeanstalkParams.jsp

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@
6868
<span class="smallNote">Version Label to Publish</span><span class="error" id="error_${app_version_param}"></span>
6969
</td>
7070
</tr>
71+
<tr>
72+
<th><label for="${app_version_skip_dupe_param}">${app_version_skip_dupe_label}: </label></th>
73+
<td><props:checkboxProperty name="${app_version_skip_dupe_param}" uncheckedValue="false" /></td>
74+
</tr>
7175
<tr>
7276
<th><label for="${wait_flag_param}">${wait_flag_label}: </label></th>
7377
<td><props:checkboxProperty name="${wait_flag_param}" uncheckedValue="false" onclick="elasticBeanstalkWaitFlag()"/></td>

aws-elasticbeanstalk-server/src/main/resources/buildServerResources/paramsConstants.jspf

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
<c:set var="app_version_param" value="<%=ElasticBeanstalkConstants.APP_VERSION_PARAM%>"/>
3737
<c:set var="app_version_label" value="<%=ElasticBeanstalkConstants.APP_VERSION_LABEL%>"/>
3838

39+
<c:set var="app_version_skip_dupe_param" value="<%=ElasticBeanstalkConstants.APP_VERSION_SKIP_DUPE_PARAM%>"/>
40+
<c:set var="app_version_skip_dupe_label" value="<%=ElasticBeanstalkConstants.APP_VERSION_SKIP_DUPE_LABEL%>"/>
41+
3942
<c:set var="wait_flag_param" value="<%=ElasticBeanstalkConstants.WAIT_FLAG_PARAM%>"/>
4043
<c:set var="wait_flag_label" value="<%=ElasticBeanstalkConstants.WAIT_FLAG_LABEL%>"/>
4144

aws-elasticbeanstalk-server/src/main/resources/buildServerResources/viewElasticBeanstalkParams.jsp

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
${app_version_label}: <props:displayValue name="${app_version_param}" emptyValue="empty"/>
4242
</div>
4343

44+
<div class="parameter">
45+
${app_version_skip_dupe_label}: <strong><props:displayCheckboxValue name="${app_version_skip_dupe_param}"/></strong>
46+
</div>
47+
4448
<div class="parameter">
4549
${wait_flag_label}: <strong><props:displayCheckboxValue name="${wait_flag_param}"/></strong>
4650
</div>

0 commit comments

Comments
 (0)