Skip to content

Commit

Permalink
[JENKINS-19973] Add option to hide ssh command from console output
Browse files Browse the repository at this point in the history
The commands are visible by default on both new configuration
and the "migrated" ones to retain backward compatibility.
  • Loading branch information
ljader committed Apr 12, 2018
1 parent 12ec2e9 commit af0093c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
23 changes: 21 additions & 2 deletions src/main/java/org/jvnet/hudson/plugins/SSHBuildWrapper.java
Expand Up @@ -36,6 +36,7 @@
import org.jenkinsci.plugins.jsch.JSchConnector;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

Expand All @@ -52,6 +53,7 @@ public final class SSHBuildWrapper extends BuildWrapper {
private String siteName;
private String preScript;
private String postScript;
private boolean hideCommand;

public SSHBuildWrapper() {
}
Expand Down Expand Up @@ -96,7 +98,11 @@ private boolean executePreBuildScript(AbstractBuild<?, ?> build, BuildListener l
vars.putAll(build.getEnvironment(listener));
vars.putAll(build.getBuildVariables());
String runtime_cmd = VariableReplacerUtil.preludeWithEnvVars(preScript, vars);
log(logger, "executing pre build script:\n" + VariableReplacerUtil.scrub(runtime_cmd, vars, build.getSensitiveBuildVariables()));
if (hideCommand) {
log(logger, "executing pre build script");
} else {
log(logger, "executing pre build script:\n" + VariableReplacerUtil.scrub(runtime_cmd, vars, build.getSensitiveBuildVariables()));
}
if (runtime_cmd != null && !runtime_cmd.trim().equals("")) {
return site.executeCommand(logger, runtime_cmd) == 0;
}
Expand All @@ -116,7 +122,11 @@ private boolean executePostBuildScript(AbstractBuild<?, ?> build, BuildListener
vars.putAll(build.getEnvironment(listener));
vars.putAll(build.getBuildVariables());
String runtime_cmd = VariableReplacerUtil.preludeWithEnvVars(postScript, vars);
log(logger, "executing post build script:\n" + VariableReplacerUtil.scrub(runtime_cmd, vars, build.getSensitiveBuildVariables()));
if (hideCommand) {
log(logger, "executing post build script");
} else {
log(logger, "executing post build script:\n" + VariableReplacerUtil.scrub(runtime_cmd, vars, build.getSensitiveBuildVariables()));
}
if (runtime_cmd != null && !runtime_cmd.trim().equals("")) {
return site.executeCommand(logger, runtime_cmd) == 0;
}
Expand All @@ -139,6 +149,15 @@ public void setPostScript(String postScript) {
this.postScript = postScript;
}

public boolean isHideCommand() {
return hideCommand;
}

@DataBoundSetter
public void setHideCommand(boolean hideCommand) {
this.hideCommand = hideCommand;
}

public CredentialsSSHSite getSite() {
CredentialsSSHSite[] sites = DESCRIPTOR.getSites();

Expand Down
23 changes: 18 additions & 5 deletions src/main/java/org/jvnet/hudson/plugins/SSHBuilder.java
Expand Up @@ -12,6 +12,7 @@
import hudson.util.ListBoxModel;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

Expand All @@ -24,6 +25,7 @@ public class SSHBuilder extends Builder {
private String siteName;
private String command;
private boolean execEachLine;
private boolean hideCommand;

@Deprecated
public SSHBuilder(String siteName, String command) {
Expand Down Expand Up @@ -62,6 +64,15 @@ public void setExecEachLine(boolean execEachLine) {
this.execEachLine = execEachLine;
}

@DataBoundSetter
public void setHideCommand(boolean hideCommand) {
this.hideCommand = hideCommand;
}

public boolean isHideCommand() {
return hideCommand;
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
CredentialsSSHSite site = getSite();
Expand All @@ -81,11 +92,13 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
String scrubbed_cmd = VariableReplacerUtil.scrub(runtime_cmd, vars, build.getSensitiveBuildVariables());

if (runtime_cmd != null && runtime_cmd.trim().length() > 0) {
if (execEachLine) {
listener.getLogger().printf("[SSH] commands:%n%s%n", scrubbed_cmd);
}
else {
listener.getLogger().printf("[SSH] script:%n%s%n", scrubbed_cmd);
if (!hideCommand) {
if (execEachLine) {
listener.getLogger().printf("[SSH] commands:%n%s%n", scrubbed_cmd);
}
else {
listener.getLogger().printf("[SSH] script:%n%s%n", scrubbed_cmd);
}
}
listener.getLogger().printf("%n[SSH] executing...%n");
return site.executeCommand(listener.getLogger(), runtime_cmd, execEachLine) == 0;
Expand Down
Expand Up @@ -13,4 +13,7 @@
<f:textarea name="ssh.postScript" value="${instance.postScript}"/>
</f:entry>

<f:entry title="${%Hide command from console output}" field="hideCommand">
<f:checkbox />
</f:entry>
</j:jelly>
Expand Up @@ -12,4 +12,7 @@
<f:checkbox />
</f:entry>

<f:entry title="${%Hide command from console output}" field="hideCommand">
<f:checkbox />
</f:entry>
</j:jelly>

0 comments on commit af0093c

Please sign in to comment.