Skip to content

Commit 69eec98

Browse files
ansoniDDRBoxman
authored andcommitted
Add support for Jenkins Pipeline
1 parent 1312c91 commit 69eec98

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
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: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@
3131
import com.amazonaws.services.codedeploy.model.RegisterApplicationRevisionRequest;
3232
import com.amazonaws.services.codedeploy.model.S3Location;
3333

34+
import hudson.AbortException;
3435
import hudson.FilePath;
3536
import hudson.Launcher;
36-
import hudson.Extension;
3737
import hudson.Util;
38-
import hudson.model.AbstractBuild;
39-
import hudson.model.BuildListener;
38+
import hudson.Extension;
4039
import hudson.model.AbstractProject;
4140
import hudson.model.Result;
41+
import hudson.model.Run;
42+
import hudson.model.TaskListener;
4243
import hudson.tasks.BuildStepMonitor;
4344
import hudson.tasks.BuildStepDescriptor;
4445
import hudson.tasks.Publisher;
4546
import hudson.util.DirScanner;
46-
import hudson.util.FileVisitor;
4747
import hudson.util.FormValidation;
4848
import hudson.util.ListBoxModel;
49-
import net.sf.json.JSONException;
49+
import jenkins.tasks.SimpleBuildStep;
5050
import net.sf.json.JSONObject;
5151

5252
import org.apache.commons.lang.StringUtils;
@@ -65,17 +65,18 @@
6565
import java.util.Map;
6666
import java.util.UUID;
6767

68+
import javax.annotation.Nonnull;
6869
import javax.servlet.ServletException;
6970

7071
/**
7172
* The AWS CodeDeploy Publisher is a post-build plugin that adds the ability to start a new CodeDeploy deployment
7273
* with the project's workspace as the application revision.
73-
* <p/>
74+
*
7475
* To configure, users must create an IAM role that allows "S3" and "CodeDeploy" actions and must be assumable by
7576
* the globally configured keys. This allows the plugin to get temporary credentials instead of requiring permanent
7677
* credentials to be configured for each project.
7778
*/
78-
public class AWSCodeDeployPublisher extends Publisher {
79+
public class AWSCodeDeployPublisher extends Publisher implements SimpleBuildStep {
7980
public static final long DEFAULT_TIMEOUT_SECONDS = 900;
8081
public static final long DEFAULT_POLLING_FREQUENCY_SECONDS = 15;
8182
public static final String ROLE_SESSION_NAME = "jenkins-codedeploy-plugin";
@@ -182,13 +183,13 @@ public AWSCodeDeployPublisher(
182183
}
183184

184185
@Override
185-
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
186+
public void perform(@Nonnull Run<?,?> build, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws IOException, InterruptedException {
186187
this.logger = listener.getLogger();
187188
envVars = build.getEnvironment(listener);
188189
final boolean buildFailed = build.getResult() == Result.FAILURE;
189190
if (buildFailed) {
190191
logger.println("Skipping CodeDeploy publisher as build failed");
191-
return true;
192+
return;
192193
}
193194

194195
final AWSClients aws;
@@ -215,14 +216,13 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
215216
this.proxyPort);
216217
}
217218

218-
boolean success;
219+
boolean success = false;
219220

220221
try {
221222

222223
verifyCodeDeployApplication(aws);
223224

224-
final String projectName = build.getProject().getName();
225-
final FilePath workspace = build.getWorkspace();
225+
final String projectName = build.getDisplayName();
226226
if (workspace == null) {
227227
throw new IllegalArgumentException("No workspace present for the build.");
228228
}
@@ -244,11 +244,11 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
244244
this.logger.println("Failed CodeDeploy post-build step; exception follows.");
245245
this.logger.println(e.getMessage());
246246
e.printStackTrace(this.logger);
247-
success = false;
248-
249247
}
250248

251-
return success;
249+
if (!success) {
250+
throw new AbortException();
251+
}
252252
}
253253

254254
private FilePath getSourceDirectory(FilePath basePath) throws IOException, InterruptedException {
@@ -493,10 +493,10 @@ public BuildStepMonitor getRequiredMonitorService() {
493493
}
494494

495495
/**
496+
*
496497
* Descriptor for {@link AWSCodeDeployPublisher}. Used as a singleton.
497498
* The class is marked as public so that it can be accessed from views.
498-
* <p/>
499-
* <p/>
499+
*
500500
* See <tt>src/main/resources/com/amazonaws/codedeploy/AWSCodeDeployPublisher/*.jelly</tt>
501501
* for the actual HTML fragment for the configuration screen.
502502
*/

0 commit comments

Comments
 (0)