From 69eec989c6145f7e6e641ce8c2b4561a3ef63641 Mon Sep 17 00:00:00 2001 From: Anthony Johnson Date: Mon, 6 Jun 2016 00:07:11 -0700 Subject: [PATCH] Add support for Jenkins Pipeline --- README.md | 14 ++++++++ .../codedeploy/AWSCodeDeployPublisher.java | 34 +++++++++---------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 644e10d..f2c4713 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,8 @@ Setting up After building and installing the plugin, some simple configuration is needed for your project. +**Freestyle** + 1. Open up your project configuration 1. In the `Post-build Actions` section, select "Deploy an application to AWS CodeDeploy" @@ -25,6 +27,18 @@ associated role has, at minimum, a policy that permits `codedeploy:*` and 1. Temporary access keys. These will use the global keys from the Jenkins instance. +**Pipeline** + +1. Create a [Jenkins Pipeline](https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Plugin) project +1. Use the Pipeline Snippet Generator +1. For 'Sample Step', choose 'step: General Build Step' +1. For 'Build Step', choose 'Deploy an application to AWS CodeDeploy' +1. populate variables and then 'Generate Groovy' + +Here is a rather blank example: + + step([$class: 'AWSCodeDeployPublisher', applicationName: '', awsAccessKey: '', awsSecretKey: '', credentials: 'awsAccessKey', deploymentGroupAppspec: false, deploymentGroupName: '', deploymentMethod: 'deploy', excludes: '', iamRoleArn: '', includes: '**', proxyHost: '', proxyPort: 0, region: 'ap-northeast-1', s3bucket: '', s3prefix: '', subdirectory: '', versionFileName: '', waitForCompletion: false]) + License ------- diff --git a/src/main/java/com/amazonaws/codedeploy/AWSCodeDeployPublisher.java b/src/main/java/com/amazonaws/codedeploy/AWSCodeDeployPublisher.java index 7ecc46f..d386509 100644 --- a/src/main/java/com/amazonaws/codedeploy/AWSCodeDeployPublisher.java +++ b/src/main/java/com/amazonaws/codedeploy/AWSCodeDeployPublisher.java @@ -31,22 +31,22 @@ import com.amazonaws.services.codedeploy.model.RegisterApplicationRevisionRequest; import com.amazonaws.services.codedeploy.model.S3Location; +import hudson.AbortException; import hudson.FilePath; import hudson.Launcher; -import hudson.Extension; import hudson.Util; -import hudson.model.AbstractBuild; -import hudson.model.BuildListener; +import hudson.Extension; import hudson.model.AbstractProject; import hudson.model.Result; +import hudson.model.Run; +import hudson.model.TaskListener; import hudson.tasks.BuildStepMonitor; import hudson.tasks.BuildStepDescriptor; import hudson.tasks.Publisher; import hudson.util.DirScanner; -import hudson.util.FileVisitor; import hudson.util.FormValidation; import hudson.util.ListBoxModel; -import net.sf.json.JSONException; +import jenkins.tasks.SimpleBuildStep; import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils; @@ -65,17 +65,18 @@ import java.util.Map; import java.util.UUID; +import javax.annotation.Nonnull; import javax.servlet.ServletException; /** * The AWS CodeDeploy Publisher is a post-build plugin that adds the ability to start a new CodeDeploy deployment * with the project's workspace as the application revision. - *

+ * * To configure, users must create an IAM role that allows "S3" and "CodeDeploy" actions and must be assumable by * the globally configured keys. This allows the plugin to get temporary credentials instead of requiring permanent * credentials to be configured for each project. */ -public class AWSCodeDeployPublisher extends Publisher { +public class AWSCodeDeployPublisher extends Publisher implements SimpleBuildStep { public static final long DEFAULT_TIMEOUT_SECONDS = 900; public static final long DEFAULT_POLLING_FREQUENCY_SECONDS = 15; public static final String ROLE_SESSION_NAME = "jenkins-codedeploy-plugin"; @@ -182,13 +183,13 @@ public AWSCodeDeployPublisher( } @Override - public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException { + public void perform(@Nonnull Run build, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws IOException, InterruptedException { this.logger = listener.getLogger(); envVars = build.getEnvironment(listener); final boolean buildFailed = build.getResult() == Result.FAILURE; if (buildFailed) { logger.println("Skipping CodeDeploy publisher as build failed"); - return true; + return; } final AWSClients aws; @@ -215,14 +216,13 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis this.proxyPort); } - boolean success; + boolean success = false; try { verifyCodeDeployApplication(aws); - final String projectName = build.getProject().getName(); - final FilePath workspace = build.getWorkspace(); + final String projectName = build.getDisplayName(); if (workspace == null) { throw new IllegalArgumentException("No workspace present for the build."); } @@ -244,11 +244,11 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis this.logger.println("Failed CodeDeploy post-build step; exception follows."); this.logger.println(e.getMessage()); e.printStackTrace(this.logger); - success = false; - } - return success; + if (!success) { + throw new AbortException(); + } } private FilePath getSourceDirectory(FilePath basePath) throws IOException, InterruptedException { @@ -493,10 +493,10 @@ public BuildStepMonitor getRequiredMonitorService() { } /** + * * Descriptor for {@link AWSCodeDeployPublisher}. Used as a singleton. * The class is marked as public so that it can be accessed from views. - *

- *

+ * * See src/main/resources/com/amazonaws/codedeploy/AWSCodeDeployPublisher/*.jelly * for the actual HTML fragment for the configuration screen. */