Skip to content

Commit

Permalink
[FIXES JENKINS-10694] lookup MavenModuleSetBuild submodules for artif…
Browse files Browse the repository at this point in the history
…acts
  • Loading branch information
ndeloof committed Aug 12, 2011
1 parent 3ffd084 commit 7df1a28
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 51 deletions.
97 changes: 55 additions & 42 deletions src/main/java/org/jenkins/plugins/cloudbees/CloudbeesPublisher.java
Expand Up @@ -15,20 +15,12 @@
*/
package org.jenkins.plugins.cloudbees;

import com.cloudbees.api.ApplicationInfo;
import com.cloudbees.api.ApplicationListResponse;
import com.cloudbees.api.BeesClientException;
import com.cloudbees.api.UploadProgress;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.maven.MavenBuild;
import hudson.maven.MavenModule;
import hudson.maven.MavenModuleSetBuild;
import hudson.maven.reporters.MavenAbstractArtifactRecord;
import hudson.maven.reporters.MavenArtifact;
import hudson.maven.reporters.MavenArtifactRecord;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.AutoCompletionCandidates;
Expand All @@ -42,25 +34,29 @@
import hudson.util.CopyOnWriteList;
import hudson.util.FormValidation;
import hudson.util.IOException2;
import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.jenkins.plugins.cloudbees.util.FileFinder;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

import javax.servlet.ServletException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.jenkins.plugins.cloudbees.util.FileFinder;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

import com.cloudbees.api.ApplicationInfo;
import com.cloudbees.api.ApplicationListResponse;
import com.cloudbees.api.BeesClientException;
import com.cloudbees.api.UploadProgress;
import net.sf.json.JSONObject;

