Skip to content

Commit

Permalink
Issue awslabs#40 propagate job cancellation to CodeDeploy
Browse files Browse the repository at this point in the history
Change-Id: Ib42ea1acc853aed054906e9f0b8d110e0b4511df
  • Loading branch information
Brandon Rochon committed Nov 17, 2015
1 parent 40d7b24 commit f253aed
Showing 1 changed file with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import com.amazonaws.services.codedeploy.model.GetDeploymentRequest;
import com.amazonaws.services.codedeploy.model.RegisterApplicationRevisionRequest;
import com.amazonaws.services.codedeploy.model.S3Location;
import com.amazonaws.services.codedeploy.model.StopDeploymentRequest;
import com.amazonaws.services.codedeploy.model.StopDeploymentResult;

import hudson.FilePath;
import hudson.Launcher;
Expand Down Expand Up @@ -210,6 +212,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
}

boolean success;
String deploymentId = null;

try {

Expand All @@ -222,10 +225,21 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
RevisionLocation revisionLocation = zipAndUpload(aws, projectName, getSourceDirectory(build.getWorkspace()), envVars);

registerRevision(aws, revisionLocation);
String deploymentId = createDeployment(aws, revisionLocation);
deploymentId = createDeployment(aws, revisionLocation);

success = waitForDeployment(aws, deploymentId);

} catch (InterruptedException e) {

this.logger.println("User cancelling CodeDeploy job");
try {
this.logger.println(stopDeployment(aws, deploymentId));
} catch (Exception ex) {
this.logger.println(e.getMessage());
e.printStackTrace(this.logger);
}
success = false;

} catch (Exception e) {

this.logger.println("Failed CodeDeploy post-build step; exception follows.");
Expand Down Expand Up @@ -368,6 +382,18 @@ private String createDeployment(AWSClients aws, RevisionLocation revisionLocatio
return createDeploymentResult.getDeploymentId();
}

private String stopDeployment(AWSClients aws, String deploymentId) throws Exception {

this.logger.println("Stopping deployment " + deploymentId);

StopDeploymentResult stopDeploymentResult = aws.codedeploy.stopDeployment(
new StopDeploymentRequest()
.withDeploymentId(deploymentId)
);

return stopDeploymentResult.getStatus();
}

private boolean waitForDeployment(AWSClients aws, String deploymentId) throws InterruptedException {

if (!this.waitForCompletion) {
Expand Down

0 comments on commit f253aed

Please sign in to comment.