Skip to content

Commit

Permalink
Merge pull request #4 from recena/JENKINS-28689
Browse files Browse the repository at this point in the history
[JENKINS-28689] Make compatible with Workflow plugin
  • Loading branch information
stephenc committed Aug 5, 2015
2 parents 7c39895 + 050eebf commit 5a72bf8
Show file tree
Hide file tree
Showing 13 changed files with 907 additions and 24 deletions.
60 changes: 48 additions & 12 deletions pom.xml
Expand Up @@ -29,7 +29,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.532.2</version>
<version>1.609.1</version>
</parent>

<artifactId>ssh-agent</artifactId>
Expand Down Expand Up @@ -70,6 +70,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<findbugs-maven-plugin.version>3.0.1</findbugs-maven-plugin.version>
<findbugs.failOnError>true</findbugs.failOnError>
<workflow-jenkins-plugin.version>1.9-beta-2-SNAPSHOT</workflow-jenkins-plugin.version>
</properties>

<repositories>
Expand All @@ -90,7 +91,7 @@
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>0.12.0</version>
<version>0.14.0</version>
</dependency>
<dependency>
<groupId>tomcat</groupId>
Expand All @@ -117,6 +118,18 @@
<artifactId>jnr-unixsocket-nodep</artifactId>
<version>0.3.1</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>${workflow-jenkins-plugin.version}</version>
</dependency>

<!-- plugin dependencies -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand All @@ -131,17 +144,42 @@
<!-- jenkins dependencies -->
<!-- test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<groupId>org.jenkins-ci.modules</groupId>
<artifactId>sshd</artifactId>
<version>1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>${workflow-jenkins-plugin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>${workflow-jenkins-plugin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<version>${workflow-jenkins-plugin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>${workflow-jenkins-plugin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-support</artifactId>
<version>${workflow-jenkins-plugin.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -165,9 +203,7 @@
</requireMavenVersion>
<requireMavenVersion>
<version>(,3.0),[3.0.4,)</version>
<message>Maven 3.0 through 3.0.3 inclusive do not pass correct settings.xml to Maven Release
Plugin.
</message>
<message>Maven 3.0 through 3.0.3 inclusive do not pass correct settings.xml to Maven Release Plugin</message>
</requireMavenVersion>
</rules>
</configuration>
Expand Down
Expand Up @@ -39,18 +39,19 @@
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
import hudson.model.Hudson;
import hudson.model.Item;
import hudson.security.ACL;
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;
import hudson.util.IOException2;
import hudson.util.ListBoxModel;
import hudson.util.Secret;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.Stapler;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.ObjectStreamException;
import java.io.PrintWriter;
Expand All @@ -61,7 +62,6 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* A build wrapper that provides an SSH agent using supplied credentials
Expand Down Expand Up @@ -238,8 +238,8 @@ private Environment createSSHAgentEnvironment(AbstractBuild build, Launcher laun
* @param c the credentials.
* @return the description.
*/
@NonNull
private static String description(@NonNull StandardUsernameCredentials c) {
@Nonnull
public static String description(@Nonnull StandardUsernameCredentials c) {
String description = Util.fixEmptyAndTrim(c.getDescription());
return c.getUsername() + (description != null ? " (" + description + ")" : "");
}
Expand Down Expand Up @@ -307,7 +307,7 @@ public SSHAgentEnvironment(Launcher launcher, final BuildListener listener,
RemoteAgent agent = null;
listener.getLogger().println("[ssh-agent] Looking for ssh-agent implementation...");
Map<String, Throwable> faults = new LinkedHashMap<String, Throwable>();
for (RemoteAgentFactory factory : Hudson.getInstance().getExtensionList(RemoteAgentFactory.class)) {
for (RemoteAgentFactory factory : Jenkins.getActiveInstance().getExtensionList(RemoteAgentFactory.class)) {
if (factory.isSupported(launcher, listener)) {
try {
listener.getLogger().println("[ssh-agent] " + factory.getDisplayName());
Expand Down
@@ -0,0 +1,99 @@
package com.cloudbees.jenkins.plugins.sshagent;

import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardUsernameListBoxModel;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import hudson.Extension;
import hudson.model.Item;
import hudson.security.ACL;
import hudson.util.ListBoxModel;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.Stapler;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

public class SSHAgentStep extends AbstractStepImpl implements Serializable {

private static final long serialVersionUID = 1L;

/**
* The {@link com.cloudbees.plugins.credentials.common.StandardUsernameCredentials#getId()}s of the credentials
* to use.
*/
private final List<String> credentials;

/**
* If a credentials is missed, the SSH Agent is launched anyway.
* By the fault is false. Initialized in the constructor.
*/
private boolean ignoreMissing;

/**
* Default parameterized constructor.
*
* @param credentials
*/
@DataBoundConstructor
public SSHAgentStep(final List<String> credentials) {
this.credentials = credentials;
this.ignoreMissing = false;
}

@Extension
public static final class DescriptorImpl extends AbstractStepDescriptorImpl {

public DescriptorImpl() {
super(SSHAgentStepExecution.class);
}

@Override
public String getFunctionName() {
return "sshagent";
}

@Override
public String getDisplayName() {
return Messages.SSHAgentBuildWrapper_DisplayName();
}

@Override
public boolean takesImplicitBlockArgument() {
return true;
}

/**
* Populate the list of credentials available to the job.
*
* @return the list box model.
*/
@SuppressWarnings("unused") // used by stapler
public ListBoxModel doFillCredentialsItems() {
Item item = Stapler.getCurrentRequest().findAncestorObject(Item.class);
return new StandardUsernameListBoxModel().withAll(
CredentialsProvider.lookupCredentials(SSHUserPrivateKey.class, item, ACL.SYSTEM,
Collections.<DomainRequirement>emptyList())
);
}

}

@DataBoundSetter
public void setIgnoreMissing(final boolean ignoreMissing) {
this.ignoreMissing = ignoreMissing;
}

public boolean isIgnoreMissing() {
return ignoreMissing;
}

public List<String> getCredentials() {
return credentials;
}

}

0 comments on commit 5a72bf8

Please sign in to comment.