Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate job cancellation to CodeDeploy #41

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deploymentId might be null here, we should handle that case.

} catch (Exception ex) {
this.logger.println(e.getMessage());
e.printStackTrace(this.logger);
}
success = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the stopDeployment fails because the deployment completed successfully, we shouldn't fail the job, but actually mark it as successful.


} 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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stopping a deployment might not be immediate. We might want to wait for the deployment to actually stop before continuing.

new StopDeploymentRequest()
.withDeploymentId(deploymentId)
);

return stopDeploymentResult.getStatus();
}

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

if (!this.waitForCompletion) {
Expand Down