in core/src/main/java/hudson/cli/BuildCommand.java [102:221]
protected int run() throws Exception {
job.checkPermission(Item.BUILD);
ParametersAction a = null;
if (!parameters.isEmpty()) {
ParametersDefinitionProperty pdp = job.getProperty(ParametersDefinitionProperty.class);
if (pdp==null)
throw new IllegalStateException(job.getFullDisplayName()+" is not parameterized but the -p option was specified.");
//TODO: switch to type annotations after the migration to Java 1.8
List<ParameterValue> values = new ArrayList<>();
for (Entry<String, String> e : parameters.entrySet()) {
String name = e.getKey();
ParameterDefinition pd = pdp.getParameterDefinition(name);
if (pd==null) {
String nearest = EditDistance.findNearest(name, pdp.getParameterDefinitionNames());
throw new CmdLineException(null, nearest == null ?
String.format("'%s' is not a valid parameter.", name) :
String.format("'%s' is not a valid parameter. Did you mean %s?", name, nearest));
}
ParameterValue val = pd.createValue(this, Util.fixNull(e.getValue()));
if (val == null) {
throw new CmdLineException(null, String.format("Cannot resolve the value for the parameter '%s'.",name));
}
values.add(val);
}
// handle missing parameters by adding as default values ISSUE JENKINS-7162
for(ParameterDefinition pd : pdp.getParameterDefinitions()) {
if (parameters.containsKey(pd.getName()))
continue;
// not passed in use default
ParameterValue defaultValue = pd.getDefaultParameterValue();
if (defaultValue == null) {
throw new CmdLineException(null, String.format("No default value for the parameter '%s'.",pd.getName()));
}
values.add(defaultValue);
}
a = new ParametersAction(values);
}
if (checkSCM) {
SCMTriggerItem item = SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(job);
if (item == null)
throw new AbortException(job.getFullDisplayName()+" has no SCM trigger, but checkSCM was specified");
// preemptively check for a polling veto
if (SCMDecisionHandler.firstShouldPollVeto(job) != null) {
return 0;
}
if (!item.poll(new StreamTaskListener(stdout, getClientCharset())).hasChanges())
return 0;
}
if (!job.isBuildable()) {
String msg = Messages.BuildCommand_CLICause_CannotBuildUnknownReasons(job.getFullDisplayName());
if (job instanceof ParameterizedJobMixIn.ParameterizedJob && ((ParameterizedJobMixIn.ParameterizedJob) job).isDisabled()) {
msg = Messages.BuildCommand_CLICause_CannotBuildDisabled(job.getFullDisplayName());
} else if (job.isHoldOffBuildUntilSave()){
msg = Messages.BuildCommand_CLICause_CannotBuildConfigNotSaved(job.getFullDisplayName());
}
throw new IllegalStateException(msg);
}
Queue.Item item = ParameterizedJobMixIn.scheduleBuild2(job, 0, new CauseAction(new CLICause(Jenkins.getAuthentication().getName())), a);
QueueTaskFuture<? extends Run<?,?>> f = item != null ? (QueueTaskFuture)item.getFuture() : null;
if (wait || sync || follow) {
if (f == null) {
throw new IllegalStateException(BUILD_SCHEDULING_REFUSED);
}
Run<?,?> b = f.waitForStart(); // wait for the start
stdout.println("Started "+b.getFullDisplayName());
stdout.flush();
if (sync || follow) {
try {
if (consoleOutput) {
// read output in a retry loop, by default try only once
// writeWholeLogTo may fail with FileNotFound
// exception on a slow/busy machine, if it takes
// longish to create the log file
int retryInterval = 100;
for (int i=0;i<=retryCnt;) {
try {
b.writeWholeLogTo(stdout);
break;
}
catch (FileNotFoundException | NoSuchFileException e) {
if ( i == retryCnt ) {
Exception myException = new AbortException();
myException.initCause(e);
throw myException;
}
i++;
Thread.sleep(retryInterval);
}
}
}
f.get(); // wait for the completion
stdout.println("Completed "+b.getFullDisplayName()+" : "+b.getResult());
return b.getResult().ordinal;
} catch (InterruptedException e) {
if (follow) {
return 125;
} else {
// if the CLI is aborted, try to abort the build as well
f.cancel(true);
Exception myException = new AbortException();
myException.initCause(e);
throw myException;
}
}
}
}
return 0;
}