/**
* @author Olivier Lamy
Expand Down Expand Up @@ -140,44 +136,45 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, final Build
new CloudbeesApiHelper.CloudbeesApiRequest(DescriptorImpl.CLOUDBEES_API_URL, cloudbeesAccount.apiKey,
cloudbeesAccount.secretKey);

List<ArtifactFilePathSaveAction> artifactFilePathSaveActions =
build.getActions(ArtifactFilePathSaveAction.class);
List<ArtifactFilePathSaveAction> artifactFilePathSaveActions = retrieveArtifactFilePathSaveActions(build);

if (artifactFilePathSaveActions.isEmpty() && StringUtils.isBlank(filePattern)) {
listener.getLogger().println(Messages._CloudbeesPublisher_noArtifacts( build.getProject().getName() ));
return true;
}

String warPath = null;

String warPath = null;
findWarPath:
for (ArtifactFilePathSaveAction artifactFilePathSaveAction : artifactFilePathSaveActions) {
listener.getLogger().println("artifacts " + artifactFilePathSaveAction.mavenArtifactWithFilePaths);
for (MavenArtifactWithFilePath artifactWithFilePath : artifactFilePathSaveAction.mavenArtifactWithFilePaths) {
if (StringUtils.equals("war", artifactWithFilePath.type)) {
listener.getLogger().println("artifactWithFilePath" + artifactWithFilePath.filePath);
listener.getLogger().println(Messages.CloudbeesPublisher_WarPathFound(artifactWithFilePath) );
warPath = artifactWithFilePath.filePath;
break findWarPath;
}
}
}

if (StringUtils.isBlank(warPath) && StringUtils.isBlank(filePattern)) {
listener.getLogger().println(Messages._CloudbeesPublisher_noWarArtifacts());
return false;
}
if (StringUtils.isBlank(warPath) && !StringUtils.isBlank(filePattern)) {
//search file in the workspace with the pattern
FileFinder fileFinder = new FileFinder(filePattern);
List<String> fileNames = build.getWorkspace().act(fileFinder);
listener.getLogger().println("found remote files : " + fileNames);
if (fileNames.size() > 1) {
listener.getLogger().println("your pattern must return only one file to deploy");
return false;
} else if (fileNames.size() == 0) {
listener.getLogger().println(Messages._CloudbeesPublisher_noArtifactsFound(filePattern));
if (StringUtils.isBlank(warPath)) {
if (StringUtils.isBlank(filePattern)) {
listener.getLogger().println(Messages._CloudbeesPublisher_noWarArtifacts());
return false;
}
// so we use only the first found
warPath = fileNames.get(0);
} else {
//search file in the workspace with the pattern
FileFinder fileFinder = new FileFinder(filePattern);
List<String> fileNames = build.getWorkspace().act(fileFinder);
listener.getLogger().println("found remote files : " + fileNames);
if (fileNames.size() > 1) {
listener.getLogger().println(Messages.CloudbeesPublisher_ToManyFilesMatchingPattern());
return false;
} else if (fileNames.size() == 0) {
listener.getLogger().println(Messages._CloudbeesPublisher_noArtifactsFound(filePattern));
return false;
}
// so we use only the first found
warPath = fileNames.get(0);
}
}


Expand All @@ -195,7 +192,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, final Build

warPath = tmpArchive.getPath();

listener.getLogger().println(" deploying archive to cloudbees application " + applicationId);
listener.getLogger().println(Messages.CloudbeesPublisher_Deploying(applicationId));

String description = "Jenkins build " + build.getId();
CloudbeesApiHelper.getBeesClient(apiRequest).applicationDeployWar(applicationId, "environnement",
Expand All @@ -216,6 +213,22 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, final Build
return true;
}

private List<ArtifactFilePathSaveAction> retrieveArtifactFilePathSaveActions(AbstractBuild<?, ?> build) {
List<ArtifactFilePathSaveAction> artifactFilePathSaveActions = new ArrayList<ArtifactFilePathSaveAction>();
List<ArtifactFilePathSaveAction> actions = build.getActions(ArtifactFilePathSaveAction.class);
if (actions != null) artifactFilePathSaveActions.addAll(actions);

if (build instanceof MavenModuleSetBuild) {
for (List<MavenBuild> mavenBuilds : ((MavenModuleSetBuild) build).getModuleBuilds().values()) {
for (MavenBuild mavenBuild : mavenBuilds) {
actions = mavenBuild.getActions(ArtifactFilePathSaveAction.class);
if (actions != null) artifactFilePathSaveActions.addAll(actions);
}
}
}
return artifactFilePathSaveActions;
}

private static class ConsoleListenerUploadProgress
implements UploadProgress {
private final BuildListener listener;
Expand Down
@@ -1,7 +1,7 @@
CloudbeesPublisher.noArtifacts = Skipping CloudBees deployment as no artifacts has been saved for project {0}
CloudbeesPublisher.noWarArtifacts = No war artifacts have been saved. Are you sure your build produced some?
CloudbeesPublisher.noArtifactsFound = Could not find the war specified by the pattern {0}
CloudbeesPublisher.perform = CloudbeesPublisher :: perform {0} :: {1}
CloudbeesPublisher.perform = CloudbeesPublisher deploying to application {0} :: {1}
CloudbeesPublisher.displayName = Deploy to CloudBees
CloudbeesPublisher.nameNotEmpty = Name cannot be empty
CloudbeesPublisher.apiKeyNotEmpty = API Key cannot be empty
Expand All @@ -10,3 +10,6 @@ CloudbeesPublisher.applicationIdNotEmpty = Application Id cannot be empty
CloudbeesPublisher.possibleApplicationIds = Possible applicationIds are {0}
CloudbeesPublisher.authenticationFailure = The signature was invalid, likely because the secret key and/or API key were incorrect.
CloudbeesPublisher.noAccount = There is no CloudBees account configured. Please configure it in the global configuration (Manage Jenkins -> Configure System, CloudBees accounts -> Add)
CloudbeesPublisher.WarPathFound = Deployable artifact found {0}
CloudbeesPublisher.ToManyFilesMatchingPattern = Your pattern must return only one file to deploy
CloudbeesPublisher.Deploying=Deploying archive to cloudbees application {0}
2 changes: 1 addition & 1 deletion src/main/webapp/help-file-pattern.html
@@ -1,3 +1,3 @@
<div>
If you don't use a maven2 build you must specify a file pattern to find the archive file to deploy.
If you don't use a maven build you must specify a file pattern to find the archive file to deploy.
</div>
Expand Up @@ -20,10 +20,6 @@
import hudson.model.FreeStyleProject;
import hudson.tasks.Ant;
import hudson.tasks.Maven;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.jvnet.hudson.test.ExtractResourceSCM;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -32,6 +28,11 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.jvnet.hudson.test.ExtractResourceSCM;

/**
* @author Olivier Lamy
*/
Expand All @@ -53,16 +54,26 @@ public void testWithMaven2Project()
assertOnFileItems();
}

public void failtestWithMaven3ProjectWithPattern()
/**
* Fails with :
Caused by: java.io.IOException: Remote call on channel failed
at hudson.remoting.Channel.call(Channel.java:652)
at hudson.remoting.RemoteInvocationHandler.invoke(RemoteInvocationHandler.java:158)
... 31 more
Caused by: java.lang.AssertionError
at hudson.model.Run.setResult(Run.java:329)
at hudson.maven.MavenBuild$ProxyImpl.setResult(MavenBuild.java:427)
*/
public void failtestWithMaven3Project()
throws Exception
{
MavenModuleSet m = createMavenProject();
Maven.MavenInstallation mavenInstallation = configureMaven3();
m.setMaven( mavenInstallation.getName() );

m.setGoals( "clean install -e -X" );
m.setGoals( "clean install" );
m.setScm( new ExtractResourceSCM( getClass().getResource( "test-project.zip" ) ) );
m.getPublishers().add( new CloudbeesPublisher( "olamy", "foo/beer", "**/translate-puzzle-webapp/**target**/*.war" ) );
m.getPublishers().add( new CloudbeesPublisher( "olamy", "foo/beer", null ) );
MavenModuleSetBuild mmsb = buildAndAssertSuccess( m );
assertOnFileItems();
}
Expand Down

1 comment on commit 7df1a28

@olamy
Copy link
Member

@olamy olamy commented on 7df1a28 Aug 12, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't miss to update the changelog in the wiki page https://wiki.jenkins-ci.org/display/JENKINS/Cloudbees+Deployer+Plugin
:P

Please sign in to comment.