Skip to content

Add support for Jenkins Pipeline #72

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

Merged
merged 1 commit into from
Dec 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
-------

Expand Down
34 changes: 17 additions & 17 deletions src/main/java/com/amazonaws/codedeploy/AWSCodeDeployPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
* <p/>
*
* 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";
Expand Down Expand Up @@ -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;
Expand All @@ -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.");
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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.
* <p/>
* <p/>
*
* See <tt>src/main/resources/com/amazonaws/codedeploy/AWSCodeDeployPublisher/*.jelly</tt>
* for the actual HTML fragment for the configuration screen.
*/
Expand Down