Skip to content

Commit 57c2bd8

Browse files
ansoniDDRBoxman
authored andcommitted
Add support for Jenkins Pipeline
1 parent 1312c91 commit 57c2bd8

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Setting up
1212
After building and installing the plugin, some simple configuration is needed
1313
for your project.
1414

15+
**Freestyle**
16+
1517
1. Open up your project configuration
1618
1. In the `Post-build Actions` section, select "Deploy an application to AWS
1719
CodeDeploy"
@@ -25,6 +27,18 @@ associated role has, at minimum, a policy that permits `codedeploy:*` and
2527
1. Temporary access keys. These will use the global keys from the Jenkins
2628
instance.
2729

30+
**Pipeline**
31+
32+
1. Create a [Jenkins Pipeline](https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Plugin) project
33+
1. Use the Pipeline Snippet Generator
34+
1. For 'Sample Step', choose 'step: General Build Step'
35+
1. For 'Build Step', choose 'Deploy an application to AWS CodeDeploy'
36+
1. populate variables and then 'Generate Groovy'
37+
38+
Here is a rather blank example:
39+
40+
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])
41+
2842
License
2943
-------
3044

src/main/java/com/amazonaws/codedeploy/AWSCodeDeployPublisher.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,16 @@
3131
import com.amazonaws.services.codedeploy.model.RegisterApplicationRevisionRequest;
3232
import com.amazonaws.services.codedeploy.model.S3Location;
3333

34-
import hudson.FilePath;
35-
import hudson.Launcher;
36-
import hudson.Extension;
37-
import hudson.Util;
38-
import hudson.model.AbstractBuild;
39-
import hudson.model.BuildListener;
40-
import hudson.model.AbstractProject;
41-
import hudson.model.Result;
34+
import hudson.*;
35+
import hudson.model.*;
4236
import hudson.tasks.BuildStepMonitor;
4337
import hudson.tasks.BuildStepDescriptor;
4438
import hudson.tasks.Publisher;
4539
import hudson.util.DirScanner;
4640
import hudson.util.FileVisitor;
4741
import hudson.util.FormValidation;
4842
import hudson.util.ListBoxModel;
43+
import jenkins.tasks.SimpleBuildStep;
4944
import net.sf.json.JSONException;
5045
import net.sf.json.JSONObject;
5146

@@ -65,17 +60,18 @@
6560
import java.util.Map;
6661
import java.util.UUID;
6762

63+
import javax.annotation.Nonnull;
6864
import javax.servlet.ServletException;
6965

7066
/**
7167
* The AWS CodeDeploy Publisher is a post-build plugin that adds the ability to start a new CodeDeploy deployment
7268
* with the project's workspace as the application revision.
73-
* <p/>
69+
*
7470
* To configure, users must create an IAM role that allows "S3" and "CodeDeploy" actions and must be assumable by
7571
* the globally configured keys. This allows the plugin to get temporary credentials instead of requiring permanent
7672
* credentials to be configured for each project.
7773
*/
78-
public class AWSCodeDeployPublisher extends Publisher {
74+
public class AWSCodeDeployPublisher extends Publisher implements SimpleBuildStep {
7975
public static final long DEFAULT_TIMEOUT_SECONDS = 900;
8076
public static final long DEFAULT_POLLING_FREQUENCY_SECONDS = 15;
8177
public static final String ROLE_SESSION_NAME = "jenkins-codedeploy-plugin";
@@ -182,13 +178,13 @@ public AWSCodeDeployPublisher(
182178
}
183179

184180
@Override
185-
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
181+
public void perform(@Nonnull Run<?,?> build, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws IOException, InterruptedException {
186182
this.logger = listener.getLogger();
187183
envVars = build.getEnvironment(listener);
188184
final boolean buildFailed = build.getResult() == Result.FAILURE;
189185
if (buildFailed) {
190186
logger.println("Skipping CodeDeploy publisher as build failed");
191-
return true;
187+
return;
192188
}
193189

194190
final AWSClients aws;
@@ -215,14 +211,13 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
215211
this.proxyPort);
216212
}
217213

218-
boolean success;
214+
boolean success = false;
219215

220216
try {
221217

222218
verifyCodeDeployApplication(aws);
223219

224-
final String projectName = build.getProject().getName();
225-
final FilePath workspace = build.getWorkspace();
220+
final String projectName = build.getDisplayName();
226221
if (workspace == null) {
227222
throw new IllegalArgumentException("No workspace present for the build.");
228223
}
@@ -244,11 +239,11 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
244239
this.logger.println("Failed CodeDeploy post-build step; exception follows.");
245240
this.logger.println(e.getMessage());
246241
e.printStackTrace(this.logger);
247-
success = false;
248-
249242
}
250243

251-
return success;
244+
if (!success) {
245+
throw new AbortException();
246+
}
252247
}
253248

254249
private FilePath getSourceDirectory(FilePath basePath) throws IOException, InterruptedException {
@@ -493,10 +488,10 @@ public BuildStepMonitor getRequiredMonitorService() {
493488
}
494489

495490
/**
491+
*
496492
* Descriptor for {@link AWSCodeDeployPublisher}. Used as a singleton.
497493
* The class is marked as public so that it can be accessed from views.
498-
* <p/>
499-
* <p/>
494+
*
500495
* See <tt>src/main/resources/com/amazonaws/codedeploy/AWSCodeDeployPublisher/*.jelly</tt>
501496
* for the actual HTML fragment for the configuration screen.
502497
*/

0 commit comments

Comments
 (0)