5757
5858import java .io .File ;
5959import java .io .FileOutputStream ;
60+ import java .io .FileReader ;
6061import java .io .IOException ;
6162import java .io .PrintStream ;
6263import java .util .Date ;
@@ -100,6 +101,8 @@ public class AWSCodeDeployPublisher extends Publisher {
100101 private final String awsAccessKey ;
101102 private final String awsSecretKey ;
102103 private final String credentials ;
104+ private final String deploymentMethod ;
105+ private final String versionFileName ;
103106
104107 private PrintStream logger ;
105108 private Map <String , String > envVars ;
@@ -117,6 +120,8 @@ public AWSCodeDeployPublisher(
117120 Long pollingTimeoutSec ,
118121 Long pollingFreqSec ,
119122 String credentials ,
123+ String versionFileName ,
124+ String deploymentMethod ,
120125 String awsAccessKey ,
121126 String awsSecretKey ,
122127 String iamRoleArn ,
@@ -142,6 +147,8 @@ public AWSCodeDeployPublisher(
142147 this .proxyHost = proxyHost ;
143148 this .proxyPort = proxyPort ;
144149 this .credentials = credentials ;
150+ this .deploymentMethod = deploymentMethod ;
151+ this .versionFileName = versionFileName ;
145152 this .awsAccessKey = awsAccessKey ;
146153 this .awsSecretKey = awsSecretKey ;
147154 this .iamRoleArn = iamRoleArn ;
@@ -165,7 +172,6 @@ public AWSCodeDeployPublisher(
165172 this .pollingFreqSec = null ;
166173 }
167174
168-
169175 this .s3bucket = s3bucket ;
170176 if (s3prefix == null || s3prefix .equals ("/" ) || s3prefix .length () == 0 ) {
171177 this .s3prefix = "" ;
@@ -218,9 +224,14 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
218224 RevisionLocation revisionLocation = zipAndUpload (aws , projectName , getSourceDirectory (build .getWorkspace ()));
219225
220226 registerRevision (aws , revisionLocation );
221- String deploymentId = createDeployment (aws , revisionLocation );
227+ if ("onlyRevision" .equals (deploymentMethod )){
228+ success = true ;
229+ } else {
230+
231+ String deploymentId = createDeployment (aws , revisionLocation );
222232
223- success = waitForDeployment (aws , deploymentId );
233+ success = waitForDeployment (aws , deploymentId );
234+ }
224235
225236 } catch (Exception e ) {
226237
@@ -263,7 +274,7 @@ private void verifyCodeDeployApplication(AWSClients aws) throws IllegalArgumentE
263274 ListApplicationsResult applications = aws .codedeploy .listApplications ();
264275 String applicationName = getApplicationNameFromEnv ();
265276 String deploymentGroupName = getDeploymentGroupNameFromEnv ();
266-
277+
267278 if (!applications .getApplications ().contains (applicationName )) {
268279 throw new IllegalArgumentException ("Cannot find application named '" + applicationName + "'" );
269280 }
@@ -281,7 +292,31 @@ private void verifyCodeDeployApplication(AWSClients aws) throws IllegalArgumentE
281292
282293 private RevisionLocation zipAndUpload (AWSClients aws , String projectName , FilePath sourceDirectory ) throws IOException , InterruptedException , IllegalArgumentException {
283294
284- File zipFile = File .createTempFile (projectName + "-" , ".zip" );
295+ File zipFile = null ;
296+ File versionFile ;
297+ versionFile = new File (sourceDirectory + "/" + versionFileName );
298+
299+ FileReader reader = null ;
300+ String version = null ;
301+ try {
302+ reader = new FileReader (versionFile );
303+ char [] chars = new char [(int ) versionFile .length () -1 ];
304+ reader .read (chars );
305+ version = new String (chars );
306+ reader .close ();
307+ } catch (IOException e ) {
308+ e .printStackTrace ();
309+ } finally {
310+ if (reader !=null ){reader .close ();}
311+ }
312+
313+ if (version != null ){
314+ zipFile = new File ("/tmp/" + projectName + "-" + version + ".zip" );
315+ zipFile .createNewFile ();
316+ } else {
317+ zipFile = File .createTempFile (projectName + "-" , ".zip" );
318+ }
319+
285320 String key ;
286321 File appspec ;
287322 File dest ;
@@ -329,7 +364,6 @@ private RevisionLocation zipAndUpload(AWSClients aws, String projectName, FilePa
329364 logger .println ("Uploading zip to s3://" + bucket + "/" + key );
330365 PutObjectResult s3result = aws .s3 .putObject (bucket , key , zipFile );
331366
332-
333367 S3Location s3Location = new S3Location ();
334368 s3Location .setBucket (bucket );
335369 s3Location .setKey (key );
@@ -418,9 +452,9 @@ private boolean waitForDeployment(AWSClients aws, String deploymentId) throws In
418452
419453 Thread .sleep (pollingFreqMillis );
420454 }
421-
455+
422456 logger .println ("Deployment status: " + deployStatus .getStatus () + "; instances: " + deployStatus .getDeploymentOverview ());
423-
457+
424458 if (!deployStatus .getStatus ().equals (DeploymentStatus .Succeeded .toString ())) {
425459 this .logger .println ("Deployment did not succeed. Final status: " + deployStatus .getStatus ());
426460 success = false ;
@@ -634,6 +668,14 @@ public String getExternalId() {
634668 return externalId ;
635669 }
636670
671+ public String getDeploymentMethod () {
672+ return deploymentMethod ;
673+ }
674+
675+ public String getVersionFileName () {
676+ return versionFileName ;
677+ }
678+
637679 public boolean getWaitForCompletion () {
638680 return waitForCompletion ;
639681 }
@@ -690,4 +732,3 @@ public String getS3PrefixFromEnv() {
690732 return Util .replaceMacro (this .s3prefix , envVars );
691733 }
692734}
693-
0 commit comments