Skip to content

Commit

Permalink
JENKINS-11723 : Add compatibility for the promotion-build plugin. You…
Browse files Browse the repository at this point in the history
… can now deploy binaries from a previous build and not only the latest one (take care that appaloosa won't allow you to deploy an older version of an application already deployed)
  • Loading branch information
aheritier committed Mar 7, 2012
1 parent 72ad246 commit 2dd6f0f
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 13 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -113,5 +113,11 @@
<artifactId>commons-io</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>promoted-builds</artifactId>
<version>2.4</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>
45 changes: 32 additions & 13 deletions src/main/java/org/jenkins/plugins/appaloosa/AppaloosaPublisher.java
Expand Up @@ -28,19 +28,20 @@
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.*;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.Hudson;
import hudson.model.Node;
import hudson.model.Result;
import hudson.plugins.promoted_builds.Promotion;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import hudson.util.FormValidation;
import hudson.util.RunList;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
Expand All @@ -52,6 +53,13 @@
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class AppaloosaPublisher extends Recorder {

public final String token;
Expand Down Expand Up @@ -100,31 +108,42 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
//search file in the workspace with the pattern
FileFinder fileFinder = new FileFinder(filePattern);

FilePath ws = build.getWorkspace();
if (ws==null) { // slave down?
listener.error(Messages.AppaloosaPublisher_buildWorkspaceUnavailable());
return false;
// Where we'll get artifacts from
FilePath rootDir;
// If the promotion plugin is used we have to take care to get data from the original build (not the promotion build)
if (Hudson.getInstance().getPlugin("promoted-builds") != null && build instanceof Promotion) {
rootDir = new FilePath (((Promotion) build).getTarget().getArtifactsDir());
} else {
rootDir = build.getWorkspace();
if (rootDir==null) { // slave down?
listener.error(Messages.AppaloosaPublisher_buildWorkspaceUnavailable());
return false;
}
}
List<String> fileNames = ws.act(fileFinder);
listener.getLogger().println(Messages.AppaloosaPublisher_RootDirectory(rootDir));

List<String> fileNames = rootDir.act(fileFinder);
listener.getLogger().println(Messages.AppaloosaPublisher_foundFiles(fileNames));

if (fileNames.isEmpty()) {
listener.error(Messages._AppaloosaPublisher_noArtifactsFound(filePattern).toString());
return false;
}

// Initialize Appaloosa Client
AppaloosaClient appaloosaClient = new AppaloosaClient(token,proxyHost,proxyPort,proxyUser,proxyPass);
appaloosaClient.useLogger(listener.getLogger());

boolean result=true;
// Deploy each artifact found
for (String filename : fileNames) {
File tmpArchive = File.createTempFile("jenkins", "temp-appaloosa-deploy."+FilenameUtils.getExtension(filename));

try {
// handle remote slave case so copy binary locally
Node buildNode = Hudson.getInstance().getNode(build.getBuiltOnStr());
FilePath tmpLocalFile = new FilePath(tmpArchive);
FilePath remoteFile = build.getWorkspace().child(filename);
FilePath remoteFile = rootDir.child(filename);
remoteFile.copyTo(tmpLocalFile);

listener.getLogger().println(Messages.AppaloosaPublisher_deploying(filename));
Expand Down
@@ -0,0 +1,74 @@
/*
* The MIT License
*
* Copyright (c) 2012 eXo platform
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkins.plugins.appaloosa;

import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.JobProperty;
import hudson.model.TaskListener;
import hudson.model.listeners.RunListener;
import hudson.plugins.promoted_builds.JobPropertyImpl;
import hudson.plugins.promoted_builds.PromotedProjectAction;
import hudson.plugins.promoted_builds.PromotionProcess;
import hudson.tasks.ArtifactArchiver;
import hudson.tasks.BuildStep;

/**
* @author Arnaud Héritier <aheritier@exoplatform.com>
*/
@Extension(optional = true)
public class AppaloosaRunListener extends RunListener<AbstractBuild> {

public void onCompleted(AbstractBuild r, TaskListener listener) {
// Searching for the promotion plugin
if (r.getProject().getProperty(JobPropertyImpl.class) != null) {
JobProperty promotionJobProperty = r.getProject().getProperty(JobPropertyImpl.class);
// Search in promotions if one of them is using Appaloosa
for (PromotionProcess process : ((PromotedProjectAction) promotionJobProperty.getProjectAction(r.getProject())).getProcesses()) {
// Search for a build step using Appaloosa
for (BuildStep buildStep : process.getBuildSteps()) {
if (buildStep instanceof AppaloosaPublisher) {
listener.getLogger().println(Messages.AppaloosaRunListener_AppaloosaInPromotion(((AppaloosaPublisher) buildStep).filePattern));
// If appaloosa is activated in a promotion it may be needed to deploy binaries that
// aren't the latest in the workspace.
// Thus we will silently archive required artifacts to use them.
try {
// TODO : Do we need avoid to archive something already done by the job itself ?
// It is from POV complex to check if all artifacts deployed by appaloosa are
// saved by the archiver
//if (r.getProject().getPublishersList().get(ArtifactArchiver.class) == null) {
//}
new ArtifactArchiver(((AppaloosaPublisher) buildStep).filePattern, null, true).perform(r, null, (BuildListener) listener);
} catch (InterruptedException e) {
listener.error(Messages.AppaloosaRunListener_BackupError(), e);
}
}
}
}
}
}

}
Expand Up @@ -35,3 +35,6 @@ AppaloosaPublisher.proxyHost=Proxy Host
AppaloosaPublisher.proxyPort=Proxy Port
AppaloosaPublisher.proxyUser=Proxy User
AppaloosaPublisher.proxyPass=Proxy Pass
AppaloosaPublisher.RootDirectory=Root directory to find files to upload : {0}
AppaloosaRunListener.AppaloosaInPromotion=There is an appaloosa step in a promotion, we need to archive artifacts ({0}) to deploy them later.
AppaloosaRunListener.BackupError=Error while backing up

0 comments on commit 2dd6f0f

Please sign in to comment.