Skip to content

Commit

Permalink
[FIXED JENKINS-9968] added an option to do polling before a build.
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsuke committed Aug 10, 2011
1 parent b375889 commit 259477a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
3 changes: 3 additions & 0 deletions changelog.html
Expand Up @@ -77,6 +77,9 @@
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-10414">issue 10414</a>)
<li class=rfe>
Added a dignosis CLI command to report the current granted authorities.
<li class=rfe>
Added an option in CLI build command to check for SCM changes before carrying out a build
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-9968">issue 9968</a>)
<li class=rfe>
If CLI fails to connect via a JNLP Slave port, fall back to HTTP full-duplex.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-10611">issue 10611</a>)
Expand Down
17 changes: 16 additions & 1 deletion core/src/main/java/hudson/cli/BuildCommand.java
Expand Up @@ -30,13 +30,17 @@
import hudson.model.ParameterValue;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.ParameterDefinition;
import hudson.model.TaskListener;
import hudson.Extension;
import hudson.AbortException;
import hudson.model.Item;
import hudson.util.EditDistance;
import hudson.scm.PollingResult;
import hudson.util.StreamTaskListener;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;

import java.nio.charset.Charset;
import java.util.concurrent.Future;
import java.util.Map;
import java.util.HashMap;
Expand All @@ -63,6 +67,9 @@ public String getShortDescription() {
@Option(name="-s",usage="Wait until the completion/abortion of the command")
public boolean sync = false;

@Option(name="-c",usage="Check for SCM changes before starting the build, and if there's no change, exit without doing a build")
public boolean checkSCM = false;

@Option(name="-p",usage="Specify the build parameters in the key=value format.")
public Map<String,String> parameters = new HashMap<String, String>();

Expand All @@ -89,6 +96,12 @@ protected int run() throws Exception {
a = new ParametersAction(values);
}

if (checkSCM) {
if (job.poll(new StreamTaskListener(stdout, getClientCharset())) == PollingResult.NO_CHANGES) {
return 0;
}
}

Future<? extends AbstractBuild> f = job.scheduleBuild2(0, new CLICause(), a);
if (!sync) return 0;

Expand All @@ -104,7 +117,9 @@ protected void printUsageSummary(PrintStream stderr) {
"Aside from general scripting use, this command can be\n" +
"used to invoke another job from within a build of one job.\n" +
"With the -s option, this command changes the exit code based on\n" +
"the outcome of the build (exit code 0 indicates a success.)\n"
"the outcome of the build (exit code 0 indicates a success.)\n" +
"With the -c option, a build will only run if there has been\n" +
"an SCM change"
);
}

Expand Down
21 changes: 21 additions & 0 deletions core/src/main/java/hudson/cli/CLICommand.java
Expand Up @@ -51,8 +51,11 @@
import java.io.InputStream;
import java.io.PrintStream;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
Expand Down Expand Up @@ -303,6 +306,24 @@ public String call() throws IOException {
private static final long serialVersionUID = 1L;
}

protected Charset getClientCharset() throws IOException, InterruptedException {
String charsetName = channel.call(new GetCharset());
try {
return Charset.forName(charsetName);
} catch (UnsupportedCharsetException e) {
LOGGER.log(Level.FINE,"Server doesn't have charset "+charsetName);
return Charset.defaultCharset();
}
}

private static final class GetCharset implements Callable<String, IOException> {
public String call() throws IOException {
return Charset.defaultCharset().name();
}

private static final long serialVersionUID = 1L;
}

/**
* Convenience method for subtypes to obtain environment variables of the client.
*/
Expand Down

0 comments on commit 259477a

Please sign in to comment.