Skip to content

Commit

Permalink
Merge pull request #28 from alexbrjo/rebase-pipeline-compat
Browse files Browse the repository at this point in the history
[JENKINS-44810] Pipeline Compatibility, again
  • Loading branch information
frekele committed Sep 29, 2017
2 parents 53e1f32 + 0c7359e commit 27b805a
Show file tree
Hide file tree
Showing 25 changed files with 463 additions and 64 deletions.
68 changes: 53 additions & 15 deletions pom.xml
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.642.4</version>
<version>2.29</version>
</parent>

<artifactId>deploy</artifactId>
Expand Down Expand Up @@ -42,6 +42,8 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<cargo.version>1.5.0</cargo.version>
<jenkins.version>1.642.3</jenkins.version>
<workflow-cps-plugin.version>2.15</workflow-cps-plugin.version>
</properties>

<dependencies>
Expand All @@ -56,20 +58,6 @@
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-core-api-generic</artifactId>
<version>${cargo.version}</version>
<exclusions>
<exclusion>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
</exclusion>
<exclusion>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
</exclusion>
<exclusion>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand Down Expand Up @@ -126,6 +114,56 @@
<version>1.4.01</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.5</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-api</artifactId>
<version>2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>2.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>${workflow-cps-plugin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<version>1.14</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>${workflow-cps-plugin.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
44 changes: 31 additions & 13 deletions src/main/java/hudson/plugins/deploy/CargoContainerAdapter.java
Expand Up @@ -4,8 +4,9 @@
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.model.BuildListener;
import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.remoting.VirtualChannel;
import hudson.util.VariableResolver;

Expand Down Expand Up @@ -44,14 +45,16 @@ public abstract class CargoContainerAdapter extends ContainerAdapter implements
/**
* Returns the container ID used by Cargo.
*
* @return
* @return the id of the container
*/
protected abstract String getContainerId();

/**
* Fills in the {@link Configuration} object.
*
* @param config
* @param config the configuration of the adapter
* @param envVars the environmental variables of the build
* @param resolver the variable resolver
*/
protected abstract void configure(Configuration config, EnvVars envVars, VariableResolver<String> resolver);

Expand All @@ -61,10 +64,10 @@ protected Container getContainer(ConfigurationFactory configFactory, ContainerFa
return containerFactory.createContainer(id, ContainerType.REMOTE, config);
}

protected void deploy(DeployerFactory deployerFactory, final BuildListener listener, Container container, File f, String contextPath) {
protected void deploy(DeployerFactory deployerFactory, final TaskListener listener, Container container, File f, String contextPath) {
Deployer deployer = deployerFactory.createDeployer(container);

listener.getLogger().println("Deploying " + f + " to container " + container.getName() + " with context " + contextPath);
listener.getLogger().println("[DeployPublisher][INFO] Deploying " + f + " to container " + container.getName() + " with context " + contextPath);
deployer.setLogger(new LoggerImpl(listener.getLogger()));


Expand All @@ -79,7 +82,7 @@ protected void deploy(DeployerFactory deployerFactory, final BuildListener liste
EAR ear = createEAR(f);
deployer.redeploy(ear);
} else {
throw new RuntimeException("Extension File Error.");
throw new RuntimeException("Extension File Error. Unsupported: .\"" + extension + "\"");
}
}

Expand All @@ -93,8 +96,15 @@ protected WAR createWAR(File deployableFile) {
return new WAR(deployableFile.getAbsolutePath());
}

/**
* Expands an encoded environment variable. Ex. if HOME=/user/alex, expands '${HOME}' to '/user/alex'
*
* @param envVars the environment variables of the build
* @param resolver unused
* @param variable the variable to expand
* @return the value of the expanded variable
*/
protected String expandVariable(EnvVars envVars, VariableResolver<String> resolver, String variable) {
String temp = envVars.expand(variable);
return Util.replaceMacro(envVars.expand(variable), resolver);
}

Expand All @@ -103,27 +113,35 @@ protected String expandVariable(EnvVars envVars, VariableResolver<String> resolv
* Creates a Deployable object EAR from the given file object.
*
* @param deployableFile The deployable file to create the Deployable from.
* @return A Deployable object.
* @return A deployable object.
*/
protected EAR createEAR(File deployableFile) {
return new EAR(deployableFile.getAbsolutePath());
}

public boolean redeploy(FilePath war, final String contextPath, final AbstractBuild<?, ?> build, Launcher launcher,
final BuildListener listener) throws IOException, InterruptedException {
return war.act(new DeployCallable(this, getContainerId(), build.getEnvironment(listener), listener, contextPath));
/**
* {@inheritDoc}
*/
@Override
public void redeployFile(FilePath war, final String contextPath, final Run<?, ?> run, final Launcher launcher, final TaskListener listener) throws IOException, InterruptedException {
EnvVars envVars = new EnvVars();
if (run instanceof AbstractBuild) {
AbstractBuild build = (AbstractBuild) run;
envVars = build.getEnvironment(listener);
}
war.act(new DeployCallable(this, getContainerId(), envVars, listener, contextPath));
}

public static class DeployCallable extends MasterToSlaveFileCallable<Boolean> {

private CargoContainerAdapter adapter;
private String containerId;
private BuildListener listener;
private TaskListener listener;
private String contextPath;
private EnvVars envVars;

public DeployCallable (CargoContainerAdapter adapter, String containerId, EnvVars envVars,
BuildListener listener, String contextPath) {
TaskListener listener, String contextPath) {
this.adapter = adapter;
this.containerId = containerId;
this.envVars = envVars;
Expand Down
51 changes: 47 additions & 4 deletions src/main/java/hudson/plugins/deploy/ContainerAdapter.java
@@ -1,16 +1,21 @@
package hudson.plugins.deploy;

import hudson.AbortException;
import hudson.DescriptorExtensionList;
import hudson.ExtensionPoint;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Describable;
import hudson.model.Hudson;
import hudson.model.Run;
import hudson.model.TaskListener;
import jenkins.model.Jenkins;

import java.io.IOException;

import static hudson.Util.isOverridden;

/**
* Encapsulates container-specific deployment operation.
*
Expand All @@ -22,18 +27,56 @@
* @author Kohsuke Kawaguchi
*/
public abstract class ContainerAdapter implements Describable<ContainerAdapter>, ExtensionPoint {

@Deprecated
public boolean redeploy(FilePath war, String aContextPath, AbstractBuild<?,?> build, Launcher launcher, final BuildListener listener) throws IOException, InterruptedException {
redeployFile(war, aContextPath, build, launcher, listener);
return true;
}

/**
* Perform redeployment.
*
* If failed, return false.
*
* Implementations should override me and make {@link #redeploy(FilePath, String, AbstractBuild, Launcher, BuildListener)}
* delegate to that implementation to be usable within Pipeline projects
*
* @param war the path of the war/ear file to deploy
* @param aContextPath the context path for the war to be deployed
* @param build the build that is being deployed
* @param launcher the launcher of the build
* @param listener the BuildListener of the build to deploy
* @throws IOException if there is an error locating the war file
* @throws InterruptedException if there is an error deploying to the server
*/
public abstract boolean redeploy(FilePath war, String aContextPath, AbstractBuild<?,?> build, Launcher launcher, final BuildListener listener) throws IOException, InterruptedException;
public void redeployFile(FilePath war, String aContextPath, Run<?,?> build, Launcher launcher, final TaskListener listener) throws IOException, InterruptedException {
if (build instanceof AbstractBuild) {
if (isOverridden(ContainerAdapter.class, getClass(), "redeploy",
FilePath.class, String.class, AbstractBuild.class, Launcher.class, BuildListener.class)) {
if (!redeploy(war, aContextPath, (AbstractBuild<?, ?>) build, launcher, (BuildListener) listener)) {
throw new AbortException("Deployment failed for unknown reason");
}
} else {
throw new AbortException(
"This ContainerAdapter doesn't have an implementation of redeployFile(). Please contact " +
"the plugin maintainer and ask them to update their plugin to be compatible with Workflow"
);
}
} else {
throw new AbortException(
"[JENKINS-44810] redeploy() called using a Run, but this ContainerAdapter doesn't have an " +
"implementation for Run. Please contact the plugin maintainer and ask them to update their " +
"plugin to be compatible pipeline"
);
}
}

public ContainerAdapterDescriptor getDescriptor() {
return (ContainerAdapterDescriptor)Hudson.getInstance().getDescriptor(getClass());
return (ContainerAdapterDescriptor)Jenkins.getActiveInstance().getDescriptor(getClass());
}

public static DescriptorExtensionList<ContainerAdapter,ContainerAdapterDescriptor> all() {
return Hudson.getInstance().<ContainerAdapter,ContainerAdapterDescriptor>getDescriptorList(ContainerAdapter.class);
return Jenkins.getActiveInstance().<ContainerAdapter,ContainerAdapterDescriptor>getDescriptorList(ContainerAdapter.class);
}
}
Expand Up @@ -27,6 +27,7 @@ public abstract class DefaultCargoContainerAdapterImpl extends CargoContainerAda
public @interface Property {
/**
* Property name.
* @return the property name
*/
String value();
}
Expand Down

0 comments on commit 27b805a

Please sign in to comment.